[SERVER-14623] ObjectId.fromDate is not correct for all dates Created: 21/Jul/14  Updated: 22/Jul/15  Resolved: 29/Jul/14

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: 2.7.2
Fix Version/s: 2.7.5

Type: Bug Priority: Minor - P4
Reporter: James O'Leary Assignee: James O'Leary
Resolution: Done Votes: 0
Labels: community-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-12007 add support for new ObjectId(Date|Num... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

Run the following code in a v2.7.2/v2.7.3 shell :

> ok = Math.pow(2, 28)
268435456
> notok = ok -1 
268435455
> ObjectId.fromDate(new Date(ok  * 1000))
ObjectId("100000000000000000000000")
> ObjectId.fromDate(new Date(notok  * 1000))
Error: invalid object id: length

Sprint: Server 2.7.4, Server 2.7.5
Participants:

 Description   

The ObjectId.fromDate fails for dates less than (Math.pow(2,28) - 1).

The code on line 379 should look something like :

    var pad = "00000000";
    var hexSeconds = seconds.toString(16);
    hexSeconds = pad.substring(0, pad.length - hexSeconds.length) + hexSeconds;



 Comments   
Comment by Githook User [ 29/Jul/14 ]

Author:

{u'username': u'jimoleary', u'name': u'Jim OLeary', u'email': u'jim.oleary@gmail.com'}

Message: SERVER-14623 fixed shell function ObjectId.fromDate for dates before 1978-07-04T21:24:15Z

ObjectId.fromDate is not correct for all dates

There is an under-run for dates less than 1978-07-04T21:24:15Z ('Math.pow(2,28) - 1' seconds)
Added extra test for upper date limit.

Closes #725

Signed-off-by: Benety Goh <benety@mongodb.com>
Branch: master
https://github.com/mongodb/mongo/commit/c7590ac4ec54f52af224248f83a4f722b0abf319

Comment by James O'Leary [ 28/Jul/14 ]

A Simpler solution is just :

    var hexSeconds = Math.floor(timestamp/1000).toString(16).pad(8,false,'0');

Generated at Thu Feb 08 03:35:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.