3
> 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?

2) 夏令时切换并且纽约的偏移量从 -4 变为 -5 后会发生什么?数据库中的值没有改变,所以我唯一的结论是我的应用程序将开始在任何地方显示“1:38”而不是正确的 2:38?

我最关心的是这里的#2。#1 更多的是一种好奇心。

谢谢!

4

1 回答 1

1

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

你可以这样测试:

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)这对我来说似乎也不合适。我的猜测是他们只对小时、分钟等的实际值感兴趣……而忽略时区。

无论如何,您最好使用:

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

另请参阅我刚刚问的这个问题......

于 2011-03-14T15:37:57.483 回答