如何从现有的 Time.zone for Rails 创建 Ruby DateTime?

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

How to create a Ruby DateTime from existing Time.zone for Rails?

ruby-on-railsrubydatetimeruby-on-rails-3.2timezone

提问by at.

I have users entering in dates in a Ruby on Rails website. I parse the dates into a DateTime object with something like:

我有用户在 Ruby on Rails 网站中输入日期。我将日期解析为 DateTime 对象,如下所示:

date = DateTime.new(params[:year].to_i, params[:month].to_i, params[:day].to_i, params[:hour].to_i, params[:minute].to_i)

or

或者

date = DateTime.parse(params[:date])

Both DateTimes will notbe in the time zone of the user which I previously set with something like:

两个DateTimes 都不会在我之前设置的用户的时区中,例如:

Time.zone = "Pacific Time (US & Canada)"

How do I parse the above DateTimes to be in the right time zone? I know the DateTime.newmethod has a 7th argument for the time offset. Is there an easy way to look up the offset for a time zone in a given time? Or should I be using something other than DateTime?

如何将上述DateTimes解析为正确的时区?我知道该DateTime.new方法具有时间偏移的第 7 个参数。有没有一种简单的方法可以在给定时间内查找时区的偏移量?或者我应该使用除DateTime?

采纳答案by at.

You can use Time.zone.localif you set Time.zonepreviously:

Time.zone.local如果您Time.zone之前设置,您可以使用:

user_time = Time.zone.local(params[:year].to_i, params[:month].to_i, params[:day].to_i, params[:hour].to_i, params[:minute].to_i, 0)

Have a look at the ActiveSupport::TimeWithZonedocumentation.

看看ActiveSupport::TimeWithZone文档。

回答by shweta

Try:

尝试:

Time.zone = "Pacific Time (US & Canada)"
Time.zone.parse('8-11-2013 23:59:59') #=> Fri, 08 Nov 2013 23:59:59 PST -08:00 

OR

或者

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

OR

或者

DateTime.now.in_time_zone("Pacific Time (US & Canada)")

回答by SriramK89

You can use the following code to create a DateTimeobject with your desired TimeZone.

您可以使用以下代码创建DateTime具有所需TimeZone.

DateTime.new(2013, 6, 29, 10, 15, 30).change(:offset => "+0530")

UPDATE: With new1.9.1 version of RailsRuby, the below line does the same job like a magic.

更新:使用新的1.9.1 版本的RailsRuby,下面这行就像魔法一样完成了同样的工作。

DateTime.new(2013, 6, 29, 10, 15, 30, "+0530")

Documentation here

文档在这里

回答by Tun

DateTime.newaccepts optional offsetargument (as the seventh) starting from ruby 1.9.1

DateTime.new从 ruby​​ 1.9.1 开始接受可选的偏移参数(作为第七个)

We can write

我们可以写

DateTime.new(2018, 1, 1, 0, 0, 0, Time.zone.formatted_offset)

回答by Adreamus

If you have already a correct DateTime object with the name 'datetime', and you want to copy it, you can simply call the 'getutc' on it and after that use the 'in_time_zone'

如果您已经有一个名为“datetime”的正确 DateTime 对象,并且您想复制它,您可以简单地调用它的“getutc”,然后使用“in_time_zone”

DateTime.new(datetime.getutc.year, datetime.getutc.month, datetime.getutc.day, time.getutc.hour, time.getutc.min).in_time_zone