Skip to content

Commit b60b6d6

Browse files
committed
fix(widget/clock): improve locale handling for time formatting
1 parent 39927fc commit b60b6d6

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/core/widgets/yasb/clock.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from tzlocal import get_localzone_name
99
from itertools import cycle
1010
from core.utils.widgets.animation_manager import AnimationManager
11+
import locale
1112

1213
class ClockWidget(BaseWidget):
1314
validation_schema = VALIDATION_SCHEMA
@@ -35,9 +36,6 @@ def __init__(
3536
self._label_content = label
3637
self._padding = container_padding
3738
self._label_alt_content = label_alt
38-
if self._locale:
39-
import locale
40-
locale.setlocale(locale.LC_TIME, self._locale)
4139

4240
# Construct container
4341
self._widget_container_layout: QHBoxLayout = QHBoxLayout()
@@ -114,7 +112,9 @@ def _update_label(self):
114112
label_parts = re.split('(<span.*?>.*?</span>)', active_label_content)
115113
label_parts = [part for part in label_parts if part]
116114
widget_index = 0
117-
115+
if self._locale:
116+
org_locale = locale.getlocale(locale.LC_TIME)
117+
118118
for part in label_parts:
119119
part = part.strip()
120120
if part and widget_index < len(active_widgets) and isinstance(active_widgets[widget_index], QLabel):
@@ -123,6 +123,8 @@ def _update_label(self):
123123
active_widgets[widget_index].setText(icon)
124124
else:
125125
try:
126+
if self._locale:
127+
locale.setlocale(locale.LC_TIME, self._locale)
126128
datetime_format_search = re.search('\\{(.*)}', part)
127129
datetime_format_str = datetime_format_search.group()
128130
datetime_format = datetime_format_search.group(1)
@@ -132,7 +134,9 @@ def _update_label(self):
132134
format_label_content = part
133135
active_widgets[widget_index].setText(format_label_content)
134136
widget_index += 1
135-
137+
if self._locale:
138+
locale.setlocale(locale.LC_TIME, org_locale)
139+
136140
def _next_timezone(self):
137141
self._active_tz = next(self._timezones)
138142
if self._tooltip:

0 commit comments

Comments
 (0)