Skip to content

Commit

Permalink
Fix DateTimeError for non-valid timezones (#2395)
Browse files Browse the repository at this point in the history
* Fix DateTimeError for non-valid timezones

* Changelog

* Added doctest
  • Loading branch information
xispa authored Sep 29, 2023
1 parent f498e5d commit 0bc289d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
2.5.0 (unreleased)
------------------

- #2395 Fix DateTimeError for non-valid/old timezones
- #2396 Add after sequential transition event handler
- #2394 Ajax support for transitions retract and retest
- #2393 Allow empty analysis method selection
Expand Down
7 changes: 6 additions & 1 deletion src/senaite/core/api/dtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from bika.lims.api import get_tool
from DateTime import DateTime
from DateTime.DateTime import DateError
from DateTime.DateTime import DateTimeError
from DateTime.DateTime import SyntaxError
from DateTime.DateTime import TimeError
from zope.i18n import translate
Expand Down Expand Up @@ -139,7 +140,11 @@ def to_DT(dt):
except (SyntaxError, IndexError):
return None
elif is_dt(dt):
return DateTime(dt.isoformat())
try:
# XXX Why do this instead of DateTime(dt)?
return DateTime(dt.isoformat())
except DateTimeError:
return DateTime(dt)
elif is_d(dt):
dt = datetime(dt.year, dt.month, dt.day)
return DateTime(dt.isoformat())
Expand Down
22 changes: 22 additions & 0 deletions src/senaite/core/tests/doctests/API_datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ Timezone aware datetimes are converterd to `GMT+<tzoffset>`
>>> dtime.to_DT(local_dt)
DateTime('2021/08/01 12:00:00 GMT+2')

Old dates with obsolete timezones (e.g. LMT) are converted as well

>>> old_dt = datetime(1682, 8, 16, 2, 44, 52)
>>> old_dt = dtime.to_zone(old_dt, "Pacific/Port_Moresby")
>>> old_dt
datetime.datetime(1682, 8, 16, 2, 44, 52, tzinfo=<DstTzInfo 'Pacific/Port_Moresby' LMT+9:49:00 STD>)
>>> old_dt.utcoffset().total_seconds()
35340.0
>>> old_DT = dtime.to_DT(old_dt)
>>> old_DT
DateTime('1682/08/16 02:44:52 Pacific/Port_Moresby')
>>> old_DT.tzoffset()
35340

Convert to datetime
...................
Expand Down Expand Up @@ -247,6 +260,12 @@ Get the timezone from `datetime.date` objects:
>>> dtime.get_timezone(dt.date)
'Etc/GMT'

We can even get the obsolete timezone that was applying to an old date:

>>> old_dt = datetime(1682, 8, 16, 2, 44, 54)
>>> old_dt = dtime.to_zone(old_dt, "Pacific/Port_Moresby")
>>> dtime.get_timezone(old_dt)
'LMT'

Get the timezone info
.....................
Expand Down Expand Up @@ -337,6 +356,9 @@ Check if timezone is valid
>>> dtime.is_valid_timezone("CEST")
False

>>> dtime.is_valid_timezone("LMT")
False


Get the default timezone from the system
........................................
Expand Down

0 comments on commit 0bc289d

Please sign in to comment.