forked from MicroPythonOS/MicroPythonOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_zone.py
More file actions
42 lines (33 loc) · 1.46 KB
/
time_zone.py
File metadata and controls
42 lines (33 loc) · 1.46 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
from .time_zones import TIME_ZONE_MAP
from . import config
class TimeZone:
"""Timezone utility class for converting and managing timezone information."""
timezone_preference = None
@staticmethod
def timezone_to_posix_time_zone(timezone):
"""
Convert a timezone name to its POSIX timezone string.
Args:
timezone (str or None): Timezone name (e.g., 'Africa/Abidjan') or None.
Returns:
str: POSIX timezone string (e.g., 'GMT0'). Returns 'GMT0' if timezone is None or not found.
"""
if timezone is None or timezone not in TIME_ZONE_MAP:
return "GMT0"
return TIME_ZONE_MAP[timezone]
@staticmethod
def get_timezones():
"""
Get a list of all available timezone names.
Returns:
list: List of timezone names (e.g., ['Africa/Abidjan', 'Africa/Accra', ...]).
"""
return sorted(TIME_ZONE_MAP.keys()) # even though they are defined alphabetical, the order isn't maintained in MicroPython
@staticmethod
def refresh_timezone_preference():
"""
Refresh the timezone preference from SharedPreferences.
"""
TimeZone.timezone_preference = config.SharedPreferences("com.micropythonos.settings").get_string("timezone")
if not TimeZone.timezone_preference:
TimeZone.timezone_preference = "Etc/GMT" # Use a default value so that it doesn't refresh every time the time is requested