Skip to content

Commit

Permalink
fix(widget/clock): improve locale handling for time formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
amnweb committed Jan 20, 2025
1 parent 39927fc commit b60b6d6
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/core/widgets/yasb/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from tzlocal import get_localzone_name
from itertools import cycle
from core.utils.widgets.animation_manager import AnimationManager
import locale

class ClockWidget(BaseWidget):
validation_schema = VALIDATION_SCHEMA
Expand Down Expand Up @@ -35,9 +36,6 @@ def __init__(
self._label_content = label
self._padding = container_padding
self._label_alt_content = label_alt
if self._locale:
import locale
locale.setlocale(locale.LC_TIME, self._locale)

# Construct container
self._widget_container_layout: QHBoxLayout = QHBoxLayout()
Expand Down Expand Up @@ -114,7 +112,9 @@ def _update_label(self):
label_parts = re.split('(<span.*?>.*?</span>)', active_label_content)
label_parts = [part for part in label_parts if part]
widget_index = 0

if self._locale:
org_locale = locale.getlocale(locale.LC_TIME)

for part in label_parts:
part = part.strip()
if part and widget_index < len(active_widgets) and isinstance(active_widgets[widget_index], QLabel):
Expand All @@ -123,6 +123,8 @@ def _update_label(self):
active_widgets[widget_index].setText(icon)
else:
try:
if self._locale:
locale.setlocale(locale.LC_TIME, self._locale)
datetime_format_search = re.search('\\{(.*)}', part)
datetime_format_str = datetime_format_search.group()
datetime_format = datetime_format_search.group(1)
Expand All @@ -132,7 +134,9 @@ def _update_label(self):
format_label_content = part
active_widgets[widget_index].setText(format_label_content)
widget_index += 1

if self._locale:
locale.setlocale(locale.LC_TIME, org_locale)

def _next_timezone(self):
self._active_tz = next(self._timezones)
if self._tooltip:
Expand Down

0 comments on commit b60b6d6

Please sign in to comment.