The default time zone offset in Ruby is apparently -0800. I want to set mine to -0500. How do I do this?
-
3Change the time zone on your OS. – Phrogz Jan 16 '11 at 04:44
-
That worked. If you put that as an answer, I'll accept it. – Jason Swett Jan 16 '11 at 05:39
-
3You should change the Ruby TZ variable. It is set by the host's operating system. Use TZ=EST to set your time zone to -05:00 – Andrew Hendrie Mar 04 '15 at 11:54
-
It's worth noting that due to the disaster that is "Daylight Saving", choosing to specify "EST" and any similar "*ST" and "*DT" values is a very bad idea. EST does always mean -05:00, however it is the incorrect time zone for that region about 7 months out of the year. When setting this value, ALWAYS use the geographical name: "America/New_York" or "America/Los_Angeles" work great. For the exhaustive canonical list, see: https://github.com/tzinfo/tzinfo-data/tree/master/lib/tzinfo/data/definitions – XP84 Mar 05 '20 at 18:20
4 Answers
Set the TZ environment variable...
$ ruby -e 'puts Time.now'
Sat Jan 15 20:49:10 -0800 2011
$ TZ=UTC ruby -e 'puts Time.now'
Sun Jan 16 04:49:20 +0000 2011
Ruby gets the time zone information from the host's operating system.
Most directly, it uses a C library API specified by C99 and Posix.
The implementation of that API is system-specific, on my Mac that means it consults /etc/localtime
unless there is a TZ environment variable.
It's about the same on Linux.

- 6,205
- 4
- 40
- 71

- 143,651
- 25
- 248
- 329
-
Here is the list of available Time Zones https://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html – stopanko Aug 29 '21 at 22:02
updated answer: use ActiveSupport
The more recent releases of Rails's ActiveSupport module offer a MUCH better solution in the active_support/time module. (Note that modules in ActiveSupport can be loaded without dragging in all of Rails...)
I recommend this approach since it doesn't require setting any global state (e.g. setting the time zone on your OS or modifying ENV['TZ']
) that might have unexpected side effects elsewhere. Here's how you use it:
>> require 'active_support/time'
=> true
>> Time.at(1000000000).in_time_zone('US/Eastern')
=> Sat, 08 Sep 2001 21:46:40 EDT -04:00
>> Time.at(1000000000).in_time_zone('US/Pacific')
=> Sat, 08 Sep 2001 18:46:40 PDT -07:00
PS: if you want to see all of the time zone names supported, you can refer to:
>> ActiveSupport::TimeZone::MAPPING
=> => {"International Date Line West"=>"Pacific/Midway", "Midway Island"=>"Pacific/Midway", ...}
(original answer -- now outdated)
A bit late to the party, but found that I needed to set the time zone to different values according to user data.
What I used to do was (but see update below):
def with_time_zone(tz_name)
prev_tz = ENV['TZ']
ENV['TZ'] = tz_name
yield
ensure
ENV['TZ'] = prev_tz
end
Which permits things like:
>> with_time_zone('US/Eastern') { puts Time.at(1000000000) }
2001-09-08 21:46:40 -0400
>> with_time_zone('US/Pacific') { puts Time.at(1000000000) }
2001-09-08 18:46:40 -0700

- 1
- 1

- 33,645
- 23
- 135
- 217
To programmatically set the ruby time zone, also set environment variables from within ruby by accessing the ENV hash:
ENV['TZ'] = 'UTC'
Time.at 0
#=> 1970-01-01 00:00:00 +0000
This helps to avoid the need to modify your OS just for an app, and gives you more portability of your app if you move it to a different machine. If you're using Rails, ActiveSupport::TimeZone also offers some functionality to help with overriding the TimeZone.

- 4,511
- 2
- 31
- 36
Change the time zone on your OS; Ruby will pick up the change.

- 296,393
- 112
- 651
- 745
-
4This solution works, but it isn't terribly portable. It could cause your code to behave differently on different machines if they don't share this OS level setting. You may be better off overriding the time-zone programmatically, but it really depends on your needs. There are definitely scenarios where it's good to set your machine's time-zone to UTC, for example on servers. However, it means your development machines would could be using a different time-zone, or other programs (such as an email/calendar client) would be forced to use UTC. – jsears Jan 13 '16 at 15:27
-
4That worked great, but I was two hours late for my dentist appointment. – Cary Swoveland Oct 23 '19 at 20:52