rails' utc_to_local and daylight savings time

这一生的挚爱 提交于 2019-12-10 19:16:20

问题


> e = Event.first
> e.registration_start_utc  #registration_start_utc is a datetime column
 => Sat, 23 Oct 2010 06:38:00 UTC +00:00 
> e.registration_start_utc.utc?
 => true 
> ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(e.registration_start_utc)
 => Sat, 23 Oct 2010 02:38:00 UTC +00:00

2 questions about this:

1) Why is that last output showing "UTC" -- the hour got converted (6 => 2) but it still says UTC. Why not EST/EDT?

2) What happens after daylight savings time switches over and the offset for New York moves from -4 to -5? The value in the DB doesn't change so my only conclusion is that my app will start showing "1:38" everywhere instead of the correct 2:38?

I'm mostly concerned with #2 here. #1 is more of a curiosity.

Thanks!


回答1:


2) utc_to_local uses the date to determine which offset is correct, so the output will always be the same for a given date.

You can test for that like this:

t = Time.utc(2011,3, 14, 12)
# => 2011-03-14 12:00:00 UTC
t2 = Time.utc(2011,3, 11, 12)
# => 2011-03-11 12:00:00 UTC
ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(t)
# => 2011-03-14 08:00:00 UTC
ActiveSupport::TimeZone.find_tzinfo("America/New_York").utc_to_local(t2)
# => 2011-03-14 07:00:00 UTC

1) It doesn't seem right to me either. My guess is that they are interested only in the actual value of the hour, minutes, etc... and disregard the timezone.

In any case, you might be better off using:

e.registration_start_utc.in_time_zone("Eastern Time (US & Canada)")

See also this question I just asked...



来源:https://stackoverflow.com/questions/4002958/rails-utc-to-local-and-daylight-savings-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!