-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
30 lines (25 loc) · 817 Bytes
/
util.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
import adafruit_datetime
class Util:
@staticmethod
def to_datetime(as_str: str) -> adafruit_datetime.datetime:
# workaround for https://github.com/adafruit/Adafruit_CircuitPython_datetime/issues/22
if as_str[-1] == "Z":
print(f"Warning: applying workaround for unsupported datetime format {as_str}")
as_str = as_str[:-1] + "-00:00"
return adafruit_datetime.datetime.fromisoformat(as_str)
@staticmethod
def datetime_to_time_str(datetime_obj: adafruit_datetime.datetime) -> str:
hour = datetime_obj.hour
minute = datetime_obj.minute
meridian = "a"
if hour == 0:
hour = 12
elif hour == 12:
meridian = "p"
elif hour > 12:
hour -= 12
meridian = "p"
return f"{hour}:{minute:02}{meridian}"
@staticmethod
def format_battery_percent(percent: int) -> str:
return f"{percent}%"