Skip to content

Commit ba21d86

Browse files
Add TimeZone framework
1 parent 176d4cc commit ba21d86

File tree

5 files changed

+39
-29
lines changed

5 files changed

+39
-29
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import lvgl as lv
22

3-
from mpos import Intent, PackageManager, SettingActivity, SettingsActivity
3+
from mpos import Intent, PackageManager, SettingActivity, SettingsActivity, TimeZone
44

55
from calibrate_imu import CalibrateIMUActivity
66
from check_imu_calibration import CheckIMUCalibrationActivity
@@ -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 mpos.time.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: mpos.time.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 PackageManager.get_app_list()]},
4848
{"title": "Check IMU Calibration", "key": "check_imu_calibration", "ui": "activity", "activity_class": CheckIMUCalibrationActivity},

internal_filesystem/lib/mpos/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .task_manager import TaskManager
1414
from .camera_manager import CameraManager
1515
from .sensor_manager import SensorManager
16+
from .time_zone import TimeZone
1617

1718
# Common activities
1819
from .app.activities.chooser import ChooserActivity
@@ -87,5 +88,7 @@
8788
"get_all_widgets_with_text",
8889
# Submodules
8990
"apps", "ui", "config", "net", "content", "time", "sensor_manager",
90-
"camera_manager", "sdcard", "battery_voltage", "audio", "hardware", "bootloader"
91+
"camera_manager", "sdcard", "battery_voltage", "audio", "hardware", "bootloader",
92+
# Timezone utilities
93+
"TimeZone"
9194
]

internal_filesystem/lib/mpos/time.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import time
22
from . import config
3-
from .timezones import TIMEZONE_MAP
3+
from .time_zone import TimeZone
44

55
import localPTZtime
66

@@ -38,7 +38,7 @@ def localtime():
3838
global timezone_preference
3939
if not timezone_preference: # if it's the first time, then it needs refreshing
4040
refresh_timezone_preference()
41-
ptz = timezone_to_posix_time_zone(timezone_preference)
41+
ptz = TimeZone.timezone_to_posix_time_zone(timezone_preference)
4242
t = time.time()
4343
try:
4444
localtime = localPTZtime.tztime(t, ptz)
@@ -47,26 +47,3 @@ def localtime():
4747
return time.localtime()
4848
return localtime
4949

50-
def timezone_to_posix_time_zone(timezone):
51-
"""
52-
Convert a timezone name to its POSIX timezone string.
53-
54-
Args:
55-
timezone (str or None): Timezone name (e.g., 'Africa/Abidjan') or None.
56-
57-
Returns:
58-
str: POSIX timezone string (e.g., 'GMT0'). Returns 'GMT0' if timezone is None or not found.
59-
"""
60-
if timezone is None or timezone not in TIMEZONE_MAP:
61-
return "GMT0"
62-
return TIMEZONE_MAP[timezone]
63-
64-
def get_timezones():
65-
"""
66-
Get a list of all available timezone names.
67-
68-
Returns:
69-
list: List of timezone names (e.g., ['Africa/Abidjan', 'Africa/Accra', ...]).
70-
"""
71-
return sorted(TIMEZONE_MAP.keys()) # even though they are defined alphabetical, the order isn't maintained in MicroPython
72-
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from .time_zones import TIME_ZONE_MAP
2+
3+
4+
class TimeZone:
5+
"""Timezone utility class for converting and managing timezone information."""
6+
7+
@staticmethod
8+
def timezone_to_posix_time_zone(timezone):
9+
"""
10+
Convert a timezone name to its POSIX timezone string.
11+
12+
Args:
13+
timezone (str or None): Timezone name (e.g., 'Africa/Abidjan') or None.
14+
15+
Returns:
16+
str: POSIX timezone string (e.g., 'GMT0'). Returns 'GMT0' if timezone is None or not found.
17+
"""
18+
if timezone is None or timezone not in TIME_ZONE_MAP:
19+
return "GMT0"
20+
return TIME_ZONE_MAP[timezone]
21+
22+
@staticmethod
23+
def get_timezones():
24+
"""
25+
Get a list of all available timezone names.
26+
27+
Returns:
28+
list: List of timezone names (e.g., ['Africa/Abidjan', 'Africa/Accra', ...]).
29+
"""
30+
return sorted(TIME_ZONE_MAP.keys()) # even though they are defined alphabetical, the order isn't maintained in MicroPython

internal_filesystem/lib/mpos/timezones.py renamed to internal_filesystem/lib/mpos/time_zones.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# and then asked an LLM to shorten the list (otherwise it's a huge scroll)
33
# by keeping only the commonly used cities.
44

5-
TIMEZONE_MAP = {
5+
TIME_ZONE_MAP = {
66
"Africa/Abidjan": "GMT0", # West Africa, GMT0
77
"Africa/Accra": "GMT0", # Ghana’s capital
88
"Africa/Addis_Ababa": "EAT-3", # Ethiopia’s capital

0 commit comments

Comments
 (0)