Skip to content

Commit

Permalink
Use a different library in pywws.timezone
Browse files Browse the repository at this point in the history
The pytz library is deprecated. Replacing it with dateutil.tz makes
pywws.timezone a lot simpler as dateutil.tz solves many of the same
problems.

Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed Mar 29, 2022
1 parent 31519ad commit 11bd47f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 39 deletions.
4 changes: 2 additions & 2 deletions setup.py
@@ -1,6 +1,6 @@
# pywws - Python software for USB Wireless Weather Stations
# http://github.com/jim-easterbrook/pywws
# Copyright (C) 2008-18 pywws contributors
# Copyright (C) 2008-22 pywws contributors

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -208,7 +208,7 @@ def run(self):
'pywws-version = pywws.version:main',
],
},
install_requires = ['tzlocal'],
install_requires = ['python-dateutil'],
extras_require = {
'daemon' : ['python-daemon == 2.1.2'],
'sftp' : ['paramiko', 'pycrypto'],
Expand Down
6 changes: 3 additions & 3 deletions src/pywws/__init__.py
@@ -1,3 +1,3 @@
__version__ = '21.11.0'
_release = '1691'
_commit = '87168bf'
__version__ = '22.3.0'
_release = '1692'
_commit = '31519ad'
39 changes: 5 additions & 34 deletions src/pywws/timezone.py
Expand Up @@ -2,7 +2,7 @@

# pywws - Python software for USB Wireless Weather Stations
# http://github.com/jim-easterbrook/pywws
# Copyright (C) 2008-21 pywws contributors
# Copyright (C) 2008-22 pywws contributors

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -41,49 +41,24 @@
import logging
import sys

import tzlocal
import dateutil.tz

from pywws.constants import DAY, HOUR

logger = logging.getLogger(__name__)


class _TimeZone(object):
class _UTC(datetime.tzinfo):
_offset = datetime.timedelta(0)

def utcoffset(self, dt):
return self._offset

def dst(self, dt):
return self._offset

def tzname(self, dt):
return 'UTC'


def __init__(self, tz_name=None):
if tz_name:
# use a named time zone instead of system default, for testing
import pytz
self.local = pytz.timezone(tz_name)
else:
self.local = tzlocal.get_localzone()
self.local = dateutil.tz.gettz(tz_name)
logger.info('Using timezone "{!s}"'.format(self.local))
self._using_pytz = hasattr(self.local, 'localize')
if sys.version_info >= (3, 2):
self.utc = datetime.timezone.utc
else:
self.utc = self._UTC()
self.utc = dateutil.tz.UTC

def local_to_utc(self, dt):
"""Convert a local time (with or without tzinfo) to UTC without
tzinfo, as used for pywws timestamps."""
if dt.tzinfo is None:
if self._using_pytz:
dt = self.local.localize(dt)
else:
dt = dt.replace(tzinfo=self.local)
dt = dt.replace(tzinfo=self.local)
return dt.astimezone(self.utc).replace(tzinfo=None)

def utc_to_local(self, dt):
Expand Down Expand Up @@ -141,15 +116,11 @@ def _dst_transitions(self):
dst = day.dst()
for d in range(365):
day -= DAY
if self._using_pytz:
day = self.local.normalize(day)
if day.dst() == dst:
continue
hour = day
for h in range(25):
hour += HOUR
if self._using_pytz:
hour = self.local.normalize(hour)
if hour.dst() == dst:
yield hour
break
Expand Down

0 comments on commit 11bd47f

Please sign in to comment.