I'm having trouble converting a UTC Time
or TimeWithZone
to local time in Rails 3.
在Rails 3中,我无法将UTC时间或TimeWithZone转换为本地时间。
Say moment
is some Time
variable in UTC (e.g. moment = Time.now.utc
). How do I convert moment
to my time zone, taking care of DST (i.e. using EST/EDT)?
表示moment是UTC中的某个时间变量(例如moment = Time.now. UTC)。如何将时间转换到时区,处理DST(即使用EST/EDT)?
More precisely, I'd like to printout "Monday March 14, 9 AM" if the time correspond to this morning 9 AM EDT and "Monday March 7, 9 AM" if the time was 9 AM EST last monday.
更准确地说,我想打印出“周一3月14日,上午9点”,如果时间是美国东部时间上午9点,我想打印出“周一,3月7日,9点”,如果时间是美国东部时间周一上午9点。
Hopefully there's another way?
希望有另一种方式?
Edit: I first thought that "EDT" should be a recognized timezone, but "EDT" is not an actual timezone, more like the state of a timezone. For instance it would not make any sense to ask for Time.utc(2011,1,1).in_time_zone("EDT")
. It is a bit confusing, as "EST" is an actual timezone, used in a few places that do not use Daylight savings time and are (UTC-5) yearlong.
编辑:我最初认为“EDT”应该是一个公认的时区,但“EDT”不是一个实际的时区,更像是一个时区的状态。例如,询问Time.utc(2011,1,1).in_time_zone(“EDT”)没有任何意义。这有点令人困惑,因为“EST”是一个实际的时区,在一些不使用夏令时的地方使用,而且使用时间(UTC-5)年。
5 个解决方案
#1
100
Rails has its own names. See them with:
Rails有自己的名字。看到他们:
rake time:zones:us
You can also run rake time:zones:all
for all time zones. To see more zone-related rake tasks: rake -D time
您还可以运行rake time:zone:all for all time zone。要查看更多与区域相关的rake任务:rake -D time
So, to convert to EST, catering for DST automatically:
因此,要转换为EST,自动迎合DST:
Time.now.in_time_zone("Eastern Time (US & Canada)")
#2
105
Time#localtime
will give you the time in the current time zone of the machine running the code:
时间#localtime将给您在运行代码的机器当前时区中的时间:
> moment = Time.now.utc
=> 2011-03-14 15:15:58 UTC
> moment.localtime
=> 2011-03-14 08:15:58 -0700
Update: If you want to conver to specific time zones rather than your own timezone, you're on the right track. However, instead of worrying about EST vs EDT, just pass in the general Eastern Time zone -- it will know based on the day whether it is EDT or EST:
更新:如果你想要转换到特定的时区而不是你自己的时区,那么你是在正确的轨道上。然而,不用担心EST和EDT,只要通过东部时区就行了——它会根据当天的情况知道是EDT还是EST:
> Time.now.utc.in_time_zone("Eastern Time (US & Canada)")
=> Mon, 14 Mar 2011 11:21:05 EDT -04:00
> (Time.now.utc + 10.months).in_time_zone("Eastern Time (US & Canada)")
=> Sat, 14 Jan 2012 10:21:18 EST -05:00
#3
15
There is actually a nice Gem called local_time
by basecamp to do all of that on client side only, I believe:
实际上basecamp有一个很好的Gem叫做local_time,它只在客户端执行所有这些操作,我相信:
https://github.com/basecamp/local_time
https://github.com/basecamp/local_time
#4
8
It is easy to configure it using your system local zone, Just in your application.rb add this
仅在应用程序中,就可以使用系统本地区域对其进行配置。rb添加这个
config.time_zone = Time.now.zone
Then, rails should show you timestamps in your localtime or you can use something like this instruction to get the localtime
然后,rails应该在您的localtime中显示时间戳,或者您可以使用类似的指令来获取localtime
Post.created_at.localtime
#5
0
Don't know why but in my case it doesn't work the way suggested earlier. But it works like this:
我不知道为什么,但在我的情况下,它不像之前建议的那样。但它是这样运作的:
Time.now.change(offset: "-3000")
Of course you need to change offset
value to yours.
当然,您需要将偏移值更改为您的。
#1
100
Rails has its own names. See them with:
Rails有自己的名字。看到他们:
rake time:zones:us
You can also run rake time:zones:all
for all time zones. To see more zone-related rake tasks: rake -D time
您还可以运行rake time:zone:all for all time zone。要查看更多与区域相关的rake任务:rake -D time
So, to convert to EST, catering for DST automatically:
因此,要转换为EST,自动迎合DST:
Time.now.in_time_zone("Eastern Time (US & Canada)")
#2
105
Time#localtime
will give you the time in the current time zone of the machine running the code:
时间#localtime将给您在运行代码的机器当前时区中的时间:
> moment = Time.now.utc
=> 2011-03-14 15:15:58 UTC
> moment.localtime
=> 2011-03-14 08:15:58 -0700
Update: If you want to conver to specific time zones rather than your own timezone, you're on the right track. However, instead of worrying about EST vs EDT, just pass in the general Eastern Time zone -- it will know based on the day whether it is EDT or EST:
更新:如果你想要转换到特定的时区而不是你自己的时区,那么你是在正确的轨道上。然而,不用担心EST和EDT,只要通过东部时区就行了——它会根据当天的情况知道是EDT还是EST:
> Time.now.utc.in_time_zone("Eastern Time (US & Canada)")
=> Mon, 14 Mar 2011 11:21:05 EDT -04:00
> (Time.now.utc + 10.months).in_time_zone("Eastern Time (US & Canada)")
=> Sat, 14 Jan 2012 10:21:18 EST -05:00
#3
15
There is actually a nice Gem called local_time
by basecamp to do all of that on client side only, I believe:
实际上basecamp有一个很好的Gem叫做local_time,它只在客户端执行所有这些操作,我相信:
https://github.com/basecamp/local_time
https://github.com/basecamp/local_time
#4
8
It is easy to configure it using your system local zone, Just in your application.rb add this
仅在应用程序中,就可以使用系统本地区域对其进行配置。rb添加这个
config.time_zone = Time.now.zone
Then, rails should show you timestamps in your localtime or you can use something like this instruction to get the localtime
然后,rails应该在您的localtime中显示时间戳,或者您可以使用类似的指令来获取localtime
Post.created_at.localtime
#5
0
Don't know why but in my case it doesn't work the way suggested earlier. But it works like this:
我不知道为什么,但在我的情况下,它不像之前建议的那样。但它是这样运作的:
Time.now.change(offset: "-3000")
Of course you need to change offset
value to yours.
当然,您需要将偏移值更改为您的。