Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Preserve specified time offsets from YAML #121

Open
wants to merge 1 commit into from

3 participants

@chrisperelstein

This change causes parse_time function to return a Ruby Time object with an offset specified in the parsed YAML. I've been playing with Jekyll which leverages Pysch for parsing YAML data in posts and it would be nice to have Time objects extracted from posts respect the offset specified in the YAML front matter. Then, I could either display it with the specified time offset, convert it to utc with the Time::utc, or convert it to local time with Time::getlocal. This seems to me to be a little bit better behavior, but there may be a good reason not to do this that I'm not aware of.

@sheerun

fuck yeah

@tenderlove
Owner

@chrisperelstein it seems fine, but can you demo the issue for me? I don't want the code to be reverted accidentally. :(

@chrisperelstein

I'll work on getting an example up late tonight with example Yaml, but basically the last line in parse_time uses Time.at. Time.at always returns a Time object whose gmt_offset method will be the current offset on the machine in which it's running, instead an offset specified in the Yaml front matter.

Assuming a bit of Yaml like "date_published: 2013-04-08T17:34:54-07:00":

  • Currently, if I'm in New York (-4 hour offset), I'll get a Ruby Time object that whose gmt_offset method returns -14400 (-4 hours) instead of -25200 (-7 hours).
  • With my change, if I'm in New York, I'll get a Ruby Time object whose gmt_offset method returns -25200 (-7 hours).

I'll get a gist or something up tonight that demonstrates this.

@tenderlove
Owner

Bump. Any update on a test? Thanks!

@chrisperelstein

I'm sorry, I've been slack lately. Bought a house and it's been a bit of a time suck. Rough ruby script that demonstrates the issue here: https://gist.github.com/chrisperelstein/5978935. Hopefully that makes enough sense. With my change, you'll get the same result when printing the date since the Psych generated time object will get the offset originally specified in the input.

@chrisperelstein chrisperelstein Preserve offset when parsing times
Changing scaler_scanner's parse_time function to return a time object
with offset preserved if it was specified.
451f794
@tenderlove
Owner

@chrisperelstein sorry to take so long (2 years :frowning:) to get back on this. I just looked at your repro script. It looks like you're calling Psych.load against a value that Psych didn't generate. Time#to_s doesn't produce a sting that's in the YAML format spec.

If this is still an issue, can you please give me a test for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 11, 2013
  1. @chrisperelstein

    Preserve offset when parsing times

    chrisperelstein authored
    Changing scaler_scanner's parse_time function to return a time object
    with offset preserved if it was specified.
This page is out of date. Refresh to see the latest.
Showing with 1 addition and 1 deletion.
  1. +1 −1  lib/psych/scalar_scanner.rb
View
2  lib/psych/scalar_scanner.rb
@@ -143,7 +143,7 @@ def parse_time string
offset += ((tz[1] || 0) * 60)
end
- klass.at((time - offset).to_i, us)
+ klass.new(yy, m, dd, hh, mm, ss+us/1000000, offset)
end
end
end
Something went wrong with that request. Please try again.