rails的utc_to_local和夏令时 [英] rails' utc_to_local and daylight savings time

查看:70
本文介绍了rails的utc_to_local和夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> 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个问题:

1)为什么最后一个输出显示 UTC-小时已转换(6 => 2),但仍显示UTC。为什么不使用EST / EDT?

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

2)在夏令时切换并且纽约的偏移量从-4变为-5之后会发生什么? DB中的值没有改变,所以我唯一的结论是我的应用将开始在各处显示 1:38,而不是正确的2:38?

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我最关心这里的第二名。 #1更具好奇心。

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

谢谢!

推荐答案

2) utc_to_local 使用日期来确定哪个偏移量是正确的,因此在给定日期输出将始终相同。

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)我觉得这不合适要么。我的猜测是他们只对小时,分钟等的实际值感兴趣,而忽略了时区。

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...

这篇关于rails的utc_to_local和夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆