X Tutup
The Wayback Machine - https://web.archive.org/web/20240531132151/https://github.com/python/cpython/issues/80940
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

astimezone() fails on Windows for pre-epoch times #80940

Open
Snidhi mannequin opened this issue Apr 30, 2019 · 15 comments
Open

astimezone() fails on Windows for pre-epoch times #80940

Snidhi mannequin opened this issue Apr 30, 2019 · 15 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Snidhi
Copy link
Mannequin

Snidhi mannequin commented Apr 30, 2019

BPO 36759
Nosy @abalkin, @pganssle, @Windsooon, @jonnyhsu

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 = None
created_at = <Date 2019-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 = <Date 2020-03-24.08:53:35.045>
user = 'https://bugs.python.org/Snidhi'

bugs.python.org fields:

activity = <Date 2020-03-24.08:53:35.045>
actor = 'Ard Kuijpers'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2019-04-30.11:36:25.986>
creator = 'Snidhi'
dependencies = []
files = []
hgrepos = []
issue_num = 36759
keywords = []
message_count = 10.0
messages = ['341149', '341152', '341157', '341236', '342866', '364801', '364866', '364907', '364918', '364921']
nosy_count = 7.0
nosy_names = ['belopolsky', 'SilentGhost', 'p-ganssle', 'Snidhi', 'Windson Yang', 'Jonathan Hsu', 'Ard Kuijpers']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'test needed'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue36759'
versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

@Snidhi
Copy link
Mannequin Author

Snidhi mannequin commented 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

import datetime;

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);

print(d_Time);

@Snidhi Snidhi mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 30, 2019
@Windsooon
Copy link
Mannequin

Windsooon mannequin commented 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
Copy link
Mannequin

SilentGhost mannequin commented 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
Copy link
Mannequin

Windsooon mannequin commented May 2, 2019

Thanks, SilentGhost, you are right. I will leave this to a Windows expert instead.

@SilentGhost
Copy link
Mannequin

SilentGhost mannequin commented 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.

@SilentGhost SilentGhost mannequin closed this as completed May 19, 2019
@ArdKuijpers
Copy link
Mannequin

ArdKuijpers mannequin commented 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.

@ArdKuijpers ArdKuijpers mannequin added the 3.7 (EOL) end of life label Mar 22, 2020
@SilentGhost
Copy link
Mannequin

SilentGhost mannequin commented 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.

@SilentGhost SilentGhost mannequin added 3.8 only security fixes 3.9 only security fixes labels Mar 23, 2020
@SilentGhost SilentGhost mannequin reopened this Mar 23, 2020
@SilentGhost SilentGhost mannequin changed the title datetime: astimezone() results in OSError: [Errno 22] Invalid argument astimezone() fails on Windows for pre-epoch times Mar 23, 2020
@jonnyhsu
Copy link
Mannequin

jonnyhsu mannequin commented Mar 23, 2020

I'd like to take on this issue if no one else is working on it.

@jonnyhsu
Copy link
Mannequin

jonnyhsu mannequin commented Mar 24, 2020

This exception is raised because astimezone() ends up calling time.localtime() to determine the appropriate time zone. If the datetime object has a pre-epoch value, it passes a negative timestamp to time.localtime(). On Windows, time.localtime() does not accept values greater than 0 (more discussion in issue bpo-35796).

This is the minimal code required to reproduce the error:

from datetime import datetime
datetime(1969, 1, 1).astimezone()

Without the ability to ascertain the time zone with localtime(), I'm not sure if the time zone can be accurately determined. It's not clear what the proper behavior is. Maybe raise a ValueError?

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?

@ArdKuijpers
Copy link
Mannequin

ArdKuijpers mannequin commented 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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@bariod
Copy link
Contributor

bariod commented Oct 4, 2022

For some reason, for post-epoch values, this works till the year 3001, not 2038:

from datetime import datetime
datetime(3001, 1, 19, 7, 59, 59, 999999).astimezone()  # still works
datetime(3001, 1, 19, 8, 0, 0, 0).astimezone() # fails with 'OSError: [Errno 22] Invalid argument'

run with py 3.9.6

@dnowacki-usgs
Copy link

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?

@pganssle
Copy link
Member

pganssle commented Aug 9, 2023

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.

@dnowacki-usgs
Copy link

Thanks for the insight here re: the Windows API.

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.

@pganssle
Copy link
Member

pganssle commented Aug 9, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

3 participants
X Tutup