Skip to content

Instantly share code, notes, and snippets.

@lager1
Forked from philippreston/objectid-convert.py
Created March 8, 2024 16:32
Show Gist options
  • Save lager1/28e18465c4cc2425bed90b7628448eb2 to your computer and use it in GitHub Desktop.
Save lager1/28e18465c4cc2425bed90b7628448eb2 to your computer and use it in GitHub Desktop.
Convert Mongo Object Id to Readable
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