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

Fix DateTimeError for non-valid timezones #2395

merged 4 commits into from
Sep 29, 2023

Conversation

xispa
Copy link
Member

@xispa xispa commented Sep 28, 2023

Description of the issue/feature this PR addresses

This Pull Request fixes a DateTimeError when converting datetime with old/non-valid TZ to DateTime:

(Pdb) dt
datetime.datetime(1682, 8, 15, 16, 55, 52, 727996, tzinfo=<DstTzInfo 'Pacific/Port_Moresby' LMT+9:49:00 STD>)
(Pdb) dt.isoformat()
'1682-08-15T16:55:52.727996+09:49'
(Pdb) DateTime(dt.isoformat())
*** DateTimeError: Unknown time zone in date: 1682-08-15T16:55:52.727996+09:49
(Pdb) tz = get_timezone(dt)
(Pdb) is_valid_timezone(tz)
False
(Pdb) tz
'LMT'
(Pdb) DateTime(dt)
DateTime('1682/08/15 16:55:52.727996 Pacific/Port_Moresby')

Current behavior before PR

Traceback when trying to convert an old date to DateTime

Traceback (innermost last):
  Module ZPublisher.WSGIPublisher, line 176, in transaction_pubevents
  Module ZPublisher.WSGIPublisher, line 385, in publish_module
  Module ZPublisher.WSGIPublisher, line 288, in publish
  Module ZPublisher.mapply, line 85, in mapply
  Module ZPublisher.WSGIPublisher, line 63, in call_object
  Module senaite.core.decorators, line 40, in decorator
  [...]
  Module senaite.core.api.dtime, line 461, in to_localized_time
  Module senaite.core.api.dtime, line 400, in date_to_string
  Module senaite.core.api.dtime, line 142, in to_DT
  Module DateTime.DateTime, line 447, in __init__
  Module DateTime.DateTime, line 775, in _parse_args
DateTimeError: Unknown time zone in date: 1682-08-15T16:55:52.727996+09:49

Desired behavior after PR is merged

datetime is converted to DateTime without error

--
I confirm I have tested this PR thoroughly and coded it according to PEP8
and Plone's Python styleguide standards.

@xispa xispa requested a review from ramonski September 29, 2023 08:49
Copy link
Contributor

@ramonski ramonski left a comment

Choose a reason for hiding this comment

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

Thanks!

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.

@ramonski ramonski merged commit 0bc289d into 2.x Sep 29, 2023
2 checks passed
@ramonski ramonski deleted the to_DT-error branch September 29, 2023 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants