Skip to content

Instantly share code, notes, and snippets.

@atharvai
Created February 4, 2019 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atharvai/f1936b5a1c8a70ce5570e5646e40289c to your computer and use it in GitHub Desktop.
Save atharvai/f1936b5a1c8a70ce5570e5646e40289c to your computer and use it in GitHub Desktop.
BSON timestamp construct & deconstruct example
from bson import Timestamp
from datetime import datetime
ts=Timestamp(datetime.utcnow(), 1)
def ts_to_int(ts):
return (ts.time << 32) + ts.inc
def int_to_ts(t):
return Timestamp(t>>32, t & (2**32)-1)
print(ts_to_int(ts))
# 6654037895042564097L
print(int_to_ts(6654037895042564097L))
# Timestamp(1549263926, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment