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

Converted time cannot be converted back to UTC #61

Closed
rstrobl opened this issue Nov 18, 2016 · 2 comments
Closed

Converted time cannot be converted back to UTC #61

rstrobl opened this issue Nov 18, 2016 · 2 comments

Comments

@rstrobl
Copy link

rstrobl commented Nov 18, 2016

It seems like utc_to_local cannot be converted back to UTC. My guess: maybe this has something to do with the 1 hour difference that comes from summer/winter time in Berlin?

tz = TZInfo::Timezone.get('Europe/Berlin')
time = Time.at(1478874502).utc # -> 2016-11-11 14:28:22 UTC
converted_time = tz.utc_to_local(time)
back_to_utc = converted_time.utc # -> 2016-11-11 15:28:22 UTC
@philr
Copy link
Member

philr commented Nov 18, 2016

This happens because the current version of TZInfo always returns local times as UTC (with an offset of 0):

tz = TZInfo::Timezone.get('Europe/Berlin')
time = Time.at(1478874502).utc # -> 2016-11-11 14:28:22 UTC
converted_time = tz.utc_to_local(time) # -> 2016-11-11 15:28:22 UTC

The converted_time instance is already in UTC, so calling .utc won't change anything.

TZInfo works this way because because the Time class in older versions of Ruby could only represent times in either UTC or the configured local time zone.

The next major version of TZInfo will return local times using the correct offset (and only support Ruby 1.9.3 or later). This will allow conversion back to UTC. The latest code in the git master branch will give you the result you are expecting:

tz = TZInfo::Timezone.get('Europe/Berlin')
time = Time.at(1478874502).utc # -> 2016-11-11 14:28:22 UTC
converted_time = tz.utc_to_local(time) # -> 2016-11-11 15:28:22 +0100
back_to_utc = converted_time.utc # -> 2016-11-11 14:28:22 UTC

@rstrobl
Copy link
Author

rstrobl commented Nov 18, 2016

great, thanks a lot :)

@philr philr closed this as completed Nov 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants