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

时间:2021-02-25 13:34:19

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 not be 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.new method 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以外的东西吗?

5 个解决方案

#1


21  

You can use Time.zone.local if you set Time.zone previously:

如果先前设置了Time.zone,则可以使用Time.zone.local:

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::TimeWithZone documentation.

看看ActiveSupport :: TimeWithZone文档。

#2


28  

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

#3


13  

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

您可以使用以下代码使用所需的TimeZone创建DateTime对象。

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

UPDATE: With new 1.9.1 version of Rails Ruby, the below line does the same job like a magic.

更新:使用新的1.9.1版本的Rails Ruby,下面的代码就像魔术一样完成同样的工作。

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

Documentation here

文档在这里

#4


1  

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

#5


0  

DateTime.new accepts optional offset argument (as the seventh).

DateTime.new接受可选的offset参数(作为第七个)。

We can write

我们可以写

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

#1


21  

You can use Time.zone.local if you set Time.zone previously:

如果先前设置了Time.zone,则可以使用Time.zone.local:

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::TimeWithZone documentation.

看看ActiveSupport :: TimeWithZone文档。

#2


28  

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

#3


13  

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

您可以使用以下代码使用所需的TimeZone创建DateTime对象。

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

UPDATE: With new 1.9.1 version of Rails Ruby, the below line does the same job like a magic.

更新:使用新的1.9.1版本的Rails Ruby,下面的代码就像魔术一样完成同样的工作。

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

Documentation here

文档在这里

#4


1  

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

#5


0  

DateTime.new accepts optional offset argument (as the seventh).

DateTime.new接受可选的offset参数(作为第七个)。

We can write

我们可以写

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