psych icon indicating copy to clipboard operation
psych copied to clipboard

Preserve specified time offsets from YAML

Open chrisperelstein opened this issue 12 years ago • 7 comments

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.

chrisperelstein avatar Jan 16 '13 22:01 chrisperelstein

fuck yeah

sheerun avatar Feb 27 '13 13:02 sheerun

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

tenderlove avatar Apr 05 '13 18:04 tenderlove

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.

chrisperelstein avatar Apr 08 '13 21:04 chrisperelstein

Bump. Any update on a test? Thanks!

tenderlove avatar Jul 05 '13 19:07 tenderlove

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 avatar Jul 11 '13 20:07 chrisperelstein

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

tenderlove avatar Jan 08 '15 22:01 tenderlove

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

chrisperelstein avatar Apr 04 '16 16:04 chrisperelstein