-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
73 lines (57 loc) · 1.66 KB
/
Copy pathutils.py
File metadata and controls
73 lines (57 loc) · 1.66 KB
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
# Module for utility helper methods
from datetime import *
TIME_FORMAT = '%I:%M %p'
def add_time(init_time, time_delta):
"""
Adds a time_delta to a time object.
:param datetime.time init_time: the starting time
:param datetime.time_delta time_delta: the time_delta
:return: the sum
:rtype: datetime.time
"""
# O(1)
# Must convert to datetime first
init_dt = time_to_datetime(init_time)
sum_dt = init_dt + time_delta
return sum_dt.time()
def time_to_datetime(t):
"""
Converts a time object to a datetime object.
:param datetime.time t: the time object
:return: the datetime object
:rtype: datetime.datetime
"""
# Time complexity O(1)
return datetime.combine(date.today(), t)
def time_str(t, fmt):
"""
Formats a time object as a string.
:param datetime.time t: the time object
:param string fmt: the format
:return: the formatted time object
:rtype: string
"""
# Time complexity O(1)
return time_to_datetime(t).strftime(fmt)
def str_to_time(s, fmt):
"""
Parses a string into a time object
:param string s: the string to parse
:param string fmt: the format
:return: the parsed time object
:rtype: datetime.time
"""
# Time complexity O(1)
return datetime.strptime(s, fmt).time()
def time_diff(t1, t2):
"""
Calculates the difference of two datetime.time objects
:param datetime.time t1: the minuend
:param datetime.time t2: the subtrahend
:return: the difference
:rtype: datetime.time_delta
"""
# Time complexity O(1)
dt1 = time_to_datetime(t1)
dt2 = time_to_datetime(t2)
return dt1 - dt2