3

Python's strptime() function converts all the dates with year less than 69 (in format dd-mm-yy) to 20XX and above that to 19XX.

Is there any way to tweak this setting, noting found in docs.

datetime.strptime('31-07-68', '%d-%m-%y').date()

datetime.date(2068, 7, 31)

datetime.strptime('31-07-68', '%d-%m-%y').date()

datetime.date(1969, 7, 31)

Sabith
  • 1,628
  • 2
  • 20
  • 38
Aadil Srivastava
  • 609
  • 8
  • 12
  • I just update my answer to try to shift the threshold to 1950-2049 as an example (https://stackoverflow.com/a/55674284/3564632). In case you wish to change the threshold to more than 100 year and shift it to the likely start/end date, than `YY` is not enough information, as you need to know in which century to work on. – denis_lor Apr 14 '19 at 10:43

2 Answers2

2

I came up with this solution to change the threshold to 1950-2049 as an example, you could however just tweak/shift it as you want by changing the threshold variable value in the function:

from datetime import datetime, date

dateResult1950 = datetime.strptime('31-07-50', '%d-%m-%y').date()
dateResult2049 = datetime.strptime('31-07-49', '%d-%m-%y').date()

def changeThreshold(year, threshold=1950):
    return (year-threshold)%100 + threshold

print(changeThreshold(dateResult1950.year))
print(changeThreshold(dateResult2049.year))
#1950
#2049
denis_lor
  • 6,212
  • 4
  • 31
  • 55
1

Almost certainly your answer: Not without a Python patch.

From line 375 in CPython _strptime.py:

if group_key == 'y':
    year = int(found_dict['y'])
    # Open Group specification for strptime() states that a %y
    #value in the range of [00, 68] is in the century 2000, while
    #[69,99] is in the century 1900
    if year <= 68:
        year += 2000
    else:
        year += 1900

https://github.com/python/cpython/blob/master/Lib/_strptime.py

You could simulate an alternative by doing your own YY to YYYY conversion before invoking strptime.

Technical caveat answer: Python being an interpreted language where modules are imported in a well understood way, you can technically manipulate the _strptime object at initialization runtime and replace with your own function, perhaps one decorating the original.

You would need an extremely good reason to do this in production code. I did it once with another core library to work around an OS bug, in discussion with the team on when it would need to be removed. It's highly unintuitive to any future maintainer of your code, 9999/10000 times it will be better to just call some utility library in your own code. If you really need to do it, it's easy enough to work out, so I will skip the code sample to avoid copy/pasters.

Adam Burke
  • 724
  • 1
  • 7
  • 21
  • 1
    Thanks Adam, I used a similar approach for solving out this issue. Actually the data is present in the format dd-mm-yy and I needed to convert in the format yyyy-mm-dd for carrying out some calculations. – Aadil Srivastava Apr 15 '19 at 12:03
  • This is possible also without having to do any modification to the core libraries of Python, check my answer. You shouldn't modify core libraries of Python, just use it as it is and `compose` over what is given to you. – denis_lor Apr 15 '19 at 12:26
  • 1
    The "simulate an alternative by doing your own YY to YYYY conversion" is the right approach in the application, and the answer by @denis_lor does it. My answer applies to the question from the original: "is there a way to tweak this setting?". I do think eventually Python will want a better mechanism for managing this, too. – Adam Burke Apr 15 '19 at 12:32
  • I didn't realised he wanted to touch the python libraries. Thanks for the comment :) – denis_lor Apr 15 '19 at 12:38