From 310c60ad40206e4b804a2392341e804875af7038 Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Sun, 25 Jan 2026 15:42:38 +0100 Subject: [PATCH] Move mpos.time.refresh_timezone_preference() to TimeZone.refresh_timezone_preference() --- .../assets/settings.py | 2 +- internal_filesystem/lib/mpos/time.py | 19 ++++--------------- internal_filesystem/lib/mpos/time_zone.py | 12 ++++++++++++ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/internal_filesystem/builtin/apps/com.micropythonos.settings/assets/settings.py b/internal_filesystem/builtin/apps/com.micropythonos.settings/assets/settings.py index 6b87bbad..3129694f 100644 --- a/internal_filesystem/builtin/apps/com.micropythonos.settings/assets/settings.py +++ b/internal_filesystem/builtin/apps/com.micropythonos.settings/assets/settings.py @@ -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 TimeZone.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: TimeZone.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 AppManager.get_app_list()]}, {"title": "Check IMU Calibration", "key": "check_imu_calibration", "ui": "activity", "activity_class": CheckIMUCalibrationActivity}, diff --git a/internal_filesystem/lib/mpos/time.py b/internal_filesystem/lib/mpos/time.py index 8f30f2ff..d3faacd2 100644 --- a/internal_filesystem/lib/mpos/time.py +++ b/internal_filesystem/lib/mpos/time.py @@ -1,11 +1,8 @@ import time -from . import config from .time_zone import TimeZone import localPTZtime -timezone_preference = None - def epoch_seconds(): import sys if sys.platform == "esp32": @@ -23,22 +20,14 @@ def sync_time(): print('Syncing time with', ntptime.host) ntptime.settime() # Fetch and set time (in UTC) print("Time sync'ed successfully") - refresh_timezone_preference() # if the time was sync'ed, then it needs refreshing + TimeZone.refresh_timezone_preference() # if the time was sync'ed, then it needs refreshing except Exception as e: print('Failed to sync time:', e) -def refresh_timezone_preference(): - global timezone_preference - prefs = config.SharedPreferences("com.micropythonos.settings") - timezone_preference = prefs.get_string("timezone") - if not timezone_preference: - timezone_preference = "Etc/GMT" # Use a default value so that it doesn't refresh every time the time is requested - def localtime(): - global timezone_preference - if not timezone_preference: # if it's the first time, then it needs refreshing - refresh_timezone_preference() - ptz = TimeZone.timezone_to_posix_time_zone(timezone_preference) + if not TimeZone.timezone_preference: # if it's the first time, then it needs refreshing + TimeZone.refresh_timezone_preference() + ptz = TimeZone.timezone_to_posix_time_zone(TimeZone.timezone_preference) t = time.time() try: localtime = localPTZtime.tztime(t, ptz) diff --git a/internal_filesystem/lib/mpos/time_zone.py b/internal_filesystem/lib/mpos/time_zone.py index d364cc6f..876ccbb3 100644 --- a/internal_filesystem/lib/mpos/time_zone.py +++ b/internal_filesystem/lib/mpos/time_zone.py @@ -1,9 +1,12 @@ 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): """ @@ -28,3 +31,12 @@ class TimeZone: 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