slashmili / python-jalali

Jalali calendar binding for Python based on Python's datetime module
http://pypi.python.org/pypi/jdatetime/
Other
337 stars 47 forks source link

`strptime` does not support the `%y` directive #100

Closed 5j9 closed 2 years ago

5j9 commented 2 years ago
>>> import jdatetime, datetime
>>> datetime.datetime.strptime('00/1/1', '%y/%m/%d')  # works fine
datetime.datetime(2000, 1, 1, 0, 0)
>>> jdatetime.datetime.strptime('00/1/1', '%y/%m/%d')  # unexpected error:
Traceback (most recent call last):
  File "C:\Users\a\AppData\Local\Programs\Python\Python310\lib\site-packages\jdatetime\__init__.py", line 950, in strptime
    raise ValueError()
ValueError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\a\AppData\Local\Programs\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py", line 3444, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-f4d57fcf7498>", line 1, in <module>
    jdatetime.datetime.strptime('00/1/1', '%y/%m/%d')
  File "C:\Users\a\AppData\Local\Programs\Python\Python310\lib\site-packages\jdatetime\__init__.py", line 962, in strptime
    raise ValueError(
ValueError: time data '00/1/1' does not match format '%y/%m/%d'

Expected output: jdatetime.datetime(1400, 1, 1, 0, 0) (use the first two digits of the current century to interpret 00)

runtime information

hramezani commented 2 years ago

I confirm that jdatetime.datetime.strptim cannot handle %y. Would you like to prepare a patch and fix it?

5j9 commented 2 years ago

I just noticed that it is more complicated than just using the current century:

>>> datetime.datetime.strptime('68/1/1', '%y/%m/%d')
datetime.datetime(2068, 1, 1, 0, 0)
>>> datetime.datetime.strptime('69/1/1', '%y/%m/%d')
datetime.datetime(1969, 1, 1, 0, 0)

CPython follows the Open Group specification (IEEE Std 1003.1-2001). See https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Lib/_strptime.py#L377 .

I am not sure what would be the appropriate range for Jalali dates here.

hramezani commented 2 years ago

Not sure! We can consider [00, 68] in century 1400 and [69,99] in the century 1300. Or consider all of them in century 1400. @slashmili @farzadghanei What do you think?

slashmili commented 2 years ago

the spec is absurd and unreasonable . Anyway I think this is an ok solution:

We can consider [00, 68] in century 1400 and [69,99] in the century 1300.