# TzTime buglet

**URL:** <https://www.ruby-forum.com/t/tztime-buglet/116718>\
**Category:** Rails\
**Created:** [October 8, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/tztime-buglet/116718 "2007-10-08T23:38:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [October 8, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/tztime-buglet/116718/1 "2007-10-08T23:38:14Z")

</div>

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
