This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
timestamps.py
58 lines (49 loc) · 2.01 KB
/
timestamps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- python-indent-offset: 4 -*-
'''
timestamps: utilities for working with timestamps
'''
__version__ = '1.0.0'
__author__ = 'Michael Hucka <[email protected]>'
__email__ = '[email protected]'
__license__ = 'GPLv3'
from datetime import datetime
from dateutil import parser
from time import time, mktime
# Main code.
# .............................................................................
# Mongo date & time objects are in UTC. (See here for examples:
# https://api.mongodb.org/python/current/examples/datetimes.html)
# However, those objects take up 48 bytes. If we store date/time values
# as floats, they only take up 24 bytes. Since every entry in the database
# has at least 2 dates, that means we can save 48 * 25,000,000 bytes = 1.2 GB
# (at least) by storing them as floats instead of the default date objects.
def canonicalize_timestamp(value):
'''Returns a POSIX timestamp value in UTC.
The value can be inverted using the following:
from datetime import datetime
datetime.utcfromtimestamp(thevalue)
'''
if isinstance(value, float):
# Assume it's already a POSIX timestamp float in UTC.
return value
elif isinstance(value, str):
# Assume ISO8601 format such as GitHub's: "2012-07-20T01:19:13Z"
datetime_created_at = parser.parse(value)
return mktime(datetime_created_at.utctimetuple())
elif isinstance(value, datetime):
return mktime(value.utctimetuple())
else:
# Should do more here, but not sure what.
return value
def now_timestamp():
'''Returns a UTC-aware POSIX date/time stamp for "now", as a float.'''
return mktime(datetime.now().utctimetuple())
def timestamp_str(value):
'''Returns a readable string from a floating point POSIX date/time stamp.'''
if not value:
return ''
elif isinstance(value, float):
# 2012-07-20T01:19:13Z
return datetime.utcfromtimestamp(value).isoformat()
else:
raise ValueError('Expected a float but got "{}"'.format(value))