Add TimeZone framework

This commit is contained in:
Thomas Farstrike
2026-01-24 19:31:54 +01:00
parent 176d4cc33c
commit ba21d86bd7
5 changed files with 39 additions and 29 deletions
@@ -1,6 +1,6 @@
import lvgl as lv
from mpos import Intent, PackageManager, SettingActivity, SettingsActivity
from mpos import Intent, PackageManager, SettingActivity, SettingsActivity, TimeZone
from calibrate_imu import CalibrateIMUActivity
from check_imu_calibration import CheckIMUCalibrationActivity
@@ -42,7 +42,7 @@ class Settings(SettingsActivity):
# Basic settings, alphabetically:
{"title": "Light/Dark Theme", "key": "theme_light_dark", "ui": "radiobuttons", "ui_options": [("Light", "light"), ("Dark", "dark")], "changed_callback": self.theme_changed},
{"title": "Theme Color", "key": "theme_primary_color", "placeholder": "HTML hex color, like: EC048C", "ui": "dropdown", "ui_options": theme_colors, "changed_callback": self.theme_changed},
{"title": "Timezone", "key": "timezone", "ui": "dropdown", "ui_options": [(tz, tz) for tz in mpos.time.get_timezones()], "changed_callback": lambda *args: mpos.time.refresh_timezone_preference()},
{"title": "Timezone", "key": "timezone", "ui": "dropdown", "ui_options": [(tz, tz) for tz in TimeZone.get_timezones()], "changed_callback": lambda *args: mpos.time.refresh_timezone_preference()},
# Advanced settings, alphabetically:
{"title": "Auto Start App", "key": "auto_start_app", "ui": "radiobuttons", "ui_options": [(app.name, app.fullname) for app in PackageManager.get_app_list()]},
{"title": "Check IMU Calibration", "key": "check_imu_calibration", "ui": "activity", "activity_class": CheckIMUCalibrationActivity},
+4 -1
View File
@@ -13,6 +13,7 @@ from .net.download_manager import DownloadManager
from .task_manager import TaskManager
from .camera_manager import CameraManager
from .sensor_manager import SensorManager
from .time_zone import TimeZone
# Common activities
from .app.activities.chooser import ChooserActivity
@@ -87,5 +88,7 @@ __all__ = [
"get_all_widgets_with_text",
# Submodules
"apps", "ui", "config", "net", "content", "time", "sensor_manager",
"camera_manager", "sdcard", "battery_voltage", "audio", "hardware", "bootloader"
"camera_manager", "sdcard", "battery_voltage", "audio", "hardware", "bootloader",
# Timezone utilities
"TimeZone"
]
+2 -25
View File
@@ -1,6 +1,6 @@
import time
from . import config
from .timezones import TIMEZONE_MAP
from .time_zone import TimeZone
import localPTZtime
@@ -38,7 +38,7 @@ def localtime():
global timezone_preference
if not timezone_preference: # if it's the first time, then it needs refreshing
refresh_timezone_preference()
ptz = timezone_to_posix_time_zone(timezone_preference)
ptz = TimeZone.timezone_to_posix_time_zone(timezone_preference)
t = time.time()
try:
localtime = localPTZtime.tztime(t, ptz)
@@ -47,26 +47,3 @@ def localtime():
return time.localtime()
return localtime
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 TIMEZONE_MAP:
return "GMT0"
return TIMEZONE_MAP[timezone]
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(TIMEZONE_MAP.keys()) # even though they are defined alphabetical, the order isn't maintained in MicroPython
+30
View File
@@ -0,0 +1,30 @@
from .time_zones import TIME_ZONE_MAP
class TimeZone:
"""Timezone utility class for converting and managing timezone information."""
@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
@@ -2,7 +2,7 @@
# and then asked an LLM to shorten the list (otherwise it's a huge scroll)
# by keeping only the commonly used cities.
TIMEZONE_MAP = {
TIME_ZONE_MAP = {
"Africa/Abidjan": "GMT0", # West Africa, GMT0
"Africa/Accra": "GMT0", # Ghanas capital
"Africa/Addis_Ababa": "EAT-3", # Ethiopias capital