Fixes #636. Prevents losing days when using date columns in certain timezones. #646
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The problem is pretty complex. Say we have a
data
column in MySQL and an input string like2012-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 aDate
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 to2012-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 to2012-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 parsing2012-05-11 00:00:00
in EDT will giveThu 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?