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
{{ message }}
Repository navigation
astimezone() fails on Windows for pre-epoch times #80940
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=Noneclosed_at=Nonecreated_at=<Date2019-04-30.11:36:25.986>labels= ['3.7', '3.8', 'type-bug', 'library', '3.9']
title='astimezone() fails on Windows for pre-epoch times'updated_at=<Date2020-03-24.08:53:35.045>user='https://bugs.python.org/Snidhi'
With: Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
importdatetime;
d_Time=datetime.datetime.strptime('03:30 PM', '%I:%M %p');
d_Time=d_Time.astimezone(datetime.timezone.utc);
# RESULTS IN OSError: [Errno 22] Invalid argument# WHEREAS the foll. does not have the issue!d_Time=datetime.datetime(year=d_Time.year,
month=d_Time.month,
day=d_Time.day,
hour=d_Time.hour,
minute=d_Time.minute,
second=d_Time.second,
tzinfo=datetime.timezone.utc);
on macOS 10.14.4, I got ValueError: offset must be a timedelta representing a whole number of minutes, not datetime.timedelta(0, 29143). I will do some research to see why this happen.
SilentGhost commented on Apr 30, 2019
SilentGhostmannequin
Mannequin
More actions
This seems like a duplicate (or at least very similar) to the bpo-29097. Could you try a newer version of Python (that issue was fixed in 3.6.7) to make sure it's not a duplicate?
That was specifically a Windows bug, Windson, make sure that what you're seeing is not fixed elsewhere if you only have macOS available.
Windsooon commented on May 2, 2019
Windsooonmannequin
Mannequin
More actions
Thanks, SilentGhost, you are right. I will leave this to a Windows expert instead.
SilentGhost commented on May 19, 2019
SilentGhostmannequin
Mannequin
More actions
I'm going to close this issue as a duplicate of bpo-29097. If you're still experience this problem on python3.7, please re-open.
Encountered this bug on Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)], so on Windows 10. I can reproduce the bug with the code in message https://bugs.python.org/msg341149.
It does not seem to be a duplicate of bpo-29097 since I cannot reproduce that issue.
Yes, I was also able to verify this issue on 3.8.2 on win10. Argument to astimezone is not required, and this happens for both naïve and aware objects.
changed the title [-]datetime: astimezone() results in OSError: [Errno 22] Invalid argument[/-][+]astimezone() fails on Windows for pre-epoch times[/+]on Mar 23, 2020
2 remaining items
ArdKuijpers commented on Mar 24, 2020
ArdKuijpersmannequin
Mannequin
More actions
It would be helpful to have a better error message than '[Errno 22] Invalid argument' on Windows, so a ValueError seems to be a good idea at the moment.
PEP-615 proposes to include the IANA tz database, which would negate the need for a system call. Should we wait for this PEP before fixing this issue? Thoughts?
FWIW, PEP-615 was accepted and became part of Python 3.9 in May 2020, so perhaps this approach is viable now?
FWIW, PEP-615 was accepted and became part of Python 3.9 in May 2020, so perhaps this approach is viable now?
PEP 615 didn't really change anything with respect to this. The issue is that naïve times are local times, and need to be resolved with the system's time zone handling mechanisms. The linked article goes into detail about why it is not sufficient for all use cases to attach a tzinfo that corresponds to "local time" (though individuals are free to do that if they have one of the many very common situations where these concerns don't matter).
This one may be a fundamental limitation of the Windows API, though I doubt it's a major issue, since time zone handling doesn't make nearly as much sense the further back (or forward!) in time you go. It seems unusual for someone to actually care about the answer to the question, "What time zone does this computer think should be attached to a date in local time in 1965?" for example.
It seems unusual for someone to actually care about the answer to the question, "What time zone does this computer think should be attached to a date in local time in 1965?" for example.
In my specific case, we have decades of environmental observations, often made pre-1970, tagged in a database with times in the local time zone for the observation. When retrieving these data we wish to unify them all to GMT/UTC.
In my specific case, we have decades of environmental observations, often made pre-1970, tagged in a database with times in the local time zone for the observation. When retrieving these data we wish to unify them all to GMT/UTC.
Yes, but these observations were not made on the computer you are running the code from, and even if they were, there's nothing that says they were using the set of time zone rules that currently applies on the computer you are running the code from. For code like that you need to either know the UTC time or the local time zone rules that were in effect at the time (or possibly both), and the built-in support for local time would be at best coincidentally useful.
vstinner@WIN C:\victor\python\main>python
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.
>>> from datetime import datetime
>>> datetime(1969, 1, 1).astimezone()
datetime.datetime(1969, 1, 1, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=50400), 'Îles de la Ligne (heure d’été)'))
But datetime(3001, 1, 19, 8, 0, 0, 0).astimezone() # fails with 'OSError: [Errno 22] Invalid argument' still fails.
Activity
Snidhi commentedon Apr 30, 2019
With: Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
print(d_Time);
Windsooon commentedon Apr 30, 2019
on macOS 10.14.4, I got
ValueError: offset must be a timedelta representing a whole number of minutes, not datetime.timedelta(0, 29143).I will do some research to see why this happen.SilentGhost commentedon Apr 30, 2019
This seems like a duplicate (or at least very similar) to the bpo-29097. Could you try a newer version of Python (that issue was fixed in 3.6.7) to make sure it's not a duplicate?
That was specifically a Windows bug, Windson, make sure that what you're seeing is not fixed elsewhere if you only have macOS available.
Windsooon commentedon May 2, 2019
Thanks, SilentGhost, you are right. I will leave this to a Windows expert instead.
SilentGhost commentedon May 19, 2019
I'm going to close this issue as a duplicate of bpo-29097. If you're still experience this problem on python3.7, please re-open.
ArdKuijpers commentedon Mar 22, 2020
Encountered this bug on Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)], so on Windows 10. I can reproduce the bug with the code in message https://bugs.python.org/msg341149.
It does not seem to be a duplicate of bpo-29097 since I cannot reproduce that issue.
SilentGhost commentedon Mar 23, 2020
Yes, I was also able to verify this issue on 3.8.2 on win10. Argument to astimezone is not required, and this happens for both naïve and aware objects.
[-]datetime: astimezone() results in OSError: [Errno 22] Invalid argument[/-][+]astimezone() fails on Windows for pre-epoch times[/+]2 remaining items
ArdKuijpers commentedon Mar 24, 2020
It would be helpful to have a better error message than '[Errno 22] Invalid argument' on Windows, so a ValueError seems to be a good idea at the moment.
bariod commentedon Oct 4, 2022
For some reason, for post-epoch values, this works till the year 3001, not 2038:
run with py 3.9.6
dnowacki-usgs commentedon Aug 8, 2023
FWIW, PEP-615 was accepted and became part of Python 3.9 in May 2020, so perhaps this approach is viable now?
pganssle commentedon Aug 9, 2023
PEP 615 didn't really change anything with respect to this. The issue is that naïve times are local times, and need to be resolved with the system's time zone handling mechanisms. The linked article goes into detail about why it is not sufficient for all use cases to attach a tzinfo that corresponds to "local time" (though individuals are free to do that if they have one of the many very common situations where these concerns don't matter).
This one may be a fundamental limitation of the Windows API, though I doubt it's a major issue, since time zone handling doesn't make nearly as much sense the further back (or forward!) in time you go. It seems unusual for someone to actually care about the answer to the question, "What time zone does this computer think should be attached to a date in local time in 1965?" for example.
dnowacki-usgs commentedon Aug 9, 2023
Thanks for the insight here re: the Windows API.
In my specific case, we have decades of environmental observations, often made pre-1970, tagged in a database with times in the local time zone for the observation. When retrieving these data we wish to unify them all to GMT/UTC.
pganssle commentedon Aug 9, 2023
Yes, but these observations were not made on the computer you are running the code from, and even if they were, there's nothing that says they were using the set of time zone rules that currently applies on the computer you are running the code from. For code like that you need to either know the UTC time or the local time zone rules that were in effect at the time (or possibly both), and the built-in support for local time would be at best coincidentally useful.
vstinner commentedon Jan 15, 2026
Issue fixed by #143463.
But
datetime(3001, 1, 19, 8, 0, 0, 0).astimezone() # fails with 'OSError: [Errno 22] Invalid argument'still fails.