Skip to content

Commit

Permalink
Issue 666: fix for parsing summer time (#667)
Browse files Browse the repository at this point in the history
* rollback change for localize

* add more fixes

* update tests

* apply pep8

* avoid pytz
  • Loading branch information
jkotan authored Jul 3, 2024
1 parent fc18eb1 commit deea79e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2024-07-03 Jan Kotanski <jankotan@gmail.com>
* fix wrong parsing of summer time (#666)
* tagged as v4.6.2

2024-07-02 Jan Kotanski <jankotan@gmail.com>
* fix pilc file name (#663)
* tagged as v4.6.1
Expand Down
15 changes: 9 additions & 6 deletions nxstools/filewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import weakref
import time
import pytz
import datetime
import threading
import numpy
Expand Down Expand Up @@ -528,16 +527,20 @@ def currenttime(cls):
:rtype: :obj:`str`
"""
tzone = time.tzname[0]
fmt = '%Y-%m-%dT%H:%M:%S.%f%z'
try:
tz = pytz.timezone(tzone)
if sys.version_info >= (3, 9):
import zoneinfo
tz = zoneinfo.ZoneInfo(tzone)
starttime = datetime.datetime.now().replace(tzinfo=tz)
else:
import pytz
tz = pytz.timezone(tzone)
starttime = tz.localize(datetime.datetime.now())
except Exception:
import tzlocal
tz = tzlocal.get_localzone()
fmt = '%Y-%m-%dT%H:%M:%S.%f%z'
if sys.version_info > (3, 6):
starttime = datetime.datetime.now().replace(tzinfo=tz)
else:
starttime = tz.localize(datetime.datetime.now())
return str(starttime.strftime(fmt))

def default_field(self):
Expand Down
16 changes: 10 additions & 6 deletions nxstools/nxsfileinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import stat
import re
import time
import pytz
import datetime
import pwd
import grp
Expand Down Expand Up @@ -3327,17 +3326,22 @@ def isotime(self, tme):
:rtype: :obj:`str`
"""
tzone = time.tzname[0]
fmt = '%Y-%m-%dT%H:%M:%S.%f%z'
try:
tz = pytz.timezone(tzone)
if sys.version_info >= (3, 9):
import zoneinfo
tz = zoneinfo.ZoneInfo(tzone)
starttime = \
datetime.datetime.fromtimestamp(tme).replace(tzinfo=tz)
else:
import pytz
tz = pytz.timezone(tzone)
starttime = tz.localize(datetime.datetime.fromtimestamp(tme))
except Exception:
import tzlocal
tz = tzlocal.get_localzone()
fmt = '%Y-%m-%dT%H:%M:%S.%f%z'
if sys.version_info > (3, 6):
starttime = \
datetime.datetime.fromtimestamp(tme).replace(tzinfo=tz)
else:
starttime = tz.localize(datetime.datetime.fromtimestamp(tme))
return str(starttime.strftime(fmt))

def filterout(self, fpath, filters):
Expand Down
15 changes: 9 additions & 6 deletions nxstools/nxsfileparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import xml.etree.ElementTree as et
import numpy as np
import math
import pytz
import time
import dateutil.parser
import re
Expand Down Expand Up @@ -105,18 +104,22 @@ def isoDate(text):
pass
if not result:
date = dateutil.parser.parse(text)
fmt = '%Y-%m-%dT%H:%M:%S.%f%z'
if date.tzinfo is None:
tzone = time.tzname[0]
try:
tz = pytz.timezone(tzone)
if sys.version_info >= (3, 9):
import zoneinfo
tz = zoneinfo.ZoneInfo(tzone)
date = date.replace(tzinfo=tz)
else:
import pytz
tz = pytz.timezone(tzone)
date = tz.localize(date)
except Exception:
import tzlocal
tz = tzlocal.get_localzone()
if sys.version_info > (3, 6):
date = date.replace(tzinfo=tz)
else:
date = tz.localize(date)
fmt = '%Y-%m-%dT%H:%M:%S.%f%z'
result = str(date.strftime(fmt))
except Exception:
result = text
Expand Down
2 changes: 1 addition & 1 deletion nxstools/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
""" NXS tools release version"""

#: (:obj:`str`) package version
__version__ = "4.6.1"
__version__ = "4.6.2"

0 comments on commit deea79e

Please sign in to comment.