Is there a BUG in datetime?

Viewed 44

I am a beginner in Python. I have a question about "datetime.strptime" and "datetime.timestamp".

I want to convert DateTime string (e.g. "2022-04-12T19:40:23.742Z" to a float. Here is my code:

from datetime import datetime
import sys
print("Python version:\n ", sys.version)

def DTStr_float(S):
    S = S.replace('Z', '000').replace('T', ' ')
    T = datetime.strptime(S, '%Y-%m-%d %H:%M:%S.%f')
    f = datetime.timestamp(T)
    return(f)

I try the code:

print(DTStr_float("1987-12-10T13:48:14.600Z"))
print(DTStr_float("1987-12-10T13:48:14.6Z"))
print(DTStr_float("1977-12-10T13:48:14.600Z"))
print(DTStr_float("1967-12-10T13:48:14.600Z"))

Surprisingly, it works fine for the three first but make error on the last!

Here is my output:

Python version:
  3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:18:16) [MSC v.1928 64 bit (AMD64)]
566129894.6
566129894.6
250597094.6
Traceback (most recent call last):
  File "D:\MHM\EQ\Time_Float.py", line 14, in <module>
    print(DTStr_float("1967-12-10T13:48:14.600Z"))
  File "D:\MHM\EQ\Time_Float.py", line 8, in DTStr_float
    f = datetime.timestamp(T)
OSError: [Errno 22] Invalid argument

I am trying to more test and write this one:

from datetime import datetime
import numpy.random as RND
import sys
print("Python version:\n ", sys.version)

def DTStr_float(S): #"2022-04-12T19:40:23.742Z"
    S = S.replace('Z', '000').replace('T', ' ')
    T = datetime.strptime(S, '%Y-%m-%d %H:%M:%S.%f')
    f = datetime.timestamp(T)
    return(f)

Start_Year = 1960
for k in range(100):
    dy = 1 + RND.randint(28)
    mt = 1 + RND.randint(12)
    yr = Start_Year + RND.randint(50)
    t = RND.randint(24)
    m = RND.randint(60)
    s = RND.randint(60)
    ms = RND.randint(100)
    Str = '{:4d}-{:2d}-{:2d}T'.format(yr,mt,dy,)+\
          '{:2d}:{:2d}:{:2d}.{:3d}Z'.format(t,m,s,ms)
    Str = Str.replace(' ', '0')
    print('\n',Str,'  ', DTStr_float(Str),  end=' ')

It works but in some cases may result out Error!?

An output example:

Python version:
  3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:18:16) [MSC v.1928 64 bit (AMD64)]

 1974-09-22T11:45:23.066Z    149069723.066 
 1999-02-17T22:38:27.090Z    919278507.09 
 1999-03-01T19:28:00.073Z    920303880.073 
 1977-02-02T04:34:56.015Z    223693496.015 
 1990-03-04T13:47:45.093Z    636545865.093 
 1985-01-19T17:33:29.031Z    474991409.031 
 1971-07-24T23:25:57.034Z    49229757.034 
 2008-04-20T04:38:55.035Z    1208650135.035 
 1977-08-10T13:14:34.095Z    240050674.095 Traceback (most recent call last):
  File "D:\MHM\EQ\Time_Float.py", line 39, in <module>
    print('\n',Str,'  ', DTStr_float(Str),  end=' ')
  File "D:\MHM\EQ\Time_Float.py", line 24, in DTStr_float
    f = datetime.timestamp(T)
OSError: [Errno 22] Invalid argument

Is there using "datetime.strptime" may help?

Any ideas would be appreciated! I found it has been answered in: "https://stackoverflow.com/questions/71680355/oserror-errno-22-invalid-argument-when-using-datetime-strptime"

It was because it needs time zone information to convert time due to summer/winter time change.

thanks alot FObersteiner!

0 Answers
Related