【问题标题】:Easy way to generate http date (GMT) string in ruby在 ruby​​ 中生成 http 日期 (GMT) 字符串的简单方法
【发布时间】:2017-08-26 07:46:50
【问题描述】:

我需要在 Ruby 中生成一个 HTTP (GMT) 日期字符串。这是因为我需要使用 API。

生成它的简单(开箱即用)方法是什么?

【问题讨论】:

    标签: ruby date gmt


    【解决方案1】:

    我发现 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 上不起作用。
    【解决方案2】:

    Time.now.getgm 适合你吗?

    Time.now.gmt? #=> fale
    Time.now.getgm.gmt? #=> true
    

    【讨论】:

    • 谢谢。但事实并非如此,我需要 GMT 字符串为“Thu, 06 Oct 2011 02:26:12 GMT”,而“getgm”方法返回的是时间而不是字符串。 “Time.now.httpdate”完成了这项工作。
    猜你喜欢
    • 2012-10-06
    • 2011-10-07
    • 2016-11-06
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多