在Rails中将时间从一个时区转换到另一个时区

时间:2022-01-29 23:31:48

My created_at timestamps 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

5 个解决方案

#1


164  

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)')

#2


15  

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

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

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

你永远都不应该那样做,因为时间。区域将信息存储在线程中,它可能会泄漏到该线程处理的下一个请求。

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来确保时间。请求完成后重新设置区域。喜欢的东西:

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

请在这里阅读更多

#3


8  

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.

获取指定时区中的时间。

#4


2  

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一样,一切都会很好。

#5


2  

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

#1


164  

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)')

#2


15  

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

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

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

你永远都不应该那样做,因为时间。区域将信息存储在线程中,它可能会泄漏到该线程处理的下一个请求。

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来确保时间。请求完成后重新设置区域。喜欢的东西:

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

请在这里阅读更多

#3


8  

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.

获取指定时区中的时间。

#4


2  

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一样,一切都会很好。

#5


2  

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