You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Move mpos.time.refresh_timezone_preference() to TimeZone.refresh_timezone_preference()
This commit is contained in:
@@ -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},
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user