Skip to content

Commit

Permalink
Make DateTime timezone aware
Browse files Browse the repository at this point in the history
The optional timezeon parameter requires the pytz library.
  • Loading branch information
t-wissmann committed May 16, 2023
1 parent 91ec0d1 commit a50f18b
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions barpyrus/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import subprocess
import time
import datetime
import sys
import select
import os
Expand Down Expand Up @@ -132,14 +133,32 @@ def on_click(self, button):


class DateTime(Label):
def __init__(self,time_format = '%H:%M, %Y-%m-%d'):
"""
This is a label widget that displays the current date/time
in the specified format. The optional parameter 'timezone'
expects the string representation of a timezone (e.g.
'Europe/Berlin'), and requires that the pytz library is installed.
"""
def __init__(self, time_format = '%H:%M, %Y-%m-%d', timezone=None):
super(DateTime,self).__init__('')
self.timer_interval = 1
self.time_format = time_format
self.last_time = ''
self.tz_name = timezone
self.tz = None
if self.tz_name:
# if tz_name is provided, we need the 'pytz' library:
import pytz
self.tz = pytz.timezone(self.tz_name)
# set label
self.timeout()

def timeout(self):
self.label = time.strftime(self.time_format)
if self.tz:
t = datetime.datetime.now(self.tz)
else:
t = datetime.datetime.now()
self.label = t.strftime(self.time_format)
if_changed = (self.label != self.last_time)
self.last_time = self.label
return if_changed
Expand Down

0 comments on commit a50f18b

Please sign in to comment.