Skip to content

Fixes #636. Prevents losing days when using date columns in certain timezones. #646

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

Closed
wants to merge 1 commit into from

Conversation

tgpfeiffer
Copy link
Contributor

The problem is pretty complex. Say we have a data column in MySQL and an input string like 2012-05-12 00:00:00 Z as in the unit test.

In a time zone like CET (UTC+1), passing this string to the Date constructor will give a Date object in the local time zone, i.e. Sat May 12 2012 01:00:00 GMT+0100 (CET). If this is stored in the database, it will be truncated to 2012-05-12 (which is the correct day). When it is fetched from the database, it will be assumed that the time is 00:00:00 and UTC, so we will end up with exactly the same date as when parsing the input string.

In a time zone like EDT (UTC-4), passing the same string to the Date constructor will also give a Date object in the local time zone, i.e. Fri May 11 2012 20:00:00 GMT-0400 (EDT). If this is stored in the database, it will be truncated to 2012-05-11 (which is different than before, but we can argue this is correct behavior, since in a EDT place, the point in time described by the input string is May 11). However, when it is fetched from the database, it will be assumed that the time is 00:00:00 and UTC, and parsing 2012-05-11 00:00:00 in EDT will give Thu May 10 2012 20:00:00 GMT-0400 (EDT), i.e., we lose one day - which is what @iarna observed.

In order to deal with that, after parsing a date string in a time zone with a positive offset compared to UTC (i.e., UTC-x), I add 24 hours to get back the original time stamp that we put in. This should fix the issue that @iarna reported.

Can someone please comment on that before merging in?

@tgpfeiffer
Copy link
Contributor Author

If merged, this would also fix #360 (more or less duplicate of #636).

@felixge
Copy link
Collaborator

felixge commented Nov 18, 2013

However, when it is fetched from the database, it will be assumed that the time is 00:00:00 and UTC

What timeZone setting are you using? Are you referring to this code: https://github.com/felixge/node-mysql/blob/master/lib/protocol/packets/RowDataPacket.js#L61 ? Or are you saying that the Date constructor is making this assumption?

--fg

@tgpfeiffer
Copy link
Contributor Author

@felixge The Date constructor makes this assumption:

> new Date("2012-05-12")
Sat May 12 2012 02:00:00 GMT+0200 (CEST)

When the system time zone is EDT:

> new Date("2012-05-12")
Fri May 11 2012 20:00:00 GMT-0400 (EDT)

@felixge
Copy link
Collaborator

felixge commented Nov 19, 2013

I don't have time to think through this today, I'll ping you tomorrow.

@felixge
Copy link
Collaborator

felixge commented Nov 21, 2013

Fixed in dc23690. The test should never have used a UTC Date, as this will yield a different local date in different time zones, causing the outcome of the test to change. The other thing that was broken is that we didn't add '00:00:00' to local DATE fields before parsing them, causing them to be parsed as UTC.

@felixge felixge closed this Nov 21, 2013
dveeden pushed a commit to dveeden/mysql that referenced this pull request Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants