Ruby-on-rails 在 Rails 3 中将 UTC 转换为本地时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5300493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert UTC to local time in Rails 3
提问by Marc-André Lafortune
I'm having trouble converting a UTC Timeor TimeWithZoneto local time in Rails 3.
我在 Rails 3中无法转换 UTCTime或TimeWithZone本地时间。
Say momentis some Timevariable in UTC (e.g. moment = Time.now.utc). How do I convert momentto my time zone, taking care of DST (i.e. using EST/EDT)?
说moment是TimeUTC 中的一些变量(例如moment = Time.now.utc)。如何转换moment到我的时区,注意 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.
更准确地说,如果时间对应于美国东部时间今天上午 9 点和“3 月 7 日星期一上午 9 点”,如果时间是美国东部时间上周一上午 9 点,我想打印输出“3 月 14 日星期一上午 9 点”。
Hopefully there's another way?
希望有另一种方式?
Edit: I first thoughtthat "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" isan 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) 一年的地方使用。
回答by Dylan Markow
Time#localtimewill give you the time in the current time zone of the machine running the code:
Time#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
回答by Zabba
Rails has its own names. See them with:
Rails 有自己的名字。通过以下方式查看它们:
rake time:zones:us
You can also run rake time:zones:allfor all time zones.
To see more zone-related rake tasks: rake -D time
您还可以rake time:zones:all针对所有时区跑步。要查看更多与区域相关的 rake 任务:rake -D time
So, to convert to EST, catering for DST automatically:
因此,要转换为 EST,自动满足 DST:
Time.now.in_time_zone("Eastern Time (US & Canada)")
回答by jacr1614
It is easy to configure it using your system local zone, Just in your application.rb add this
使用您的系统本地区域很容易配置它,只需在您的 application.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 应该在您的本地时间显示时间戳,或者您可以使用类似此指令的内容来获取本地时间
Post.created_at.localtime
回答by mahatmanich
There is actually a nice Gem called local_timeby basecamp to do all of that on client side only, I believe:
实际上有一个local_time由 basecamp调用的不错的 Gem只在客户端完成所有这些,我相信:
回答by Seybo Glaux
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 offsetvalue to yours.
当然,您需要将offset值更改为您的值。
回答by therealadrain
If you're actually doing it just because you want to get the user's timezone then all you have to do is change your timezone in you config/applications.rb.
如果您实际上只是因为想要获取用户的时区而这样做,那么您所要做的就是更改您的时区config/applications.rb。
Like this:
像这样:
Rails, by default, will save your time record in UTC even if you specify the current timezone.
默认情况下,即使您指定了当前时区,Rails 也会以 UTC 格式保存您的时间记录。
config.time_zone = "Singapore"
So this is all you have to do and you're good to go.
所以这就是你所要做的一切,你可以开始了。

