I like to use strftime() to format the output time in various ways, each
of
which needs to have the time zone appended if it differs from the
current
user’s timezone. However, using %Z in a strftime() call failed to do
what I
expected:
TzTime.zone = TimeZone[‘Central Time (US & Canada)’]
=> #<TzinfoTimezone:0x9883228 @name=“Central Time (US & Canada)”,
@tzinfo=#<TZInfo::DataTimezone: America/Chicago>, @utc_offset=-21600>
TzTime.at(Time.now)
=> 2007-10-08 21:14:20 CDT
TzTime.at(TzTime.now).strftime("%H:%M:%S %Z")
=> “16:16:23 UTC”
Hmm, well, that sucks. So, I wrote a quick little hack for
vendor/plugins/tztime/lib/tz_time.rb:
def strftime(format)
format.gsub!(/%Z/, period.abbreviation.to_s)
# XXXMLG should handle %z here too, of course, but…
time.strftime(format)
end
Now, this should be done better, but oh well. It works:
TzTime.at(TzTime.now).strftime("%H:%M:%S %Z")
=> “16:18:48 CDT”
Some comments on the tzinfo library that sort of tweaked me a bit:
(1) I dislike the concept of a “default timezone” that is used
per-user.
If the whole system wants a default one, that’s great, but setting it
using
TzTime.zone= seems like a hack. Additionally, #at, and all the others,
should probably take an optional time zone that can be used to set the
time
zone from the user’s values. After all, when sending 100 mail messages,
I’m
not certain it’s thread-safe to set it globally…
(2) It’s unfortunate it doesn’t play well with rails 1.2.3, but I can
cope
and just put in a temporary hack.
–Michael