在 Rails 3 中将 UTC 转换为本地时间

发布于 2024-10-21 21:11:43 字数 656 浏览 1 评论 0原文

我在 Rails 3 中将 UTC TimeTimeWithZone 转换为本地时间时遇到问题。

假设 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

若沐 2024-10-28 21:11:43

Time#localtime 将为您提供运行代码的计算机当前时区的时间:

> moment = Time.now.utc
  => 2011-03-14 15:15:58 UTC 
> moment.localtime
  => 2011-03-14 08:15:58 -0700 

更新:如果您想转换为特定时区而不是您自己的时区,那么您就在右边追踪。然而,不必担心 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 

Time#localtime will give you the time in the current time zone of the machine running the code:

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

> 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 
缱绻入梦 2024-10-28 21:11:43

Rails 有自己的名字。查看它们:

rake time:zones:us

您还可以针对所有时区运行 rake time:zones:all
要查看更多与区域相关的 rake 任务:rake -D time

因此,要转换为 EST,自动适应 DST:

Time.now.in_time_zone("Eastern Time (US & Canada)")

Rails has its own names. See them with:

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

So, to convert to EST, catering for DST automatically:

Time.now.in_time_zone("Eastern Time (US & Canada)")
月隐月明月朦胧 2024-10-28 21:11:43

使用系统本地区域很容易配置它,
只需在您的 application.rb 添加此

config.time_zone = Time.now.zone

然后,rails 应该显示您本地时间的时间戳,或者您可以使用类似此指令的内容来获取本地时间

Post.created_at.localtime

It is easy to configure it using your system local zone,
Just in your application.rb add this

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

Post.created_at.localtime
玻璃人 2024-10-28 21:11:43

实际上,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

请帮我爱他 2024-10-28 21:11:43

不知道为什么,但就我而言,它不能按照之前建议的方式工作。
但它的工作原理是这样的:

Time.now.change(offset: "-3000")

当然,您需要将 offset 值更改为您的值。

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.

饮惑 2024-10-28 21:11:43

如果您实际上这样做只是因为您想获取用户的时区,那么您所要做的就是更改 config/applications.rb 中的时区。

就像这样:

默认情况下,即使您指定了当前时区,Rails 也会以 UTC 格式保存您的时间记录。

config.time_zone = "Singapore"

因此,这就是您所要做的全部事情,然后就可以开始了。

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.

config.time_zone = "Singapore"

So this is all you have to do and you're good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文