Move mpos.time.refresh_timezone_preference() to TimeZone.refresh_timezone_preference()

This commit is contained in:
Thomas Farstrike
2026-01-25 15:42:38 +01:00
parent a2ffd35997
commit 310c60ad40
3 changed files with 17 additions and 16 deletions
@@ -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},
+4 -15
View File
@@ -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)
+12
View File
@@ -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