3

I need to generate an HTTP (GMT) date string in Ruby. This is because of a requirement of an API that I'm consuming.

What is an easy (out of the box) way to generate it?

Tonatiuh
  • 2,205
  • 1
  • 21
  • 22

2 Answers2

7

I found that Ruby comes with a method for the Time class out of the box for this:

Time.now.httpdate # => "Thu, 06 Oct 2011 02:26:12 GMT"

The time class also supports the following methods

Time.now.iso8601  # => "2011-10-05T22:26:12-04:00"
Time.now.rfc2822  # => "Wed, 05 Oct 2011 22:26:12 -0400"

Source: http://ruby-doc.org/stdlib-2.0.0/libdoc/time/rdoc/Time.html#class-Time-label-Converting+to+a+String

Tonatiuh
  • 2,205
  • 1
  • 21
  • 22
0

does Time.now.getgm work for you?

Time.now.gmt? #=> fale
Time.now.getgm.gmt? #=> true
Voski
  • 139
  • 2
  • 4
  • 1
    Thanks. But it doesn't, I needed the GMT string as "Thu, 06 Oct 2011 02:26:12 GMT" and what the "getgm" method returns is a Time not a string. "Time.now.httpdate" did the job. – Tonatiuh Mar 31 '17 at 23:33