Skip to content

Commit 392b91f

Browse files
authored
Merge pull request #174 from mathoudebine/feature/allow-custom-date-time-formatting
Allow custom date/time formatting from babel module
2 parents 99d656d + b11bdba commit 392b91f

File tree

4 files changed

+88
-71
lines changed

4 files changed

+88
-71
lines changed

library/stats.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2222

2323
import datetime
24+
import locale
2425
import math
2526
import os
2627
import platform
2728
import sys
2829

30+
import babel.dates
2931
from psutil._common import bytes2human
3032

3133
import library.config as config
@@ -756,9 +758,17 @@ class Date:
756758
@staticmethod
757759
def stats():
758760
date_now = datetime.datetime.now()
761+
762+
if platform.system() == "Windows":
763+
# Windows does not have LC_TIME environment variable, use deprecated getdefaultlocale() that returns language code following RFC 1766
764+
lc_time = locale.getdefaultlocale()[0]
765+
else:
766+
lc_time = babel.dates.LC_TIME
767+
759768
if config.THEME_DATA['STATS']['DATE']['DAY']['TEXT'].get("SHOW", False):
769+
date_format = config.THEME_DATA['STATS']['DATE']['DAY']['TEXT'].get("FORMAT", 'medium')
760770
display.lcd.DisplayText(
761-
text=f"{date_now.strftime('%x')}",
771+
text=f"{babel.dates.format_date(date_now, format=date_format, locale=lc_time)}",
762772
x=config.THEME_DATA['STATS']['DATE']['DAY']['TEXT'].get("X", 0),
763773
y=config.THEME_DATA['STATS']['DATE']['DAY']['TEXT'].get("Y", 0),
764774
font=config.THEME_DATA['STATS']['DATE']['DAY']['TEXT'].get("FONT",
@@ -773,8 +783,9 @@ def stats():
773783
)
774784

775785
if config.THEME_DATA['STATS']['DATE']['HOUR']['TEXT'].get("SHOW", False):
786+
time_format = config.THEME_DATA['STATS']['DATE']['HOUR']['TEXT'].get("FORMAT", 'medium')
776787
display.lcd.DisplayText(
777-
text=f"{date_now.strftime('%X')}",
788+
text=f"{babel.dates.format_time(date_now, format=time_format, locale=lc_time)}",
778789
x=config.THEME_DATA['STATS']['DATE']['HOUR']['TEXT'].get("X", 0),
779790
y=config.THEME_DATA['STATS']['DATE']['HOUR']['TEXT'].get("Y", 0),
780791
font=config.THEME_DATA['STATS']['DATE']['HOUR']['TEXT'].get("FONT",

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PyYAML~=6.0 # For themes files
55
psutil~=5.9.4 # CPU / disk / network metrics
66
GPUtil~=1.4.0 # Nvidia GPU
77
pystray~=0.19.4 # Tray icon (all OS)
8+
babel~=2.11.0 # Date/time formatting
89

910
# Following packages are for AMD GPU on Linux
1011
# Note: you may need to manually install Cython first

res/themes/LandscapeEarth/theme.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ STATS:
3939
TEXT:
4040
SHOW: True
4141
SHOW_UNIT: True
42-
X: 50
42+
X: 48
4343
Y: 281
4444
FONT: generale-mono/GeneraleMonoA.ttf
4545
FONT_SIZE: 16
@@ -106,6 +106,7 @@ STATS:
106106
INTERVAL: 1
107107
DAY:
108108
TEXT:
109+
FORMAT: medium
109110
SHOW: True
110111
X: 14
111112
Y: 64
@@ -115,10 +116,11 @@ STATS:
115116
BACKGROUND_IMAGE: background.png
116117
HOUR:
117118
TEXT:
119+
FORMAT: short
118120
SHOW: True
119121
X: 14
120122
Y: 30
121123
FONT: generale-mono/GeneraleMonoA.ttf
122-
FONT_SIZE: 20
124+
FONT_SIZE: 26
123125
FONT_COLOR: 255, 255, 255
124126
BACKGROUND_COLOR: 16, 28, 57

0 commit comments

Comments
 (0)