如何将 unix 时间戳(自纪元以来的秒数)转换为 Ruby DateTime?
如何将 Unix 时间戳(自纪元以来的秒数)转换为 Ruby DateTime?
How do you convert a Unix timestamp (seconds since epoch) to Ruby DateTime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
抱歉,短暂的突触故障。这是真正的答案。
简短示例(考虑到当前系统时区):
进一步更新(针对 UTC):
最近更新:我对以下中的顶级解决方案进行了基准测试此线程在一两周前处理 HA 服务时,惊讶地发现
Time.at(..)
优于DateTime.strptime(..)
(更新:添加了更多基准)。Sorry, brief moment of synapse failure. Here's the real answer.
Brief example (this takes into account the current system timezone):
Further update (for UTC):
Recent Update: I benchmarked the top solutions in this thread while working on a HA service a week or two ago, and was surprised to find that
Time.at(..)
outperformsDateTime.strptime(..)
(update: added more benchmarks).DateTime.strptime
可以处理自纪元以来的秒数。该数字必须转换为字符串:DateTime.strptime
can handle seconds since epoch. The number must be converted to a string:时区处理
我只是想澄清一下,尽管这已经被评论了,所以未来的人们不会错过这个非常重要的区别。
显示 UTC 格式的返回值,要求秒数为字符串并输出 UTC Time 对象,而
显示 LOCAL 时区的返回值,通常需要 FixNum 参数,但 Time 对象本身仍然是 UTC 格式,即使显示不是。
因此,即使我将相同的整数传递给这两个方法,由于类的
#to_s
方法的工作方式,我似乎得到了两个不同的结果。然而,正如 @Eero 不得不提醒我两次:两个返回值之间的相等比较仍然返回 true。同样,这是因为这些值基本上是相同的(尽管不同的类,
#==
方法会为您处理这个问题),但是#to_s
方法的打印效果显着不同的字符串。不过,如果我们查看字符串,我们可以看到它们确实是同一时间,只是打印在不同的时区。方法参数说明
文档还说“如果给出数字参数,则结果为当地时间”。这是有道理的,但对我来说有点困惑,因为他们没有在文档中给出任何非整数参数的示例。因此,对于一些非整数参数示例:
您不能使用字符串参数,但可以在
Time.at
中使用时间参数,它将返回参数所在时区的结果:基准
在与 @AdamEberlin 讨论他的答案后,我决定发布稍微改变的基准,以使一切尽可能平等。另外,我不想再次构建这些,所以这是保存它们的好地方。
Time.at(int).to_datetime ~ 快 2.8 倍
****经过编辑,在各方面都不是完全错误的****
****添加基准****
Time Zone Handling
I just want to clarify, even though this has been commented so future people don't miss this very important distinction.
displays a return value in UTC and requires the seconds to be a String and outputs a UTC Time object, whereas
displays a return value in the LOCAL time zone, normally requires a FixNum argument, but the Time object itself is still in UTC even though the display is not.
So even though I passed the same integer to both methods, I seemingly two different results because of how the class'
#to_s
method works. However, as @Eero had to remind me twice of:An equality comparison between the two return values still returns true. Again, this is because the values are basically the same (although different classes, the
#==
method takes care of this for you), but the#to_s
method prints drastically different strings. Although, if we look at the strings, we can see they are indeed the same time, just printed in different time zones.Method Argument Clarification
The docs also say "If a numeric argument is given, the result is in local time." which makes sense, but was a little confusing to me because they don't give any examples of non-integer arguments in the docs. So, for some non-integer argument examples:
you can't use a String argument, but you can use a Time argument into
Time.at
and it will return the result in the time zone of the argument:Benchmarks
After a discussion with @AdamEberlin on his answer, I decided to publish slightly changed benchmarks to make everything as equal as possible. Also, I never want to have to build these again so this is as good a place as any to save them.
Time.at(int).to_datetime ~ 2.8x faster
****edited to not be completely and totally incorrect in every way****
****added benchmarks****
一条命令将日期时间转换为 Unix 格式,然后转换为字符串,
最后使用 strftime 来格式化日期
示例:
One command to convert date time to Unix format and then to string
finally strftime is used to format date
Example:
如果您只想要一个日期,则可以执行
Date.strptime(invoice.date.to_s, '%s')
,其中invoice.date
的形式为< code>Fixnum 然后转换为String
。If you wanted just a Date, you can do
Date.strptime(invoice.date.to_s, '%s')
whereinvoice.date
comes in the form of anFixnum
and then converted to aString
.这告诉您从执行代码那一刻起未来的秒数。
put(time)
根据当前时间我正在回答它打印的问题
047-05-14 05:16:16 +0000
(未来 10 亿秒)或者如果你想计算 10 亿秒从特定时间开始,其格式为
Time.mktime(year,month,date,hours,mins)
puts("I would be 10亿秒老:"+time)
This tells you the date of the number of seconds in future from the moment you execute the code.
puts(time)
according to the current time I am answering the question it prints
047-05-14 05:16:16 +0000
(1 billion seconds in future)or if you want to count billion seconds from a particular time, it's in format
Time.mktime(year, month,date,hours,minutes)
puts("I would be 1 Billion seconds old on: "+time)