wael34218
wael34218

Reputation: 4930

How to convert timezone name to shortname in ruby

Is there a way to convert timezones like "America/Los_Angeles" to timezone shortname "PDT" in ruby?

Upvotes: 2

Views: 2231

Answers (2)

d_rail
d_rail

Reputation: 4119

More explicit answer:

Time.now.in_time_zone("America/Los_Angeles").strftime('%Z')

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500805

The abbreviations are typically contextual - so "PDT" only applies while daylight saving time is in operation.

Judging by the documentation, if you format a Time with strftime and use a format string of %Z, you should get the time zone abbreviation.

Personally I dislike using the abbreviations, given that they're ambiguous and can cause a lot of confusion. (I've seen people using "PST" year-round, for example, referring to "7/20/2012 9:00 PST" for example - a date/time which makes no sense.) That's a different matter though :)

Upvotes: 3

Related Questions