-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Support for UTC dates as a config param #289
Conversation
I also made a different branch where the param name is 'tz' instead of 'utc' to possibly allow for a more complete timezone solution in the future. |
Instead of having an |
dresende, yes, that's a good idea. I headed down that path with this version: BryanDonovan@76842d7 |
I did this on master but I'm still fighting with the code on a local branch to make all dates synched from/to database. I'll merge it as soon as it's stable and tested. |
Maybe just copy the timezone tests from my branch over, and modify? |
I'll check them out soon :) |
I pushed the branch, you can check it here 9c6775f. Can anyone test this? I did some queries locally with several timezones (like +22:00 or -13:45). Default is "Z" (+0000). All tests seem to pass. Can anyone verify? @BryanDonovan @felixge |
I get a test failure during "make test":
|
Also, it looks like UTC is now the default? I personally prefer that, but it would be a dangerous change for people not expecting it. |
About your test, that's weird. I'm on GMT+1 but changed the test/run to use UTC-5:00 and it worked. I'm going to look a bit more to that test. About UTC being default, you're probably right, or not.. I don't know. I think people not complaining about not having UTC by default don't even know the problem their raising in the future. The others needing UTC will thank us :) But yeah, it's probably better to default to local. |
Are you sure you tested the branch or the master? I'm having the same error on the master but not on the timezone branch. |
If you need assistance on doing that, do: cd /your/node-mysql/repo
git pull
git checkout timezone
node test/run |
Oh, nevermind.. I tested master. Sorry about that. |
I personally think it would be better to leave the timezone out of the test/common.js file and set it explicitly in a test that only concerns itself with timezones. That way you have a test that covers the default situation and one that covers when you set it to something. I'd go as far as having three tests.. one for default, one for UTC, and one for an offset time. For what it's worth, my tests pass with your code (after changing the config signature in the test of course). |
Ok, when I update the tests I'll ask you to test again :) |
Could you test now? This branch also fixes a regression bug on a type cast test. Default timezone in this branch is 'local'. UTC timezone can be set by using In the tests, no timezone is enforced on common, local is used. There's a new test that tries some timezones and uses a custom typeCast function to avoid the timezone shifting when reading from the database and test if it's ok. This custom typeCast works like this: con.query({
sql: "SELECT * ...",
typeCast: function (field, parser, timeZone, next) {
if (field.type != ...) return next(); // using default typeCast
// do your thing...
return ...;
}
}); This was needed to check raw date from the database and I think some other people will like to use it for custom types. |
@felixge : any objection on merging this branch (https://github.com/felixge/node-mysql/tree/timezone) if everything looks ok? |
@dresende can you turn that branch into a pull request? difficult to review otherwise. |
I get a test failure: AssertionError: "Mon Oct 29 2012 13:54:07 GMT-0700 (PDT)" === "Mon Oct 29 2012 20:54:07 GMT-0700 (PDT)" .. that's when the offset is undefined. It also fails when it's 'Z': |
@felixge : ok |
I forgot my zone switched off DST last Sunday so now I'm on UTC. That's why my tests passed :P |
@BryanDonovan : could you test now? I think it's ok now. |
Your test passes, but this does not, which concerns me a bit..
I might be missing something though. |
This business of passing the timezone around bothers me to, but it's not my project, so as long as it works, it's good by me :)
I really hate passing in three arguments to a method, and hate passing booleans in even more. the second argument should be an options hash. It would obviously take a lot of refactoring to make that happen though :). |
I agree with you about the options argument, maybe later. Timezone is needed to escape dates and you don't need to pass it, the exported .escape() will use the connection config timezone. Can you post the output of your test (the |
You mean post what the values are here?
|
No, I meant the error, to see the time diff :) |
Well, it's passing now. Yesterday the time diff was 24 hours, so I imagine this test will fail for me later today after midnight UTC. Here's the output from yesterday when it failed:
|
Ah, I bet this is just a misuse of Date().getTimezoneOffset() or Date().getHours() in my test. I'll try to debug again tonight. |
Ok, please do. Timezones are freaking bad to debug. I spent a couple of hours to do this right. And I tested with +30:00 and ridiculous offsets (so hours and even days can be different) and it worked. |
Will do. Yeah, timezones are very painful (which is why everyone should save as UTC in their databases :)). |
Everything appears to be working fine.. |
Ok, I'm making a pull request out of the branch. |
So gooood news! 👍 |
Add support for returning table alias on Columns()
This pull request adds support for an optional config param named "utc" (boolean), when, if set, causes dates in result packets to be treated as UTC. In particular, the RowDataPacket.prototype._typeCast() function does this:
This functionality is needed because setting process.env.TZ is not reliable. If any Date manipulation (E.g., new Date.getTimezoneOffset()) happens before process.env.TZ is set, then the Date class's timezone will be stuck at what it was before process.env.TZ is set. See related bug #128.
I've added two new integration tests that cover how node-mysql deals with dates and timezones.
I can't say I like adding another param to these functions, e.g.,:
Should I try adding an options hash as the third param in that function instead? It would hold the 'typeCast', 'nestTables', and 'utc' args.