Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DateTimeError for non-valid timezones #2395

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure anymore, but I guess it was also related to the timezone, e.g. GMT vs. Etc/GMT or CET etc.

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