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

UNIX time is not shown correctly #91

Closed
fukuchi opened this issue Aug 30, 2018 · 2 comments
Closed

UNIX time is not shown correctly #91

fukuchi opened this issue Aug 30, 2018 · 2 comments

Comments

@fukuchi
Copy link

fukuchi commented Aug 30, 2018

Timezone#strftime("%s", time) converts the given time to UNIX time. Because UNIX time is the number of seconds that have elapsed since 1970-01-01 00:00:00 (UTC), the result does not depends on the timezone.

However, the result of the folloing code seems incorrect.

#!/usr/bin/ruby

require 'tzinfo'

jst = TZInfo::Timezone.get("Japan")
utc = TZInfo::Timezone.get("UTC")

time = Time.now.utc

puts jst.strftime("%F %T (%Z): %s", time)
puts utc.strftime("%F %T (%Z): %s", time)

The code returned:

2018-08-30 21:18:23 (JST): 1535663903
2018-08-30 12:18:23 (UTC): 1535631503

In the case above, I expecet the result as:

2018-08-30 21:18:23 (JST): 1535631503
2018-08-30 12:18:23 (UTC): 1535631503

I used tzinfo-1.2.3 with Ruby 2.3.

philr added a commit that referenced this issue Aug 30, 2018
%s was being deferred to the result of the conversion to local. This is
actually a UTC instance being used to represent local time, so the
result would be the number of seconds since the epoch plus the total UTC
offset.

Override the handling of %s and use the passed in UTC instance to
calculate the value.

Resolves #91.
@philr
Copy link
Member

philr commented Aug 30, 2018

Thanks for the report.

This is already resolved in the upcoming version 2.0.0 release (currently on the master branch).

For earlier versions of TZInfo, this is a consequence of returning UTC instances of Time and DateTime to represent local times in order to support Ruby versions older than 1.9.3 (that can only handle times in the local system time zone and UTC).

I've committed a change to the 1.2 branch to make Timezone#strftime return the correct number of seconds since the epoch for the %s directive.

@philr philr closed this as completed Aug 30, 2018
@fukuchi
Copy link
Author

fukuchi commented Aug 30, 2018

Thank you for the quick response! Great help.

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