-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi_time.py
81 lines (66 loc) · 1.97 KB
/
rpi_time.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
#!/usr/bin/python
from datetime import datetime
import ds1302
class DS1302:
def __init__(self, rangechecks=True):
self.rangechecks = rangechecks
ds1302.init_clock()
def get_datetime(self):
if not self.check_sanity():
# if clock are insane: try to reset it
self.reset_clock()
# if clock are still insane: return None
if not self.check_sanity():
return None
year, month, date = ds1302.get_date()
hour, minute, second = ds1302.get_time()
if self.rangechecks:
if year < 2000 or year > 3000:
ds1302.set_date(2000, month, date)
return self.get_datetime()
if month not in range(1, 13):
ds1302.set_date(year, 1, date)
return self.get_datetime()
if date not in range(1, 32):
ds1302.set_date(year, month, 1)
return self.get_datetime()
if hour not in range(0, 24):
ds1302.set_time(0, minute, second)
return self.get_datetime()
if minute not in range(0, 60):
ds1302.set_time(hour, 0, second)
return self.get_datetime()
if second not in range(0, 60):
ds1302.set_time(hour, minute, 0)
return self.get_datetime()
return datetime(year, month, date,
hour, minute, second)
def set_datetime(self, dt):
if not self.check_sanity():
# if clock are insane: try to reset it
self.reset_clock()
# if clock are still insane: return False
if not self.check_sanity():
return False
ds1302.set_date(dt.year, dt.month, dt.day)
ds1302.set_time(dt.hour, dt.minute, dt.second)
return True
def check_sanity(self):
"check sanity of a clock. returns True if clock is sane and False otherwise"
year, month, date = ds1302.get_date()
hours, mins, secs = ds1302.get_time()
if year == 2000 or month == 0 or date == 0:
return False
if secs == 80:
return False
return True
def reset_clock(self):
ds1302.reset_clock()
def format_time(dt):
if dt is None:
return ""
fmt = "%m/%d/%Y %H:%M"
return dt.strftime(fmt)
def parse_time(s):
fmt = "%m/%d/%Y %H:%M"
return datetime.strptime(s, fmt)