Ruby's strftime not displaying timezone offset with '%Z'
Asked Answered
A

3

7

I have the following Time object:

[8] pry(#<#<Class:0x007f928f12f560>>)> display_num
=> 2015-02-19 09:00:00 -0600
[9] pry(#<#<Class:0x007f928f12f560>>)> display_num.is_a?(Time)
=> true
[10] pry(#<#<Class:0x007f928f12f560>>)> display_num.is_a?(DateTime)
=> false
[11] pry(#<#<Class:0x007f928f12f560>>)> display_num.strftime("%l:%M%P %Z")
=> " 9:00am "
[12] pry(#<#<Class:0x007f928f12f560>>)> display_num.strftime("%l:%M%P %z")
=> " 9:00am -0600"

I'm completely stumped as this:

[16] pry(#<#<Class:0x007f928f12f560>>)> Time.new
=> 2015-02-02 14:09:13 -0800
[17] pry(#<#<Class:0x007f928f12f560>>)> t = Time.new
=> 2015-02-02 14:09:25 -0800
[18] pry(#<#<Class:0x007f928f12f560>>)> t.strftime("%l:%M%P %Z")
=> " 2:09pm PST"

works just fine.

What is going on in the above block to prevent a timezone from displaying in human readable format?

Antimonyl answered 2/2, 2015 at 22:12 Comment(0)
G
6

The format directive %Z requests the symbolic time zone (name or abbreviation); %z is the offset. While you always know the offset, you may not know the symbolic time zone name.

I suspect that's what's going on with display_time. It was initialized with an offset, so it doesn't have any symbolic time zone name to display.

You can't reliably derive a name from an offset, either; for example, -0400 could be Atlantic Standard Time or Eastern Daylight Time. Most offsets will have more than one option for what time zone they are, and most time zones have more than one name anyway.

Gauthier answered 2/2, 2015 at 22:16 Comment(1)
That's just it, i am initializing with an offset. Doh!Antimonyl
A
7

Worth adding, if you're using Rails, ActiveSupport monkey-patches in the method in_time_zone that you can use to fix this issue with strftime:

Time.current.localtime("-06:00").strftime("TZ is: %Z") # => "TZ is: "
Time.current.in_time_zone("America/Chicago").strftime("TZ is: %Z") # => "TZ is: CDT"
Time.current.in_time_zone("America/New_York").strftime("TZ is: %Z") # => "TZ is: EDT"

References:

DateAndTime::Zones#in_time_zone

Full list of time zone strings is defined in ActiveSupport::TimeZone::MAPPING

Antons answered 18/6, 2018 at 20:28 Comment(0)
G
6

The format directive %Z requests the symbolic time zone (name or abbreviation); %z is the offset. While you always know the offset, you may not know the symbolic time zone name.

I suspect that's what's going on with display_time. It was initialized with an offset, so it doesn't have any symbolic time zone name to display.

You can't reliably derive a name from an offset, either; for example, -0400 could be Atlantic Standard Time or Eastern Daylight Time. Most offsets will have more than one option for what time zone they are, and most time zones have more than one name anyway.

Gauthier answered 2/2, 2015 at 22:16 Comment(1)
That's just it, i am initializing with an offset. Doh!Antimonyl
E
0

Use format %TZ if you want to get the format in RFC3339, that is, Z is used for UTC instead of +0000

Enterotomy answered 6/10 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.