Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pawelkrol opened this issue Jun 6, 2016 · 1 comment
Closed
Labels

Comments

@pawelkrol
Copy link

@pawelkrol pawelkrol commented Jun 6, 2016

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
Copy link
Member

@philr 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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.