【发布时间】:2017-08-26 07:46:50
【问题描述】:
我需要在 Ruby 中生成一个 HTTP (GMT) 日期字符串。这是因为我需要使用 API。
生成它的简单(开箱即用)方法是什么?
【问题讨论】:
我需要在 Ruby 中生成一个 HTTP (GMT) 日期字符串。这是因为我需要使用 API。
生成它的简单(开箱即用)方法是什么?
【问题讨论】:
我发现 Ruby 为 Time 类提供了一个开箱即用的方法:
Time.now.httpdate # => "Thu, 06 Oct 2011 02:26:12 GMT"
时间类还支持以下方法
Time.now.iso8601 # => "2011-10-05T22:26:12-04:00"
Time.now.rfc2822 # => "Wed, 05 Oct 2011 22:26:12 -0400"
来源:http://ruby-doc.org/stdlib-2.0.0/libdoc/time/rdoc/Time.html#class-Time-label-Converting+to+a+String
【讨论】:
require "time",否则这对我在 Ruby 2.6.5p114 上不起作用。
Time.now.getgm 适合你吗?
Time.now.gmt? #=> fale
Time.now.getgm.gmt? #=> true
【讨论】: