Skip to content

Fix date calculations for date, datetime, datetime2 and datetimeoffset - #1023

Merged
arthurschreiber merged 4 commits into
masterfrom
arthur/fix-date-calculations
Jan 2, 2020
Merged

arthurschreiber merged 4 commits into
masterfrom
arthur/fix-date-calculations

Conversation

@arthurschreiber

Copy link
Copy Markdown
Collaborator

This changes the date calculations to not be based on Unix Epoch milliseconds, but on actual days between the data types base time and the given value.

This should fix all weirdness around DST changes and other timezone related shenanigans.

Fixes: #1018

References: #883, #755

This fixes the value calculations for `date`, `datetime`, `datetime2` 
and `datetimeoffset` to be independent of offset changes that could 
cause unexpected values to be written to the database.

BREAKING CHANGE: This should cause the values to be written to the 
database to be more in line of what is expected, but can be different 
for some edge cases.
@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

@mrliuc can you check this out? I believe it should fix the weird issues you've been running into around datetime values. 🙇

@codecov

codecov Bot commented Dec 26, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #1023 into master will increase coverage by 0.16%.
The diff coverage is 86.04%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1023      +/-   ##
==========================================
+ Coverage   79.15%   79.31%   +0.16%     
==========================================
  Files          86       86              
  Lines        4221     4221              
  Branches      779      780       +1     
==========================================
+ Hits         3341     3348       +7     
+ Misses        606      601       -5     
+ Partials      274      272       -2
Impacted Files Coverage Δ
src/data-types/datetime.ts 100% <100%> (ø) ⬆️
src/data-types/date.ts 40.9% <66.66%> (ø) ⬆️
src/data-types/datetimeoffset.ts 75% <81.81%> (ø) ⬆️
src/data-types/datetime2.ts 83.33% <83.33%> (ø) ⬆️
src/connection.ts 63.49% <0%> (+0.28%) ⬆️
src/value-parser.ts 82.29% <0%> (+1.38%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4112998...16e3348. Read the comment docs.

@arthurschreiber

arthurschreiber commented Dec 26, 2019 •

Copy link
Copy Markdown
Collaborator Author

For some more background information here - I believe the way we calculated the number of days between the epoch dates for datetime (01.01.1900) and datetime2/date/datetimeoffset (01.01.0001) and the passed in Date object was having problems with changing UTC offsets due to timezones changing their offsets over the years. I think this could lead to the days calculation being off by one in some cases.

See the following example based on the Asia/Hong_Kong timezone:

$ TZ=Asia/Hong_Kong node
Welcome to Node.js v13.5.0.
Type ".help" for more information.
> new Date(1900, 00, 01)
1899-12-31T16:23:18.000Z
> new Date(1900, 00, 01).toString()
'Mon Jan 01 1900 00:00:00 GMT+0736 (Hong Kong Standard Time)'
> new Date().toString()
'Fri Dec 27 2019 06:43:12 GMT+0800 (Hong Kong Standard Time)'

This behavior only changed semi-recently, with Node 10. Earlier versions had different behaviour, as can be seen here when running the same commands as before on Node.js 8:

$ TZ=Asia/Hong_Kong node
> new Date(1900, 00, 01).toString()
'Mon Jan 01 1900 00:00:00 GMT+0800 (HKT)'
> new Date().toString()
'Fri Dec 27 2019 06:45:20 GMT+0800 (HKT)'

This update in Node 10 was based on a change in V8 to better reflect timezone handling and be more compliant with the EcmaScript standard, but it caused the days calculation as implemented in tedious to be wrong in some cases.

This PR switches all these day calculations over to be based on LocalDate objects from the @js-joda/core module, which completely side-steps any timezone shenanigans and just calculates days based on the calendar dates.

This also removes the special code that was added in tedious for DST changes - I think that code was never correct to begin with, as DST changes are not even handled by any of the date types available in SQL Server.

@mrliuc

mrliuc commented Dec 27, 2019

Copy link
Copy Markdown

@arthurschreiber I did some test, when I insert into DB and Millisecond>997, The time part is wrong ,It's hard to describe, Here's the test code and results, when you see it,you will konw

code:
code

results:
mocha result
db result

@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

@mrliuc This is expected behaviour in SQL Server - the datetime data type's precision is not high enough to store milliseconds with the resolution you require.

This behaviour is documented here: https://docs.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql?view=sql-server-ver15#rounding-of-datetime-fractional-second-precision

As mentioned at the top of the linked page, you should probably move to datetime2 or datetimeoffset if you require higher precision than what is offered by datetime.

@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

@dhensby Can you run this against the node-mssql test suite? I believe this will fix all weirdness around date, datetime, datetime2 and datetimeoffset data types.

@mrliuc

mrliuc commented Dec 27, 2019

Copy link
Copy Markdown

@arthurschreiber datetime2 & datetimeoffset has a different but similar issue ,

I inserted into DB '2019-12-19 23:59:59:998',the data in DB was '2019-12-19 23:59:59.9980000',when I selected,I got '2019-12-19 23:59:59:997'

I saw the test suite in node-mssql and I think it's not comprehensive enough, the test suite just select the variable like this:
select @dt1 as dt1, @dt2 as dt2, code

It's not enough,you must insert into the really db like this:
insert into [TestCreateTime] values(@dt1);select CreateTime dt1 from TestCreateTime code

Then this line of code will get the result unequal
assert.strictEqual(+result.recordset[0].dt1, date.getTime())

PS:the test code about datetime2,datetimeoffset

@mrliuc

mrliuc commented Dec 27, 2019

Copy link
Copy Markdown

It seems like :

datetime: insert wrong select right

datetim2: insert right select wrong

I would rather use the datetime,unless you're going to be compatible with this issue.

@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

@mrliuc If you need millisecond accuracy, then datetime2 is the correct datatype to use, not datetime.

You can try the following to see this is an issue on SQL Server that can't be fixed in tedious:

SELECT CONVERT(varchar, CAST('2019-11-19 23:59:59:998' as datetime), 127);
-- 2019-11-19T23:59:59.997

SELECT CONVERT(varchar, CAST('2019-11-19 23:59:59:998' as datetime2), 127);
-- 2019-11-19T23:59:59.9980000

I also added additional test cases in 37c63ed that should show that we're handling datetime2 values correctly.

Did you update the mssql version you used for testing to use a version of tedious with the fixes in this PR? 🤔 I really can't explain how or why the datetime2 values are wrong, as the test cases I just added all pass.

@mrliuc

mrliuc commented Dec 28, 2019

Copy link
Copy Markdown

@arthurschreiber You are right,I find a bug in my test case , It's all pass now.Thank you very much

@dhensby

dhensby commented Jan 1, 2020

Copy link
Copy Markdown
Contributor

@dhensby Can you run this against the node-mssql test suite? I believe this will fix all weirdness around date, datetime, datetime2 and datetimeoffset data types.

I have tested it locally and the previously failing test is now passing 🎉

@arthurschreiber
arthurschreiber merged commit 54ed089 into master Jan 2, 2020
@arthurschreiber
arthurschreiber deleted the arthur/fix-date-calculations branch January 2, 2020 22:08
@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

🎉 This PR is included in version 8.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DateTime parameter,the Day is wrong when time >'23:59:16'

4 participants