Invalid result when formatting time zone as hour and minute offset from UTC #11

Closed
pawelkrol opened this Issue Jun 6, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@pawelkrol

Formatting time zone object using abbreviated time zone name (%Z) seems to work just fine, however formatting it as hour and minute offset from UTC (%z) appears to return an invalid result. See an example below, I would expect to get -05:00 rather than +00:00:

2.1.8 :001 > tz = TZInfo::Timezone.new('America/Chicago')
 => #<TZInfo::DataTimezone: America/Chicago>
2.1.8 :002 > tz.strftime('%Z')
 => "CDT"
2.1.8 :003 > tz.strftime('%:z')
 => "+00:00"

Is this problem related to your code, or is it dependent on specific environment settings and/or ruby installation? If the latter is the case, do you know any workaround to address it? Many thanks!

@philr

This comment has been minimized.

Show comment
Hide comment
@philr

philr Jun 6, 2016

Member

The Timezone#strftime method in the latest tzinfo release doesn't support %z (it gets passed to Time#strftime, which returns '+0000').

There is a change in the tzinfo git repository that adds support for %z. You could take the latest version of Timezone#strftime from timezone.rb and apply it as a monkey patch.

Alternatively, you could obtain the current offset using Timezone#period_for_utc and then format it separately:

tz.period_for_utc(Time.now.utc).utc_total_offset
# => -18000
Member

philr commented Jun 6, 2016

The Timezone#strftime method in the latest tzinfo release doesn't support %z (it gets passed to Time#strftime, which returns '+0000').

There is a change in the tzinfo git repository that adds support for %z. You could take the latest version of Timezone#strftime from timezone.rb and apply it as a monkey patch.

Alternatively, you could obtain the current offset using Timezone#period_for_utc and then format it separately:

tz.period_for_utc(Time.now.utc).utc_total_offset
# => -18000

@philr philr closed this Jun 6, 2016

@philr philr added the question label Jun 6, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment