converting Time class object to RFC3339 in Ruby

不羁岁月 提交于 2019-12-23 07:34:43

问题


Google Calendar API(v2)'s time-related query is required to be RFC3339-formatted. When I looked up Time class after 'require "time"', I could not see rfc3339 method.


回答1:


You'll need to convert the time to a DateTime object:

Time.now.to_datetime.rfc3339 #=> "2014-11-06T10:40:54+11:00" 

See:
http://www.ruby-doc.org/stdlib-2.4.1/libdoc/date/rdoc/Time.html




回答2:


Does this help? http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-i-rfc3339

DateTime.parse('2001-02-03T04:05:06.123456789+07:00').rfc3339(9)
                          #=> "2001-02-03T04:05:06.123456789+07:00"



回答3:


The way I chose to do this was Time.now.utc.strftime('%FT%TZ') #=> "2013-08-15T06:13:28Z" which is perfect for an HTML5 type='datetime' input field.




回答4:


A website mentioned that RFC3339 is most common date format in RSS feeds, so that the conversion method is implemented as #xmlschema, but not #rfc3339.



来源:https://stackoverflow.com/questions/13247288/converting-time-class-object-to-rfc3339-in-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!