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

Preserve time zone offset when deserializing times #316

Merged
merged 2 commits into from
May 22, 2017

Conversation

stomar
Copy link
Contributor

@stomar stomar commented Apr 13, 2017

Times with specified time zone offset are converted to local time, currently. I consider this a bug, because information that is present in the YAML gets lost needlessly.

I think the upcoming major version release would be a good occasion to fix this.

Current behavior:

require "psych"

ENV["TZ"] = "EST"

time = Time.new(2017, 4, 13, 12, 0, 0, "+02:00")
yaml = Psych.dump(time)
restored = Psych::load yaml

restored == time  # => true, it's the same point in time

yaml      # => "--- 2017-04-13 12:00:00.000000000 +02:00\n...\n"
time      # => 2017-04-13 12:00:00 +0200
restored  # => 2017-04-13 05:00:00 -0500 (!)

New behavior:

# ...
yaml      # => "--- 2017-04-13 12:00:00.000000000 +02:00\n...\n"
time      # => 2017-04-13 12:00:00 +0200
restored  # => 2017-04-13 12:00:00 +0200

Note that I am not at all familiar with the psych codebase, so feel free to give feedback if something is missing.

Update: The first commit adds "bonus" test cases that do not directly concern the proposed change, but seemed reasonable.

Update: DateTime will change similarly (without need for special treatment; it seems to rely on Time). I amended the commits to include a test case that verifies this behavior.

@stomar stomar force-pushed the timezone-offset branch 3 times, most recently from eed3c7f to 5b8b041 Compare April 13, 2017 15:50
stomar added 2 commits May 22, 2017 15:56
Add test cases for times with nonzero usec, and for times
other than UTC. Note that in the latter case the test only
ensures that the restored time designates the same point
in time, not that is has the same time zone offset.
Fix support of time zone offsets. Now the time zone offset
will be preserved when times are deserialized from YAML with
a specified offset. Previously, times other than UTC ("Z")
where converted to local time.

The test cases use two different offsets to account for the
possibility of an erratic success when the tests are run in
the corresponding time zone.
@stomar stomar force-pushed the timezone-offset branch 3 times, most recently from 4694f38 to d2db988 Compare May 22, 2017 15:45
@stomar
Copy link
Contributor Author

stomar commented May 22, 2017

@tenderlove @hsbt Any thoughts on this?

@tenderlove
Copy link
Member

@stomar thanks for the bump! Yes, I think this is good. I'll merge! 😄

@tenderlove tenderlove merged commit e8cfcfe into ruby:master May 22, 2017
@stomar
Copy link
Contributor Author

stomar commented May 22, 2017

Wow, that was fast... thanks!

@stomar stomar deleted the timezone-offset branch May 22, 2017 16:05
@stomar
Copy link
Contributor Author

stomar commented May 30, 2017

@hsbt Will this be included in the preview of 2.5?

@hsbt
Copy link
Member

hsbt commented May 31, 2017

@stomar Yes, I will merge current master into Ruby 2.5 and release pysch-3.0.0.beta2.

matzbot pushed a commit to ruby/ruby that referenced this pull request Jun 16, 2017
  It contains following changes from 3.0.0.beta1

    * Preserve time zone offset when deserializing times
      ruby/psych#316

    * Enable YAML serialization of Ruby delegators
      ruby/psych#158

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59101 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
@headius headius mentioned this pull request Dec 1, 2017
75 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants