# How to Deal with Timezones the Active Support Way

DevFeed: [How to Deal with Timezones the Active Support Way](<https://devfeed.tech/articles/how-to-deal-with-timezones-the-active-support-way-21070.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2016/04/03/how-to-deal-with-timezones-the-active-support-way/>)

Published: 2016-04-03T12:00:00Z

Content type: article

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [DateTime](<https://devfeed.tech/topics/datetime.md>), [Databases](<https://devfeed.tech/topics/databases.md>)

Tags: [database](<https://devfeed.tech/tags/database.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [ruby-on-rails](<https://devfeed.tech/tags/ruby-on-rails.md>), [time](<https://devfeed.tech/tags/time.md>), [timezone](<https://devfeed.tech/tags/timezone.md>)

## AI overview

This tutorial explains how to handle timezones with Ruby on Rails' ActiveSupport. It covers supported timezone lists, parsing timestamps with Time.zone, safely applying temporary zones with Time.use_zone, and converting timestamps between zones, including displaying UTC database timestamps in a user's local time.

## Source excerpt

"Well the code is bad because we had to..." is a phrase one might hear when discussing timezone offsets or daylight savings time considerations. Unless a developer is fortunate enough to work at a company whose userbase is entirely located in the UTC timezone, writing software aware of different timezones can be a daunting task. Luckily, Ruby on Rails' ActiveSupport library has some very nice built in features that can prove invaluable when facing time related issues. Built-in Timezones ActiveSupport in Ruby on Rails 4.0+ has a built in list of all supported timezones on the TimeZone class: ActiveSupport::TimeZone.all.map(&:name) #=> ["American Samoa", "International Date Line West", "Midway Island", "Hawaii", "Alaska", "Pacific Time (US & Canada)", "Tijuana", "Arizona", "Chihuahua", "Mazatlan", "Mountain Time (US & Canada)", "Central America", "Central Time (US & Canada)", "Guadalajara", ...] This list of timezones can be used when parsing strings into Time objects and converting an existing Time object from one timezone to another. For working with timezones in the United States, a handy us_zones method is also available on the same class. One interesting detail about this list of timezones is the lack of daylight savings time qualifiers. Keeping the timezones agnostic of daylight savings helps simplify their use. A developer does not need to worry about using one timezone object over another due to the time of the year. Time.use_zone The ActiveSupport library adds some functionality to built in Ruby classes. One of those additions is the ability to set and retrieve the zone attribute on the Time class. After a zone is set, it can be used when parsing strings into Time objects: Time.zone = 'Pacific Time (US & Canada)' Time.zone.parse('2016-04-01 10:00:00') #=> Fri, 01 Apr 2016 10:00:00 PDT -07:00 This code takes a timestamp string without a timezone specified and uses the value of Time.zone to correctly represent the time in the Pacific timezone. However, an immedia