Skip to content

Instantly share code, notes, and snippets.

@tleach
Last active January 1, 2016 01:19
Show Gist options
  • Save tleach/8072406 to your computer and use it in GitHub Desktop.
Save tleach/8072406 to your computer and use it in GitHub Desktop.
How BSON encodes a datetime
if isinstance(value, datetime.datetime):
if value.utcoffset() is not None:
value = value - value.utcoffset()
millis = int(calendar.timegm(value.timetuple()) * 1000 +
value.microsecond / 1000)
return BSONDAT + name + struct.pack("<q", millis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment