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: Can't round-trip datetimes<->timestamps prior to 1970 on Windows
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.0
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, mark, r.david.murray, vstinner
Priority: normal Keywords:

Created on 2008-03-27 07:27 by mark, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg64578 - (view) Author: Mark Summerfield (mark) * Date: 2008-03-27 07:27
# If you run the code below on Py30a3 you get the output shown at the end
import calendar, datetime, time

pastdate = datetime.datetime(1969, 12, 31)
print(pastdate)
timestamp = calendar.timegm(pastdate.utctimetuple())
print(timestamp)
try:
    pastdate_x = datetime.datetime.utcfromtimestamp(timestamp)
except ValueError as err:
    print("FAIL", err)
try:
    print(time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(timestamp)))
except ValueError as err:
    print("FAIL", err)

r"""
Python 30a3

Windows output:

1969-12-31 00:00:00
-86400
FAIL timestamp out of range for platform localtime()/gmtime() function
FAIL (22, 'Invalid argument')

Linux output:
1969-12-31 00:00:00
-86400
1969-12-31T00:00:00
"""
# What this appears to show is that you can't round-trip between
datetimes and timestamps on Windows for dates prior to 1970
msg64583 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-03-27 11:50
It's most likely a platform limitation. Some platforms doen't support
negative time stamps.
msg75722 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-11-11 02:58
If you doesn't care to the time zone (UTC), use:
>>> datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=-86400)
datetime.datetime(1969, 12, 31, 0, 0)

This code is portable on Windows, Linux but also works with 
IronPython:
http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=7269
msg124481 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-12-22 03:11
Indeed, the time module is documented as not handling timestamps before the epoch.
History
Date User Action Args
2022-04-11 14:56:32adminsetgithub: 46746
2010-12-22 03:11:09r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg124481

resolution: not a bug
stage: resolved
2008-11-11 02:58:04vstinnersetnosy: + vstinner
messages: + msg75722
2008-03-27 11:50:52christian.heimessetnosy: + christian.heimes
messages: + msg64583
2008-03-27 07:27:05markcreate