Ruby-on-rails 如何在特定时区(最好是我的应用程序的默认时区,而不是 UTC)中创建新的 DateTime 对象?

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

How to create a new DateTime object in a specific time zone (preferably the default time zone of my app, not UTC)?

ruby-on-railsdatetimeruby-on-rails-3.1timezone

提问by user664833

I have set the time zone in /config/application.rb, and I expect all times generated in my app to be in this time zone by default, yet when I create a new DateTimeobject (using .new), it creates it in GMT. How can I get it to be in my app's time zone?

我已经在 中设置了时区/config/application.rb,并且我希望我的应用程序中生成的所有时间默认都在这个时区中,但是当我创建一个新DateTime对象(使用.new)时,它会在GMT. 我怎样才能让它在我的应用程序的时区?

/config/application.rb

/配置/应用程序.rb

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

irb

irb

irb> DateTime.now
=> Wed, 11 Jul 2012 19:04:56 -0700 

irb> mydate = DateTime.new(2012, 07, 11, 20, 10, 0)
=> Wed, 11 Jul 2012 20:10:00 +0000                    # GMT, but I want PDT

Using in_time_zonedoesn't work because that just converts the GMT time to PDT time, which is the wrong time:

使用in_time_zone不起作用,因为这只是将 GMT 时间转换为 PDT 时间,这是错误的时间:

irb> mydate.in_time_zone('Pacific Time (US & Canada)')
=> Wed, 11 Jul 2012 13:10:00 PDT -07:00               # wrong time (I want 20:10)

采纳答案by Peter Brown

You can use ActiveSupport's TimeWithZone(Time.zone) object to create and parse dates in the time zone of your application:

您可以使用 ActiveSupport 的TimeWithZone( Time.zone) 对象在您的应用程序的时区中创建和解析日期:

1.9.3p0 :001 > Time.zone.now
 => Wed, 11 Jul 2012 19:47:03 PDT -07:00 
1.9.3p0 :002 > Time.zone.parse('2012-07-11 21:00')
 => Wed, 11 Jul 2012 21:00:00 PDT -07:00 

回答by hangsu

Another way without string parsing:

另一种没有字符串解析的方法:

irb> Time.zone.local(2012, 7, 11, 21)
=> Wed, 07 Nov 2012 21:00:00 PDT -07:00

回答by ewH

If I have it, I usually just specify the utc_offset when instantiating Time.new or DateTime.new.

如果我有它,我通常只在实例化 Time.new 或 DateTime.new 时指定 utc_offset。

[1] pry(main)> Time.new(2013,01,06, 11, 25, 00) #no specified utc_offset defaults to system time
 => 2013-01-06 11:25:00 -0500
[2] pry(main)> Time.new(2013,01,06, 11, 25, 00, "+00:00") #UTC
 => 2013-01-06 11:25:00 +0000
[3] pry(main)> Time.new(2013,01,06, 11, 25, 00, "-08:00") #PST
 => 2013-01-06 11:25:00 -0800 

回答by JustJonG

This can be achieved in the DateTime class as well by including the timezone.

这也可以通过包含时区在 DateTime 类中实现。

2.5.1 :001 > require 'rails'
 => true
2.5.1 :002 > mydate = DateTime.new(2012, 07, 11, 20, 10, 0)
 => Wed, 11 Jul 2012 20:10:00 +0000
2.5.1 :003 > mydate = DateTime.new(2012, 07, 11, 20, 10, 0, "PST")
 => Wed, 11 Jul 2012 20:10:00 -0800

or

或者

https://docs.ruby-lang.org/en/2.6.0/DateTime.html

https://docs.ruby-lang.org/en/2.6.0/DateTime.html

2.6.0 :001 > DateTime.new(2012, 07, 11, 20, 10, 0, "-06")
 => Wed, 11 Jul 2012 20:10:00 -0600
2.6.0 :002 > DateTime.new(2012, 07, 11, 20, 10, 0, "-05")
 => Wed, 11 Jul 2012 20:10:00 -0500

回答by Michael Hagar

If you want to create a DateTime in a local timezone differentthan what is set as config.time_zone:

如果要在不同于设置的本地时区中创建 DateTime config.time_zone

la_date = ActiveSupport::TimeZone["America/Los_Angeles"].parse('2020-02-01')
DateTime.new(la_date.year, la_date.month, la_date.day, la_date.hour, la_date.min, la_date.sec, la_date.zone)

回答by drhenner

I do the following in ApplicationController to set the timezone to the user's time.

我在 ApplicationController 中执行以下操作以将时区设置为用户的时间。

I'm not sure if this is what you want.

我不确定这是否是你想要的。

class ApplicationController < ActionController::Base
  before_filter :set_timezone
  def set_timezone
    # current_user.time_zone #=> 'London'
    Time.zone = current_user.time_zone if current_user && current_user.time_zone
  end

end