Last active
March 8, 2024 16:32
-
-
Save philippreston/a07495bfa99e33dd41ac3f755399f67f to your computer and use it in GitHub Desktop.
Convert Mongo Object Id to Readable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import datetime | |
objectid = int(sys.argv[1], 16) | |
fmt = "%Y-%m-%d %H:%M:%S" | |
counter = objectid & 0xFFFFFF | |
shift = 24 | |
process_id = (objectid >> shift) & 0xFFFF | |
shift += 16 | |
machine_id = (objectid >> shift) & 0xFFFFFF | |
shift += 24 | |
timestamp = (objectid >> shift) & 0xFFFFFFFF | |
shift += 32 | |
t = datetime.datetime.fromtimestamp(timestamp).strftime(fmt) | |
print "Counter:\t%d" % counter | |
print "Process:\t%d" % process_id | |
print "Machine:\t%d" % machine_id | |
print "Time:\t\t%s" % t | |
assert shift == (12 * 8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment