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

Timestamps producing wrong day with UTC.date #91

Closed
ChrisWren opened this Issue Sep 16, 2013 · 5 comments

Comments

Projects
None yet
2 participants
@ChrisWren
Copy link

ChrisWren commented Sep 16, 2013

I looked through your issues, and saw that there has previously been issues with timestamp parsing: #46, #19.

Despite the resolution of these issues, during my use of js-yaml I am getting the date offset based on my localization. Here is an example of using js-yaml on your site versus calling the Date constructor:

screen shot 2013-09-15 at 18 21 47

As you can see js-yaml is returning the previous day.

Digging around in the source code, I removed the Date.UTC from this line of code, and received the expected output.

Is there something I am missing here about using js-yaml or would you consider this a bug worth solving via a pull request?

Thanks for this useful module!

@ixti

This comment has been minimized.

Copy link
Contributor

ixti commented Sep 16, 2013

js-yaml is a YAML implementation in JavaScript, thus it follows YAML standard. For this particular case it will be timestamp schema:

If the time zone is omitted, the timestamp is assumed to be specified in UTC. The time part may be omitted altogether, resulting in a date format. In such a case, the time part is assumed to be 00:00:00Z (start of day, UTC).

@ixti

This comment has been minimized.

Copy link
Contributor

ixti commented Sep 16, 2013

In other words it parses 2013-2-2 as UTC date/time, which is +8 hours relatively to PST.

% for TZ in UTC America/Los_Angeles; do printf "%-20s | %s\n" $TZ "$(TZ=$TZ date)"; done
UTC                  | Mon Sep 16 02:18:25 UTC 2013
America/Los_Angeles  | Sun Sep 15 19:18:25 PDT 2013

@ixti ixti closed this Sep 16, 2013

@ChrisWren

This comment has been minimized.

Copy link
Author

ChrisWren commented Sep 16, 2013

@ixti thanks for taking the time to explain it. I guess my beef is with YAML defaulting to UTC, because as a user I want to be able to put in 2013-2-2 in a blog post and get back Feb 02 2013 00:00:00 instead of Feb 01 2013 16:00:00. My solution now is going to be to convert each part of the date back to UTC using this method:

var now = jsyaml.load('2013-2-2');
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
@ixti

This comment has been minimized.

Copy link
Contributor

ixti commented Sep 16, 2013

I would extract this method into a helper, and a little bit different way:

function transposeDate(d) {
  return new Date(d.getTime() + d.getTimezoneOffset() * 60000);
}

transposeDate(yaml.load("2013-2-2"));
// Sat Feb 02 2013 00:00:00 GMT+0100 (CET)
@ixti

This comment has been minimized.

Copy link
Contributor

ixti commented Sep 16, 2013

After all you might find it's better to define your own schema with modified timestamp type. Check sources of default schemas and types...

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