Skip to content

Can't round-trip datetimes<->timestamps prior to 1970 on Windows #46746

Closed

Description

@mark-summerfield
BPO 2494
Nosy @vstinner, @tiran, @mark-summerfield, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2010-12-22.03:11:09.309>
created_at = <Date 2008-03-27.07:27:05.799>
labels = ['invalid', 'type-bug', 'library']
title = "Can't round-trip datetimes<->timestamps prior to 1970 on Windows"
updated_at = <Date 2010-12-22.03:11:09.307>
user = 'https://github.com/mark-summerfield'

bugs.python.org fields:

activity = <Date 2010-12-22.03:11:09.307>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2010-12-22.03:11:09.309>
closer = 'r.david.murray'
components = ['Library (Lib)']
creation = <Date 2008-03-27.07:27:05.799>
creator = 'mark'
dependencies = []
files = []
hgrepos = []
issue_num = 2494
keywords = []
message_count = 4.0
messages = ['64578', '64583', '75722', '124481']
nosy_count = 4.0
nosy_names = ['vstinner', 'christian.heimes', 'mark', 'r.david.murray']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2494'
versions = ['Python 3.0']

Activity

  1. mark-summerfield commented on Mar 27, 2008

    mark-summerfieldmannequin
    MannequinAuthor
    # 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

  2. added
    stdlib
    type-bug
    on Mar 27, 2008
  3. tiran commented on Mar 27, 2008

    @tiran
    Member

    It's most likely a platform limitation. Some platforms doen't support
    negative time stamps.

  4. vstinner commented on Nov 11, 2008

    @vstinner
    Member
    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

  5. bitdancer commented on Dec 22, 2010

    @bitdancer
    Member

    Indeed, the time module is documented as not handling timestamps before the epoch.

  6. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions