Ruby-on-rails 在 Rails 中将时间从一个时区转换为另一个时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1386871/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:43:16  来源:igfitidea点击:

Convert Time from one time zone to another in Rails

ruby-on-railsrubydatetime

提问by Tom Lehman

My created_attimestamps are stored in UTC:

我的created_at时间戳以 UTC 格式存储:

>> Annotation.last.created_at
=> Sat, 29 Aug 2009 23:30:09 UTC +00:00

How do I convert one of them to 'Eastern Time (US & Canada)' (taking into account daylight savings)? Something like:

如何将其中之一转换为“东部时间(美国和加拿大)”(考虑到夏令时)?就像是:

Annotation.last.created_at.in_eastern_time

回答by Steve Weet

Use the in_time_zone method of the DateTime class

使用 DateTime 类的 in_time_zone 方法

Loading development environment (Rails 2.3.2)
>> now = DateTime.now.utc
=> Sun, 06 Sep 2009 22:27:45 +0000
>> now.in_time_zone('Eastern Time (US & Canada)')
=> Sun, 06 Sep 2009 18:27:45 EDT -04:00
>> quit

So for your particular example

所以对于你的特定例子

Annotation.last.created_at.in_time_zone('Eastern Time (US & Canada)')

回答by opsidao

Although this is an old question, it's worth mentioning something. In a previous replyit's suggested to use a before_filter to set the timezone temporally.

虽然这是一个老问题,但还是值得一提。在之前的回复中,建议使用 before_filter 临时设置时区。

You should never, everdo that because Time.zone stores the information in the thread, and it will probably leak to the next request handled by that thread.

永远不应该这样做,因为 Time.zone 将信息存储在线程中,并且它可能会泄漏到该线程处理的下一个请求。

Instead you should use an around_filter to make sure that the Time.zone is reset after the request is complete. Something like:

相反,您应该使用 around_filter 来确保在请求完成后重置 Time.zone。就像是:

around_filter :set_time_zone

private

def set_time_zone
  old_time_zone = Time.zone
  Time.zone = current_user.time_zone if logged_in?
  yield
ensure
  Time.zone = old_time_zone
end

Read more about this here

在此处阅读更多相关信息

回答by Eifion

If you add this to your /config/application.rb

如果您将此添加到您的 /config/application.rb

config.time_zone = 'Eastern Time (US & Canada)'

Then you can cell

然后你可以单元格

Annotation.last.created_at.in_time_zone

to get the time in the specified time zone.

获取指定时区的时间。

回答by Dorian

If you configure your /config/application.rb

如果你配置你的 /config/application.rb

config.time_zone = 'Eastern Time (US & Canada)'

Time.now.in_time_zone

DateTime.now.in_time_zone

回答by nitecoder

Set your timezone to Eastern Time.

将您的时区设置为东部时间。

You can set your default timezone in config/environment.rb

您可以在 config/environment.rb 中设置默认时区

config.time_zone = "Eastern Time (US & Canada)"

Now all records you pull out will be in that time zone. If you need different time zones, say based on a user timezone you can change it with a before_filter in your controller.

现在,您提取的所有记录都将位于该时区。如果您需要不同的时区,例如基于用户时区,您可以使用控制器中的 before_filter 更改它。

class ApplicationController < ActionController::Base

  before_filter :set_timezone

  def set_timezone
    Time.zone = current_user.time_zone
  end
end

Just make sure you are storing all your times in the database as UTC and everything will be sweet.

只要确保您将所有时间都以UTC格式存储在数据库中,一切都会很好。