-
Notifications
You must be signed in to change notification settings - Fork 0
/
wita_pi_datetime.py
49 lines (40 loc) · 1.55 KB
/
wita_pi_datetime.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
#!/usr/bin/python
"""date and time methods for the Picroft skills
"""
import datetime
class pi_datetime:
""" provides methods around date and time
e.g. diff between two dates
more methods to come...
"""
def __init__(self):
self.iso_format = "%Y-%m-%d %H:%M:%S"
############################################################################
def diff(self, date_from, date_to):
""" calculates difference between the two dates
dates need to be formatted as string in self.iso_format
when a date is "now", the current date will be used.
The difference is returned in terms of days.
rc = 0 no errors, else some error occured
rc_msg = return message
rc_diff = difference between dates as int
"""
rc = 0
rc_msg = "success"
rc_diff = 0
if date_from == 'now':
dt_dt_from = datetime.datetime.now()
dt_str_from = dt_dt_from.strftime(self.iso_format)
else:
dt_str_from = date_from
if date_to == 'now':
dt_dt_to = datetime.datetime.now()
dt_str_to = dt_dt_to.strftime(self.iso_format)
else:
dt_str_to = date_to
dt_from = datetime.datetime.strptime(dt_str_from, self.iso_format)
dt_to = datetime.datetime.strptime(dt_str_to, self.iso_format)
rc_diff = ((dt_from - dt_to).days)
return rc, rc_msg, rc_diff
# class pi_datetime
############################################################################