2026-01-24 19:31:54 +01:00
|
|
|
from .time_zones import TIME_ZONE_MAP
|
2026-01-25 15:42:38 +01:00
|
|
|
from . import config
|
2026-01-24 19:31:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TimeZone:
|
|
|
|
|
"""Timezone utility class for converting and managing timezone information."""
|
|
|
|
|
|
2026-01-25 15:42:38 +01:00
|
|
|
timezone_preference = None
|
|
|
|
|
|
2026-01-24 19:31:54 +01:00
|
|
|
@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
|
2026-01-25 15:42:38 +01:00
|
|
|
|
|
|
|
|
@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
|