在 Rails 3 中将 UTC 转换为本地时间
我在 Rails 3 中将 UTC Time
或 TimeWithZone
转换为本地时间时遇到问题。
假设 moment
是某个 Time
> UTC 变量(例如 moment = Time.now.utc
)。如何将时刻
转换为我的时区,同时考虑 DST(即使用 EST/EDT)?
更准确地说,如果时间对应于美国东部时间今天上午 9 点,我想打印“3 月 14 日星期一上午 9 点”;如果时间是美国东部时间上周一上午 9 点,我想打印“3 月 7 日星期一上午 9 点”。
希望还有其他方法吗?
编辑:我首先想到“EDT”应该是一个公认的时区,但“EDT”不是实际的时区,更像是时区的状态。例如,请求 Time.utc(2011,1,1).in_time_zone("EDT")
是没有任何意义的。这有点令人困惑,因为“EST”是一个实际时区,在一些不使用夏令时且全年 (UTC-5) 的地方使用。
I'm having trouble converting a UTC Time
or TimeWithZone
to local time in Rails 3.
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)?
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Time#localtime
将为您提供运行代码的计算机当前时区的时间:更新:如果您想转换为特定时区而不是您自己的时区,那么您就在右边追踪。然而,不必担心 EST 与 EDT,只需传入一般东部时区即可 - 它会根据日期知道是 EDT 还是 EST:
Time#localtime
will give you the time in the current time zone of the machine running the code: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:
Rails 有自己的名字。查看它们:
您还可以针对所有时区运行
rake time:zones:all
。要查看更多与区域相关的 rake 任务:
rake -D time
因此,要转换为 EST,自动适应 DST:
Rails has its own names. See them with:
You can also run
rake time:zones:all
for all time zones.To see more zone-related rake tasks:
rake -D time
So, to convert to EST, catering for DST automatically:
使用系统本地区域很容易配置它,
只需在您的 application.rb 添加此
然后,rails 应该显示您本地时间的时间戳,或者您可以使用类似此指令的内容来获取本地时间
It is easy to configure it using your system local zone,
Just in your application.rb add this
Then, rails should show you timestamps in your localtime or you can use something like this instruction to get the localtime
实际上,basecamp 有一个名为
local_time
的漂亮 Gem,仅在客户端执行所有这些操作,我相信:https://github.com/basecamp/local_time
There is actually a nice Gem called
local_time
by basecamp to do all of that on client side only, I believe:https://github.com/basecamp/local_time
不知道为什么,但就我而言,它不能按照之前建议的方式工作。
但它的工作原理是这样的:
当然,您需要将
offset
值更改为您的值。Don't know why but in my case it doesn't work the way suggested earlier.
But it works like this:
Of course you need to change
offset
value to yours.如果您实际上这样做只是因为您想获取用户的时区,那么您所要做的就是更改
config/applications.rb
中的时区。就像这样:
默认情况下,即使您指定了当前时区,Rails 也会以 UTC 格式保存您的时间记录。
因此,这就是您所要做的全部事情,然后就可以开始了。
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
.Like this:
Rails, by default, will save your time record in UTC even if you specify the current timezone.
So this is all you have to do and you're good to go.