我想知道如何使用 ruby 1.9.2 以这种格式获取时间:我正在使用 rails 3.1 可能需要设置一些环境常量才能使用 to_s 方法使其开箱即用?
2011-11-11 15:20:01 GMT+4
反而
Time.now.to_s # => "2011-11-11 15:20:01 +0400"
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
Time.now.strftime("%D %T GMT%z")\
.gsub(/GMT([-\+]\d\d\d\d)/){"GMT#{sprintf("%+d",$1.to_i/100)}"}