pylover / khayyam

A Persian Date & Time (aka: Jalali Calendar) library with timezone, DST (daylight-saving), full formatting & parsing support for python 2.x & 3.x.
http://khayyam.dobisel.com
GNU General Public License v3.0
142 stars 22 forks source link

{KeyError}'hour12 #33

Closed alihossein closed 4 years ago

alihossein commented 4 years ago

Hi. thank for the great package. when I use %p parameter, I encounter with this message: {KeyError}'hour12

my use case :

JalaliDatetime.strptime('۹:۲۴ ق.ظ', '%k:%r %p')

I think the problem is this function in ampm.py

    def post_parser(self, ctx, formatter):
        hour12 = ctx['hour12']
pylover commented 4 years ago

Sorry for the late, I'll create a test for this case asap.

pylover commented 4 years ago

Seems you are using %k hours in 24h form with ampm parser. which is invalid, but the parser should raise an appropriate error.

pylover commented 4 years ago

Now it returns:

======================================================================
ERROR: test_am_pm (__main__.JalaliDatetimeFormatterTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/vahid/workspace/khayyam/khayyam/formatting/directives/ampm.py", line 25, in post_parser
    hour12 = ctx['hour12']
KeyError: 'hour12'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "khayyam/tests/test_jalali_datetime_formatter.py", line 224, in test_am_pm
    self.assertEqual(JalaliDatetime.strptime('۹:۲۴ ق.ظ', '%k:%r %p'), '')
  File "/home/vahid/workspace/khayyam/khayyam/jalali_datetime.py", line 257, in strptime
    result = cls.formatterfactory(fmt).parse(date_string)
  File "/home/vahid/workspace/khayyam/khayyam/formatting/formatters.py", line 118, in parse
    self._parse_post_processor(result)
  File "/home/vahid/workspace/khayyam/khayyam/formatting/formatters.py", line 114, in _parse_post_processor
    self.directives_by_name[directive_name].post_parser(parse_result, self)
  File "/home/vahid/workspace/khayyam/khayyam/formatting/directives/ampm.py", line 28, in post_parser
    '%p and %t directives should be used with %i, %I or %l'
ValueError: %p and %t directives should be used with %i, %I or %l

----------------------------------------------------------------------