-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatetime_utils.py
37 lines (26 loc) · 1.14 KB
/
datetime_utils.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
# utilities to perform datetime calculations
from datetime import datetime
from pytz import timezone
from datetime import timedelta
def get_clip_name_from_unix_time(source_guid, current_clip_start_time):
"""
"""
# convert unix time to
readable_datetime = datetime.fromtimestamp(int(current_clip_start_time)).strftime('%Y_%m_%d_%H_%M_%S')
clipname = source_guid + "_" + readable_datetime
return clipname, readable_datetime
def get_difference_between_times_in_seconds(unix_time1, unix_time2):
dt1 = datetime.fromtimestamp(int(unix_time1))
dt2 = datetime.fromtimestamp(int(unix_time2))
return (dt1-dt2).total_seconds()
def add_interval_to_unix_time(unix_time, interval_in_seconds):
dt1 = datetime.fromtimestamp(int(unix_time)) + timedelta(0, interval_in_seconds)
dt1_aware = timezone('US/Pacific').localize(dt1)
end_time_unix = int(dt1_aware.timestamp())
return end_time_unix
def get_unix_time_from_datetime_utc(dt_utc):
dt_aware = timezone('UTC').localize(dt_utc)
dt_pst = dt_aware.astimezone(timezone('US/Pacific'))
# convert to PST
unix_time = int(dt_pst.timestamp())
return unix_time