You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using datetime.datetime produces objects whose timestamp() value is not valid under Windows, at least insofar as Python calculates it. To replicate:
importdatetimedt=datetime.datetime(1970,1,2)
print(dt.timestamp()) # validdt=datetime.datetime(1969,1,2)
print(dt.timestamp()) # raises OSError: [Errno 22] Invalid argumentdt_delta=(dt-datetime.datetime(1970, 1, 1))
print(dt.total_seconds()) # works to produce the correct timestamp
What's going on here is the default datetime object doesn't handle timestamps correctly for dates before Jan 2 1970 and after Jan 19 3001. By subtracting the epoch (Jan 1 1970) from a datetime this produces a timedelta object whose total_seconds method returns the timestamp that is expected. A routine is required to get the correct timestamp that will be portable across platforms:
This should be used in place of calling the datetime.datetime.timestamp method. However, datetime.datetime.fromtimestamp will not work with these values, a function to do this needed as well:
Using
datetime.datetime
produces objects whosetimestamp()
value is not valid under Windows, at least insofar as Python calculates it. To replicate:What's going on here is the default
datetime
object doesn't handle timestamps correctly for dates before Jan 2 1970 and after Jan 19 3001. By subtracting the epoch (Jan 1 1970) from a datetime this produces atimedelta
object whosetotal_seconds
method returns the timestamp that is expected. A routine is required to get the correct timestamp that will be portable across platforms:This should be used in place of calling the
datetime.datetime.timestamp
method. However,datetime.datetime.fromtimestamp
will not work with these values, a function to do this needed as well:The text was updated successfully, but these errors were encountered: