Why MongoDB shell new ISODate (0001-01-01) returns date 1901-01-01

In the MongoDB shell on windows, if you run a query with a value

new ISODate('0001-01-01T00:00:00Z') 

he is really looking

 new ISODate('1901-01-01T00:00:00Z') 

If you enter β€œnew ISODate ('0001-01-01T00: 00: 00Z') directly into Mongo Shell, you will see that this conversion occurs when it returns ISODate (" 1901-01-01T00: 00: 00Z ") .

Oddly enough, when you use the "new date" instead of the "new ISODate" by entering:

 new Date('0001-01-01T:00:00:00Z') 

it returns an ISODate ("0001-01-01T00: 00: 00Z"), which is correct.

Both should return ISODate in accordance with the documents and, in my opinion, should act the same. Does anyone know why they are not doing this, and is this a bug or function?

+6
source share
1 answer

Inside, new ISODate really means:

 Date.UTC(year, month, date, hour, min, sec, ms); 

IE, MongoDB splits a string into regex elements (line 60 in https://github.com/mongodb/mongo/blob/master/src/mongo/shell/types.js#L56 )

The JavaScript Date object has several different initializers ( 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 not parsed or interpreted, but when you use it, like MongoDB does:

 Date.UTC( parseInt("0001") ) 

Then special rules apply for years 00-99. 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 ticket to the MongoDB server already at https://jira.mongodb.org/browse/SERVER-8164 , vote for it.

+5
source

Source: https://habr.com/ru/post/950182/


All Articles