Skip to content

Commit 310c60a

Browse files
Move mpos.time.refresh_timezone_preference() to TimeZone.refresh_timezone_preference()
1 parent a2ffd35 commit 310c60a

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

internal_filesystem/builtin/apps/com.micropythonos.settings/assets/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def getIntent(self):
4242
# Basic settings, alphabetically:
4343
{"title": "Light/Dark Theme", "key": "theme_light_dark", "ui": "radiobuttons", "ui_options": [("Light", "light"), ("Dark", "dark")], "changed_callback": self.theme_changed},
4444
{"title": "Theme Color", "key": "theme_primary_color", "placeholder": "HTML hex color, like: EC048C", "ui": "dropdown", "ui_options": theme_colors, "changed_callback": self.theme_changed},
45-
{"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()},
45+
{"title": "Timezone", "key": "timezone", "ui": "dropdown", "ui_options": [(tz, tz) for tz in TimeZone.get_timezones()], "changed_callback": lambda *args: TimeZone.refresh_timezone_preference()},
4646
# Advanced settings, alphabetically:
4747
{"title": "Auto Start App", "key": "auto_start_app", "ui": "radiobuttons", "ui_options": [(app.name, app.fullname) for app in AppManager.get_app_list()]},
4848
{"title": "Check IMU Calibration", "key": "check_imu_calibration", "ui": "activity", "activity_class": CheckIMUCalibrationActivity},

internal_filesystem/lib/mpos/time.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import time
2-
from . import config
32
from .time_zone import TimeZone
43

54
import localPTZtime
65

7-
timezone_preference = None
8-
96
def epoch_seconds():
107
import sys
118
if sys.platform == "esp32":
@@ -23,22 +20,14 @@ def sync_time():
2320
print('Syncing time with', ntptime.host)
2421
ntptime.settime() # Fetch and set time (in UTC)
2522
print("Time sync'ed successfully")
26-
refresh_timezone_preference() # if the time was sync'ed, then it needs refreshing
23+
TimeZone.refresh_timezone_preference() # if the time was sync'ed, then it needs refreshing
2724
except Exception as e:
2825
print('Failed to sync time:', e)
2926

30-
def refresh_timezone_preference():
31-
global timezone_preference
32-
prefs = config.SharedPreferences("com.micropythonos.settings")
33-
timezone_preference = prefs.get_string("timezone")
34-
if not timezone_preference:
35-
timezone_preference = "Etc/GMT" # Use a default value so that it doesn't refresh every time the time is requested
36-
3727
def localtime():
38-
global timezone_preference
39-
if not timezone_preference: # if it's the first time, then it needs refreshing
40-
refresh_timezone_preference()
41-
ptz = TimeZone.timezone_to_posix_time_zone(timezone_preference)
28+
if not TimeZone.timezone_preference: # if it's the first time, then it needs refreshing
29+
TimeZone.refresh_timezone_preference()
30+
ptz = TimeZone.timezone_to_posix_time_zone(TimeZone.timezone_preference)
4231
t = time.time()
4332
try:
4433
localtime = localPTZtime.tztime(t, ptz)

internal_filesystem/lib/mpos/time_zone.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from .time_zones import TIME_ZONE_MAP
2+
from . import config
23

34

45
class TimeZone:
56
"""Timezone utility class for converting and managing timezone information."""
67

8+
timezone_preference = None
9+
710
@staticmethod
811
def timezone_to_posix_time_zone(timezone):
912
"""
@@ -28,3 +31,12 @@ def get_timezones():
2831
list: List of timezone names (e.g., ['Africa/Abidjan', 'Africa/Accra', ...]).
2932
"""
3033
return sorted(TIME_ZONE_MAP.keys()) # even though they are defined alphabetical, the order isn't maintained in MicroPython
34+
35+
@staticmethod
36+
def refresh_timezone_preference():
37+
"""
38+
Refresh the timezone preference from SharedPreferences.
39+
"""
40+
TimeZone.timezone_preference = config.SharedPreferences("com.micropythonos.settings").get_string("timezone")
41+
if not TimeZone.timezone_preference:
42+
TimeZone.timezone_preference = "Etc/GMT" # Use a default value so that it doesn't refresh every time the time is requested

0 commit comments

Comments
 (0)