[SERVER-8164] ISODate doesn't handle years less than 100 properly Created: 14/Jan/13  Updated: 06/Dec/17  Resolved: 22/Aug/17

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: 3.5.13

Type: Bug Priority: Minor - P4
Reporter: Jeffrey Yemin Assignee: Derick Rethans
Resolution: Done Votes: 4
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented
is documented by DOCS-10738 Docs for SERVER-8164: ISODate doesn't... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

> db.date.insert({ "_id" : ISODate("0099-09-07T00:00:00Z") })
> db.date.findOne()
{ "_id" : ISODate("1999-09-07T00:00:00Z") }

expected:

> db.date.insert({ "_id" : ISODate("0099-09-07T00:00:00Z") })
> db.date.findOne()
{ "_id" : ISODate("0099-09-07T00:00:00Z") }

Sprint: Query 2017-08-21, Query 2017-09-11
Participants:

 Description   

ISODate is parsing dates where the year is less than 100 improperly. It's treating them as offsets from 1900.



 Comments   
Comment by Githook User [ 22/Aug/17 ]

Author:

{'username': 'derickr', 'email': 'github@derickrethans.nl', 'name': 'Derick Rethans'}

Message: SERVER-8164 ISODate doesn't handle years less than 100 properly
Branch: master
https://github.com/mongodb/mongo/commit/c40825a5a209467a027c5dc4b7ea8f0de2c8b4df

Comment by Derick Rethans [ 20/Aug/17 ]

adrianhesketh@hushmail.com – thanks, but we have patch already in code review (note the status of this report), that solves this a little more elegantly. You can see the current draft patch at https://github.com/mongodb/mongo/compare/master...derickr:SERVER-8164-isodate-shell-parsing

Comment by Adrian Hesketh [ 20/Aug/17 ]

I've submitted a pull request to fix this at https://github.com/mongodb/mongo/pull/1170

Comment by Stephane [ 19/Sep/13 ]

yes it's a important bug

another exemple that show the misconception problem:

> db.date.insert(

{ "_id" : new Date("0099-09-07T00:00:00Z") }

)
> db.date.findOne()

{ "_id" : ISODate("0099-09-07T00:00:00Z") }

here ISODate("0099-09-07T00:00:00Z") mean 0099-09-07T00:00:00Z
but when you write db.date.insert(

{ "_id" : new Date("0099-09-07T00:00:00Z") }

)
it's mean 1999-09-07T00:00:00Z

so ISODate is buggy and must be corrected and work same way as new Date

Comment by Derick Rethans [ 24/Jul/13 ]

I've done some research for this, as it popped up in http://stackoverflow.com/questions/17833796/why-mongodb-shell-new-isodate0001-01-01-returns-date-1901-01-01. My answer there was:

Internally, new ISODate really means:
Date.UTC(year, month, date, hour, min, sec, ms);
IE, MongoDB splits up the string into elements with a regular expression (Line 60 at https://github.com/mongodb/mongo/blob/master/src/mongo/shell/types.js#L56)
The JavaScript Date object has a few different initialisers (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Syntax). If you use:
new Date("0001-01-01T:00:00:00");
Then the four digit year 0001 is not parsed or interpreted, but when you use it like MongoDB does:
Date.UTC( parseInt("0001") )
Then special rules apply for the years 00-99 apply. The docs at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Date_instances slightly hint at this.
There is a MongoDB server ticket already at https://jira.mongodb.org/browse/SERVER-8164, please vote for it.
Generated at Thu Feb 08 03:16:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.