Time.use_zone is not working as expected
Asked Answered
K

5

20

So right now it is 2:54 PM PST in San Francisco. For some reason, this code block is not returning 12:54 PM HST in Hawaii. Am I missing something here? I would expect this code to return me the current time in Hawaii

Time.use_zone('Hawaii') do
  Time.now
end
# => 2012-01-03 14:54:54 -0800 
Kramer answered 3/1, 2012 at 22:52 Comment(0)
A
36

This should work ok:

Time.use_zone('Hawaii') do
  p Time.zone.now
end
Abrogate answered 3/1, 2012 at 23:8 Comment(0)
R
11

Try using Time.now.in_time_zone inside your block instead.

> Time.use_zone('Hawaii') do
>   Time.now.in_time_zone
> end
 => Tue, 03 Jan 2012 13:07:06 HST -10:00 
Rame answered 3/1, 2012 at 23:7 Comment(0)
A
9

Use Time.current if you want now with timezone support. Time.now is dangerous when working in a timezone aware application, as a rule of thumb I never use Time.now, only Time.current. Rails time helpers like 2.hours.ago and 4.days.from_now are based off of Time.current as well.

# Time.current will use Time.zone when needed (when Time.zone is present)
def current
  ::Time.zone ? ::Time.zone.now : ::Time.now
end

Also, this is a great article with a great cheat sheet at the bottom: https://www.varvet.com/blog/working-with-time-zones-in-ruby-on-rails/

DOs

code result
2.hours.ago Thu, 27 Aug 2015 14:39:36 AFT +04:30
1.day.from_now Fri, 28 Aug 2015 16:39:36 AFT +04:30
Time.zone.parse("2015-08-27T12:09:36Z") Thu, 27 Aug 2015 16:39:36 AFT +04:30
Time.current Thu, 27 Aug 2015 16:39:36 AFT +04:30
When supliyng an API Time.current.utc.iso8601 2015-08-27T12:09:36Z
If you can’t use Time.zone.parse Time.strptime("2015-08-27T12:09:36Z", "%Y-%m-%dT%H:%M:%S%z").in_time_zone Thu, 27 Aug 2015 16:39:36 AFT +04:30
If you really can’t have a Time or DateTime for some reason Date.current Thu, 27 Aug 2015
If you have a date and want to make the best out of it Date.current.in_time_zone Thu, 27 Aug 2015 00:00:00 AFT +04:30

DON’Ts

code result
Returns system time and ignores your configured time zone. Time.now 2015-08-27 14:09:36 +0200
Will assume time string given is in the system’s time zone. Time.parse("2015-08-27T12:09:36Z") 2015-08-27 12:09:36 UTC
Same problem as with Time.parse. Time.strptime("2015-08-27T12:09:36Z", "%Y-%m-%dT%H:%M:%S%z") 2015-08-27 12:09:36 UTC
This could be yesterday or tomorrow depending on the machine’s time zone, see issue 1 for more info. Date.today Thu, 27 Aug 2015
Agathaagathe answered 9/7, 2014 at 11:37 Comment(0)
L
5

Time.now - using server time
Time.zone.now - using rails application time (in config: config.time_zone)
Time.use_zone - using 'your' timezone for given block


This example is wrong, because Time.now get time in your server timezone and with method in_time_zone translate time into an equivalent time in Hawaii timezone. But it's no current Time in Hawaii! It's your server time with utc offset for Hawaii.

Time.use_zone('Hawaii') do
  Time.now.in_time_zone
end
=> Wed, 14 Aug 2013 10:33:18 HST -10:00 

Time.now.in_time_zone
=> Thu, 15 Aug 2013 00:32:30 MSK +04:00 

For getting time in Hawaii timezone you must use

Time.use_zone('Hawaii') do
  Time.zone.now
end
Lytton answered 14/8, 2013 at 20:23 Comment(0)
C
1

Don't use Time.now this is using your local time zone instead use Time.current

Time.use_zone('Hawaii') do
  p Time.current
end
Curtis answered 26/6, 2020 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.