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 ValueError when displaying dates before 1900 (by datetimewidget) #2292

Merged
merged 10 commits into from
Apr 13, 2023

Conversation

xispa
Copy link
Member

@xispa xispa commented Apr 10, 2023

Description of the issue/feature this PR addresses

This Pull Request makes the date time widget to not fail when displaying dates before year 1900 (see https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior)

Current behavior before PR

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 plone.autoform.view, line 42, in __call__
  Module plone.autoform.view, line 33, in render
  Module Products.Five.browser.pagetemplatefile, line 126, in __call__
  Module Products.Five.browser.pagetemplatefile, line 61, in __call__
  Module zope.pagetemplate.pagetemplate, line 135, in pt_render
  Module Products.PageTemplates.engine, line 378, in __call__
  Module z3c.pt.pagetemplate, line 176, in render
  Module chameleon.zpt.template, line 302, in render
  Module chameleon.template, line 192, in render
  Module 5703504060db8b032e75bdc7efdfea0e, line 517, in render
  Module 8c1fb1e2e89fbd61ab31092e1ef695c4, line 1449, in render_master
  Module 8c1fb1e2e89fbd61ab31092e1ef695c4, line 407, in render_content
  Module 5703504060db8b032e75bdc7efdfea0e, line 502, in __fill_content_core
  Module 5703504060db8b032e75bdc7efdfea0e, line 151, in render_content_core
  Module zope.tales.expressions, line 250, in __call__
  Module Products.PageTemplates.Expressions, line 225, in _eval
  Module Products.PageTemplates.Expressions, line 155, in render
  Module zope.browserpage.simpleviewclass, line 41, in __call__
  Module zope.browserpage.viewpagetemplatefile, line 81, in __call__
  Module zope.browserpage.viewpagetemplatefile, line 49, in __call__
  Module zope.pagetemplate.pagetemplate, line 135, in pt_render
  Module Products.PageTemplates.engine, line 378, in __call__
  Module z3c.pt.pagetemplate, line 176, in render
  Module chameleon.zpt.template, line 302, in render
  Module chameleon.template, line 192, in render
  Module 732033e5f2228c2cee7d4e884289387a, line 586, in render
  Module 732033e5f2228c2cee7d4e884289387a, line 456, in render_widget_wrapper
  Module zope.tales.expressions, line 250, in __call__
  Module Products.PageTemplates.Expressions, line 225, in _eval
  Module Products.PageTemplates.Expressions, line 155, in render
  Module z3c.form.widget, line 154, in render
  Module zope.browserpage.viewpagetemplatefile, line 49, in __call__
  Module zope.pagetemplate.pagetemplate, line 135, in pt_render
  Module Products.PageTemplates.engine, line 378, in __call__
  Module z3c.pt.pagetemplate, line 176, in render
  Module chameleon.zpt.template, line 302, in render
  Module chameleon.template, line 215, in render
  Module chameleon.template, line 192, in render
  Module 39e27c6e61b8733fff01db0a8a115ffd, line 184, in render
  Module zope.tales.pythonexpr, line 73, in __call__
   - __traceback_info__: (view.get_display_value())
  Module <string>, line 1, in <module>
  Module senaite.core.z3cform.widgets.datetimewidget, line 190, in get_display_value
  Module senaite.core.z3cform.widgets.datetimewidget, line 178, in to_localized_time
  Module Products.CMFPlone.TranslationServiceTool, line 98, in ulocalized_time
  Module Products.CMFPlone.i18nl10n, line 208, in ulocalized_time
  Module DateTime.DateTime, line 1568, in strftime
ValueError: year=1194 is before 1900; the datetime strftime() methods require year >= 1900

Desired behavior after PR is merged

Dates before 1900 are displayed (in iso-like format) without error

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

@xispa xispa changed the title Make datetimewidget to fallback to iso format when displaying dates before 1900 Fix ValueError when displaying dates before 1900 (by datetimewidget) Apr 10, 2023
@xispa
Copy link
Member Author

xispa commented Apr 13, 2023

Doctest for 11cba59 is failing:

File "/home/runner/work/senaite.core/senaite.core/src/senaite/core/tests/../z3cform/widgets/datetimewidget.txt", line 102, in datetimewidget.txt
Failed example:
    print(widget.render())
Expected:
  <html>
    <body>
      <span class="senaite-datetime-widget">2021-12-24 00:00</span>
    </body>
  </html>

Got:
  <html>
    <body>
      <span class="senaite-datetime-widget">2021-12-24T00:00:00</span>
    </body>
  </html>

Diff:
  <html>
    <body>
      <span class="senaite-datetime-widget">2021-12-24 00:00 (got: 
    2021-12-24T00:00:00
  )</span>
    </body>
  </html>

A bit confused though:

>>> from bika.lims import api
>>> from senaite.core.api import dtime
>>> from datetime import datetime
>>> from DateTime import DateTime
>>> portal = api.get_portal()
>>> ts = api.get_tool("translation_service")

>>> dt = "2022-12-14"
>>> ts.ulocalized_time(dt, long_format=True, context=portal, domain="senaite.core")
u'14/12/2022 01:00'
>>> dtime.to_localized_time(dt, long_format=True, context=portal)
u'14/12/2022 01:00'

>>> dt = datetime(2012,12,14)
>>> ts.ulocalized_time(dt, long_format=True, context=portal, domain="senaite.core")
u'14/12/2012 00:00'
>>> dtime.to_localized_time(dt, long_format=True, context=portal)
u'14/12/2012 01:00'

>>> dt = DateTime(2012,12,14)
>>> ts.ulocalized_time(dt, long_format=True, context=portal, domain="senaite.core")
u'14/12/2012 00:00'
>>> dtime.to_localized_time(dt, long_format=True, context=portal)
u'14/12/2012 00:00'

@xispa
Copy link
Member Author

xispa commented Apr 13, 2023

A bit confused though

Got it,

@ramonski ramonski merged commit a27f488 into 2.x Apr 13, 2023
@ramonski ramonski deleted the handle-before-1900-widget branch April 13, 2023 19:01
@xispa xispa added the To port label Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants