Skip to content

datetime.fromtimestamp(t).astimezone() fails for 0<=t<86400 in Windows #107078

Closed

Description

@LefterisJP

Bug report

dt.datetime.fromtimestamp(t).astimezone() fails for 0<=t<86400 in Windows for at least python 3.9, 3.10, 3.11.
Works fine in Linux, Mac.

>> import datetime as dt
>> dt.datetime.fromtimestamp(0)
datetime.datetime(1970, 1, 1, 1, 0)

>> dt.datetime.fromtimestamp(86400)
datetime.datetime(1970, 1, 2, 1, 0)

>> dt.datetime.fromtimestamp(0).astimezone()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

>> dt.datetime.fromtimestamp(86399).astimezone()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

>> dt.datetime.fromtimestamp(86400).astimezone()
datetime.datetime(1970, 1, 2, 1, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'W. Europe Standard Time'))

A screenshot also:
F1qM1ZmXgAAVrTw

Your environment

  • CPython versions tested on: 3.9, 3.10, 3.11
  • Operating system and architecture: Windows

Additional information

This looks very very related to this issue: #73283 but now it seems to happen for astimezone.

Linked PRs

Activity

  1. littlebutt commented on Jul 23, 2023

    @littlebutt
    Contributor

    I think the bug was introduced by _datetimemodule.c here.

    lt = local(u2);

    If the t is not large enough, the local method will return -1 since it will minus an epoch time(62135683200).

  2. added 2 commits that reference this issue on Jul 24, 2023
  3. littlebutt commented on Jul 24, 2023

    @littlebutt
    Contributor

    The thing is that if the t you passed is so small, the internal parameter u2 will be negative. Since the method localtime_s cannot accept a negative parameter, it will throw an OSError. I just plused a max_fold_seconds to make sure the u2 is positive. I admit it is not an elegant solotion.

  4. serhiy-storchaka commented on Feb 1, 2024

    @serhiy-storchaka
    Member

    On Linux timestamp can be negative. Why the interval 0-86400 is so important that it is worth to support it on Windows, but not negative timestamps? It is just a limitation of the platform.

  5. zooba commented on Feb 1, 2024

    @zooba
    Member

    I suspect the value 0 is important, as it's an obvious edge case (or null case). Investigating that revealed that the first value that works is 86400, and so that's the range.

    I guess people are less surprised that -1 doesn't work? TBH, I'd be okay with making this calculation fully Python specific, rather than trying to use OS APIs, assuming we can find a migration path. The CRT emulation clearly isn't compatible enough, and the OS APIs are so much richer that they aren't really compatible either.

    There was some discussion about adopting ICU for this kind of stuff... maybe if we ever do that we can have consistent timezone handling?

  6. leon-adarcewicz commented on Nov 25, 2024

    @leon-adarcewicz

    Hi folks,
    I fail with same issue for datetime.datetime(1900, 1, 1, 9, 0).astimezone(tz=None) ==> [Errno 22] Invalid argument,
    so I wanted to check if the is some solution for this issue for windows users?

  7. wkoot commented on Oct 28, 2025

    @wkoot

    Is this not a duplicate of #80620 ?

  8. sigma67 commented on Nov 26, 2025

    @sigma67

    It does seem to fail for every date before 1970 as well.

  9. vstinner commented on Jan 15, 2026

    @vstinner
    Member

    Issue fixed by #143463.

    Python 3.15.0a5+ (heads/main:f5685a266b2, Jan 15 2026, 10:57:22) [MSC v.1944 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import datetime as dt
    >>> dt.datetime.fromtimestamp(0).astimezone()
    datetime.datetime(1970, 1, 1, 14, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=50400), 'Îles de la Ligne '))
    >>> dt.datetime.fromtimestamp(86399).astimezone()
    datetime.datetime(1970, 1, 2, 13, 59, 59, tzinfo=datetime.timezone(datetime.timedelta(seconds=50400), 'Îles de la Ligne '))
    
  10. ndonkoHenri commented on Jan 16, 2026

    @ndonkoHenri

    @vstinner thanks for the info! Is there any chance we have that fix in some other "older" python versions (3.13, 3.14, for example) too? If yes, when can we expect them to land in an official release?

  11. vstinner commented on Jan 16, 2026

    @vstinner
    Member

    Hi. The change is a new feature, not a bugfix, so it cannot be backported. It will only be part of Python 3.15 and newer.

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

    OS-windowsstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions