Search code examples
rubyutcgmt

Output time with custom format (GMT instead UTC)


I'm wondering how to get time in exactly this format using ruby 1.9.2: I'm using rails 3.1 may be need to setup some environment constants to have it out of box with to_s method?

2011-11-11 15:20:01 GMT+4

instead

Time.now.to_s # => "2011-11-11 15:20:01 +0400"

Solution

  • class Time
      def to_gmt
        z = gmtoff / 3600
        strftime("%F %T GMT#{:+ if z>=0}#{z}")
      end
    end
    
    puts Time.now.to_gmt
    #=> 2011-11-11 06:43:59 GMT-7