在 Ruby 中将 UTC 时间戳转换为 ISO 8601

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

Converting UTC timestamp to ISO 8601 in Ruby

ruby-on-railsrubytimestamp

提问by ming yeow

I have a timestamp that is in UTC

我有一个 UTC 时间戳

"2010-10-25 23:48:46 UTC"

“2010-10-25 23:48:46 UTC”

I need to convert it into ISO 8601

我需要将其转换为 ISO 8601

"2010-10-29 06:09Z"

“2010-10-29 06:09Z”

The documentation is confusing as hell - what is the easiest way to do that?

文档令人困惑 - 最简单的方法是什么?

回答by Ryan Bigg

I think you're trying to trick us.

我想你是想欺骗我们。

The input date to your question is the 25th of October, 2010, whilst the output is the 29thof October, 2010. Well played!

您问题的输入日期是2010 年 10 月 25 日,而输出日期是2010 年 10 月29。打的好!

Continuing on this nit-picking thread: your times are also completely different and you're missing the seconds from the output time.

继续这个挑剔的线程:你的时间也完全不同,你错过了输出时间的秒数。

Now for the true answer.

现在来看看真正的答案。

A little factoid first though: the ISO 8601 output in Ruby is similar to the "Combined date and time" output from ISO 8601's Wikipedia page.

不过首先要说明一点:Ruby 中的 ISO 8601 输出类似于ISO 8601 维基百科页面中的“组合日期和时间”输出。

You've got a string and so you'll need to convert it into a Timeobject which you can do with to_time. Then it's simply a matter of calling iso8601on that object to get the ISO 8601 version:

你有一个字符串,所以你需要将它转换成一个Time对象,你可以用to_time. 然后只需调用iso8601该对象即可获取 ISO 8601 版本:

"2010-10-25 23:48:46 UTC".to_time.iso8601

The to_timemethod is courtesy of Rails, whilst the iso8601is courtesy of Ruby's standard library.

to_time方法由 Rails 提供,而iso8601Ruby 标准库提供。

回答by Steeve McCauley

After much experimenting, I find the Time library's parser to be better than DateTime, although the reasons escape me at the moment. With that caveat, I always use Time rather than DateTime for this kind of stuff, and the ruby documentation is also difficult to grok as to why this is so,

经过多次试验,我发现 Time 库的解析器比 DateTime 更好,尽管目前我不知道原因。有了这个警告,我总是使用 Time 而不是 DateTime 来处理这种事情,而且 ruby​​ 文档也很难理解为什么会这样,

require 'time'
puts Time.parse("2010-10-25 23:48:46 UTC").iso8601
"2010-10-25T23:48:46Z"

回答by Aerodame

Note: you have to convert (parse) a time string into a time object before you can apply the to_time method.

注意:您必须先将时间字符串转换(解析)为时间对象,然后才能应用 to_time 方法。

ruby-1.9.2-p180 :016 > "2010-10-25 23:48:46 UTC".to_time.iso8601
NoMethodError: undefined method `to_time' for "2010-10-25 23:48:46 UTC":String
    from (irb):16

Correct procedure:

正确的程序:

irb> ut = DateTime.parse("2010-10-25 23:48:46 UTC")

irb> ut.iso8601
 => "2010-10-25T23:48:46+00:00" 

回答by Allison

Adding an answer to this super old question because if you're using Rails, there is no need to convert/parse it in the way the other answers here are telling you to do:

为这个超级老问题添加一个答案,因为如果您使用 Rails,则无需按照此处的其他答案告诉您的方式来转换/解析它:

  • If it's already a timestamp (e.g., a created_ator updated_atattribute), you can directlycall the iso8601method on that timestamp (e.g., object.created_at.iso8601).
  • The iso8601method also accepts a numeric argument to display fractional digits; you would use this if you want 2020-04-06T19:16:55.604Zinstead of 2020-04-06T19:16:55Z.
  • 如果它已经是一个时间戳(例如,acreated_atupdated_at属性),您可以直接调用该iso8601时间戳上的方法(例如,object.created_at.iso8601)。
  • iso8601方法还接受一个数字参数来显示小数位数;如果您愿意2020-04-06T19:16:55.604Z,您可以使用它而不是2020-04-06T19:16:55Z.

https://api.rubyonrails.org/v5.2.4/classes/ActiveSupport/TimeWithZone.html#method-i-iso8601

https://api.rubyonrails.org/v5.2.4/classes/ActiveSupport/TimeWithZone.html#method-i-iso8601