-
Notifications
You must be signed in to change notification settings - Fork 1
/
timetools.py
88 lines (61 loc) · 2.03 KB
/
timetools.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
timetools.py provides functions that perform conversion among these types: second since epoch, string, datetime
"""
import datetime
import pytz
from dateutil.parser import parse
epoch = datetime.datetime.utcfromtimestamp(0)
epoch = epoch.replace(tzinfo=pytz.UTC)
TIME_FORMAT = '%Y-%m-%d %H:%M:%S %z'
# TODO: take mplotlib madates conversion take into consideraiton
def string_to_datetime(str_):
""" translate a formatted string to a datetime object
Args:
str_ (string): a formatted sgtring for date, time
Return:
datetime, a datetime object with UTC as timezone
"""
dt = parse(str_)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=pytz.UTC)
return dt
def datetime_to_epoch(dt):
""" translate a python datetime object to seconds since epoch
Args:
dt (datetime): a datetime object
Returns:
int, seconds since epoch
"""
return int((dt-epoch).total_seconds())
def string_to_epoch(str_):
""" translate an UTC time string to epoch time
Args:
str_ (string): a string describing a UTC time in certain format
Returns:
int, seconds since the epoch
"""
return datetime_to_epoch(string_to_datetime(str_))
def datetime_to_string(dt):
""" translate a python datetime object into a readable string
Args:
dt (datetime): a datetime object
Returns:
string, a formatted string for date, time, and time zone
"""
return datetime.datetime.strftime(dt, TIME_FORMAT)
def epoch_to_datetime(epc):
""" translate seconds since epoch to a datetime object, UTC as timezone
Args:
epc (int) : seconds since epoch
Returns:
datetime, a datetime object with UTC as timezone
"""
return datetime.datetime.fromtimestamp(epc, pytz.utc)
def epoch_to_string(epc):
""" translate seconds since epoch to a formatted string
Args:
epc (int) : seconds since epoch
Returns:
string, a formatted string for date, time
"""
return datetime_to_string(epoch_to_datetime(epc))