This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: datetime.timestamp() fails for naive-datetime values prior to the start of the epoch
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: smrpy
Priority: normal Keywords:

Created on 2022-02-06 17:51 by smrpy, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg412649 - (view) Author: Sam Roberts (smrpy) Date: 2022-02-06 17:51
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32

datetime.fromtimestamp() fails for naive-datetime values prior to the start of the epoch, but for some reason works properly for aware-datetime values prior to the start of the epoch.

This is at least inconsistent, but seems like a bug.

Negative timestamps for dates prior to the start of the epoch are used by yahoo finance and in the yfinance module.

>>> import datetime
>>> start = int(datetime.datetime(1962, 1, 31, tzinfo=datetime.timezone.utc).timestamp())
>>> start
-249868800
>>> start = int(datetime.datetime(1962, 1, 31).timestamp())
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    start = int(datetime.datetime(1962, 1, 31).timestamp())
OSError: [Errno 22] Invalid argument
msg412650 - (view) Author: Sam Roberts (smrpy) Date: 2022-02-06 17:55
the first sentence should have read:

datetime.timestamp() fails for naive-datetime values prior to the start of the epoch, but for some reason works properly for aware-datetime values prior to the start of the epoch.
msg412659 - (view) Author: Sam Roberts (smrpy) Date: 2022-02-06 19:55
this seems like an expected discrepancy because of a difference in the mechanism used for aware datatimes vs. naive datetimes, although I'm not sure I understand why the computation with naive datetimes uses the mktime() function rather than invoking datetime.timedelta.total_seconds() on a datetime difference.
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90818
2022-02-06 19:56:15smrpysetresolution: not a bug
2022-02-06 19:55:47smrpysetstatus: open -> closed

messages: + msg412659
stage: resolved
2022-02-06 17:55:57smrpysetmessages: + msg412650
title: datetime.fromtimestamp() fails for naive-datetime values prior to the start of the epoch -> datetime.timestamp() fails for naive-datetime values prior to the start of the epoch
2022-02-06 17:52:38smrpysettitle: datetime.datetime.fromtimestamp -> datetime.fromtimestamp() fails for naive-datetime values prior to the start of the epoch
2022-02-06 17:51:35smrpycreate