Preserve specified time offsets from YAML #121

wants to merge 1 commit into


3 participants

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 commented Feb 27, 2013

fuck yeah


tenderlove commented Apr 5, 2013

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

I'll work on getting an example up late tonight with example Yaml, but basically the last line in parse_time uses 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 commented Jul 5, 2013

Bump. Any update on a test? Thanks!

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

Preserve offset when parsing times
Changing scaler_scanner's parse_time function to return a time object
with offset preserved if it was specified.

tenderlove commented Jan 8, 2015

@chrisperelstein sorry to take so long (2 years 😦) 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?

@tenderlove My turn to take so long. I seem to have deleted the gist I was using as an example (sorry!) so I'm going to try to recreate something. Don't remember where the snippet of yaml came from that I used but I think it was something I was manually doing in order to fix a time zone changing issue I noticed when using octopress. The functional punchline of the pull request is to simply preserve time offset when specified regardless of where the time came from. My opinion is that Pysch shouldn't discard original offset info by automatically converting to local time offset, especially since the Time object includes the convenient getlocal method.

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