-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_time.py
59 lines (45 loc) · 1.64 KB
/
date_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
'''
Date & time settings.
'''
__all__ = (
'DateTime',
)
import logging
from time import localtime, strptime
from rocket_r60v.exceptions import SettingValueError
from .base import WritableSetting
LOGGER = logging.getLogger(__name__)
class DateTime(WritableSetting):
'''
The date & time (clock) of the machine.
'''
address = 0xA000
length = 7
def get(self, *args, **kwargs): # pylint: disable=arguments-differ,unused-argument
'''
The date & time can't be read, therefor an error is printed.
:return: The time
:rtype: str
'''
return f'The date & time can only be set and not read ("fire & forget" if you will so).'
def set(self, date_and_time, *args, **kwargs): # pylint: disable=arguments-differ
'''
Set the date & time.
:param str time: The time
'''
try:
if date_and_time == 'auto':
time_struct = localtime()
else:
time_struct = strptime(date_and_time, '%d.%m.%y %H:%M')
day = time_struct.tm_mday
month = time_struct.tm_mon
year = time_struct.tm_year - 2000
weekday = time_struct.tm_wday + 1
hour = time_struct.tm_hour
minute = time_struct.tm_min
return super().set([0, minute, hour, weekday, day, month, year], *args, **kwargs)
except (ValueError, TypeError) as ex:
error = 'Value "%s" is not a valid date & time format (auto | dd.mm.yy HH:MM)'
LOGGER.error(error, date_and_time)
raise SettingValueError(error % date_and_time) from ex