Converted time cannot be converted back to UTC #61

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

Comments

Projects
None yet
2 participants
@rstrobl

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

This comment has been minimized.

Show comment
Hide comment
@philr

philr Nov 18, 2016

Member

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
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

This comment has been minimized.

Show comment
Hide comment
@rstrobl

rstrobl Nov 18, 2016

great, thanks a lot :)

rstrobl commented Nov 18, 2016

great, thanks a lot :)

@philr philr closed this Nov 18, 2016

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