Below figure shows the windows setting for the date&time formats. I want to use this format to display formatted date&time string by using function strftime(). And it should work on python 3.4 and later version without OS admin rights. How can I get date and time formats that have been set in the windows system in python3? Or is there any equivalent method to get the formated datetime string?
I tried to use win32api.GetDateFormat and win32api.GetTimeFormat, but it doesn't work well. The behavior of these two methods are different on python 3.4 and python 3.9. In python 3.9, below code works when year >= 1970
>>> win32api.GetTimeFormat(0,0, datetime.datetime(1969,11,20,1,2,3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
>>> win32api.GetTimeFormat(0,0, datetime.datetime(1970,11,20,1,2,3))
'5:02:03 PM'
In python 3.4, above code will raise an ValueError. But it can be fixed by setting timezone. And GetDateFormat only works when year > 1600.
(Pdb) win32api.GetTimeFormat(0,0, datetime.datetime(1970,11,20,1,2,3))
*** ValueError: astimezone() cannot be applied to a naive datetime
(Pdb) win32api.GetDateFormat(0, 0,datetime.datetime(1601,10,10,1,2,3).replace(tzinfo=pytz.timezone('UTC')))
'10/10/1601'
(Pdb) win32api.GetTimeFormat(0, 0,datetime.datetime(1601,10,10,1,2,3).replace(tzinfo=pytz.timezone('UTC')))
'1:02:03 AM'
(Pdb) win32api.GetTimeFormat(0, 0,datetime.datetime(1600,10,10,1,2,3).replace(tzinfo=pytz.timezone('UTC')))
'1:02:03 AM'
(Pdb) win32api.GetDateFormat(0, 0,datetime.datetime(1600,10,10,1,2,3).replace(tzinfo=pytz.timezone('UTC')))
*** pywintypes.error: (87, 'GetDateFormat', 'The parameter is incorrect.')
(Pdb) win32api.GetDateFormat(0, 0,datetime.datetime(1601,10,10,1,2,3).replace(tzinfo=pytz.timezone('UTC')))
'10/10/1601'
Is there any way that I can easily to generate formatted datetime string based on setting of OS in python 3.4+?
