Skip to content

Instantly share code, notes, and snippets.

@tleach
Created December 21, 2013 17:27
Show Gist options
  • Save tleach/8072327 to your computer and use it in GitHub Desktop.
Save tleach/8072327 to your computer and use it in GitHub Desktop.
How BSON decodes a datetime
def _get_date(data, position, as_class, tz_aware, uuid_subtype):
millis = struct.unpack("<q", data[position:position + 8])[0]
diff = millis % 1000
seconds = (millis - diff) / 1000
position += 8
if tz_aware:
dt = EPOCH_AWARE + datetime.timedelta(seconds=seconds)
else:
dt = EPOCH_NAIVE + datetime.timedelta(seconds=seconds)
return dt.replace(microsecond=diff * 1000), position
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment