Ruby/Rails:将日期转换为 UNIX 时间戳

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

Ruby/Rails: converting a Date to a UNIX timestamp

ruby-on-railsrubydatetimestamp

提问by igul222

How would I get a UNIX timestamp (number of seconds since 1970 GMT) from a Date object in a Rails app?

如何从 Rails 应用程序中的 Date 对象获取 UNIX 时间戳(自格林威治标准时间 1970 以来的秒数)?

I know Time#to_ireturns a timestamp, but doing Date#to_timeand then getting the timestamp results in something that's off by about a month (not sure why...).

我知道Time#to_i返回一个时间戳,但是这样做Date#to_time然后获取时间戳会导致大约一个月的时间(不知道为什么......)。

Any help is appreciated, thanks!

任何帮助表示赞赏,谢谢!

Edit: OK, I think I figured it out- I was processing a date several times in a loop, and each time the date was moved a little because of a time zone mismatch, ultimately leading to my timestamp being a month off. Still, I'd be interested in knowing if there's any way to do this without relying on Date#to_time.

编辑:好的,我想我想通了 - 我在循环中多次处理日期,并且每次由于时区不匹配而稍微移动日期,最终导致我的时间戳关闭一个月。不过,我很想知道是否有任何方法可以在不依赖Date#to_time.

回答by David Grayson

The code date.to_time.to_ishould work fine. The Rails console session below shows an example:

代码date.to_time.to_i应该可以正常工作。下面的 Rails 控制台会话显示了一个示例:

>> Date.new(2009,11,26).to_time
=> Thu Nov 26 00:00:00 -0800 2009
>> Date.new(2009,11,26).to_time.to_i
=> 1259222400
>> Time.at(1259222400)
=> Thu Nov 26 00:00:00 -0800 2009

Note that the intermediate DateTime object is in local time, so the timestamp might be a several hours off from what you expect. If you want to work in UTC time, you can use the DateTime's method "to_utc".

请注意,中间 DateTime 对象是本地时间,因此时间戳可能与您期望的时间相差几个小时。如果您想在 UTC 时间工作,您可以使用 DateTime 的方法“to_utc”。

回答by Ryan Bigg

I get the following when I try it:

当我尝试时,我得到以下信息:

>> Date.today.to_time.to_i
=> 1259244000
>> Time.now.to_i
=> 1259275709

The difference between these two numbers is due to the fact that Datedoes not store the hours, minutes or seconds of the current time. Converting a Dateto a Timewill result in that day, midnight.

这两个数字之间的差异是由于Date不存储当前时间的小时、分钟或秒的事实。将 a 转换Date为 aTime将导致当天午夜。

回答by Gerry

The suggested options of using to_utcor utcto fix the local time offset does not work. For me I found using Time.utc()worked correctly and the code involves less steps:

使用to_utcutc修复本地时间偏移的建议选项不起作用。对我来说,我发现 usingTime.utc()工作正常,代码涉及的步骤更少:

> Time.utc(2016, 12, 25).to_i
=> 1482624000 # correct

vs

对比

> Date.new(2016, 12, 25).to_time.utc.to_i
=> 1482584400 # incorrect


Here is what happens when you call utc after using Date....

这是在使用Date....后调用 utc 时发生的情况。

> Date.new(2016, 12, 25).to_time
=> 2016-12-25 00:00:00 +1100 # This will use your system's time offset
> Date.new(2016, 12, 25).to_time.utc
=> 2016-12-24 13:00:00 UTC

...so clearly calling to_iis going to give the wrong timestamp.

...所以很明显调用to_i会给出错误的时间戳。

回答by Nowaker

Solution for Ruby 1.8 when you have an arbitrary DateTime object:

当您拥有任意 DateTime 对象时,Ruby 1.8 的解决方案:

1.8.7-p374 :001 > require 'date'
 => true 
1.8.7-p374 :002 > DateTime.new(2012, 1, 15).strftime('%s')
 => "1326585600"

回答by wang sky

DateTime.new(2012, 1, 15).to_time.to_i