Devon Quick Devon Quick - 4 months ago 31
Ruby Question

timezone id to short timezone (pst, pdt, cst, etc)

I use Googles Timezone API to get TimeZoneID and I pass it to the ruby gem TZInfo to get the local time. How can I get the Timezones short name (PST, PDT, EST, etc...) from this info. I tried

strftime(%Z)
but that returns 'UTC'. This is a ruby on rails app.

Answer

TZInfo::Timezone#strftime ought to work:

require "tzinfo"

tz = TZInfo::Timezone.get("America/New_York")
puts tz.strftime("%Z")

If you tried that and it didn't work, that's mysterious.

Alternatively, you can use TZInfo::TimezonePeriod#abbreviation:

tz = TZInfo::Timezone.get('America/New_York')
period = tz.period_for_utc(Time.now.utc)
puts period.abbreviation
Comments