I’m having an issue with Ruby 1.8.7 strftime where the %z is returning the local time after i convert the time to UTC.
I’m doing the following:
>> t = Time.now
=> Mon Dec 19 15:20:16 -0800 2011
>> t.strftime("%z")
=> "-0800"
>> t = Time.now.utc
=> Mon Dec 19 23:20:28 UTC 2011
>> t.strftime("%z")
=> "-0800"
Even after I change the time to UTC, the timezone formatted gets defaulted to my local PST -0800.
Is this a known issue? Is there a way around it?
Note that the fine 1.8.7 manual makes no mention of
%z:but the 1.9.3 version does have documented support for
%z:The fact the
%zproduces anything at all appears to be an undocumented and possibly accidental implementation detail.You can use
%Zin 1.8.7 and 1.9.3; for example, you get these results in 1.8.7:That will give you the timezone as UTC, PST, EDT, and similar common abbreviations. If you want the offset, you should be using
gmt_offsetin both 1.9.3 and 1.8.7:Note that
gmt_offsetgives you the offset in seconds.