classification
Title: astimezone() fails on Windows for pre-epoch times
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Ard Kuijpers, Jonathan Hsu, SilentGhost, Snidhi, Windson Yang, belopolsky, p-ganssle
Priority: normal Keywords:

Created on 2019-04-30 11:36 by Snidhi, last changed 2020-03-24 08:53 by Ard Kuijpers.

Messages (10)
msg341149 - (view) Author: Snidhi Sofpro (Snidhi) Date: 2019-04-30 11:36
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);
msg341152 - (view) Author: Windson Yang (Windson Yang) * Date: 2019-04-30 13:23
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.
msg341157 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-04-30 16:31
This seems like a duplicate (or at least very similar) to the issue 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.
msg341236 - (view) Author: Windson Yang (Windson Yang) * Date: 2019-05-02 00:06
Thanks, SilentGhost, you are right. I will leave this to a Windows expert instead.
msg342866 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-05-19 17:13
I'm going to close this issue as a duplicate of #29097. If you're still experience this problem on python3.7, please re-open.
msg364801 - (view) Author: Ard Kuijpers (Ard Kuijpers) Date: 2020-03-22 12:31
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 #29097 since I cannot reproduce that issue.
msg364866 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2020-03-23 17:27
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.
msg364907 - (view) Author: Jonathan Hsu (Jonathan Hsu) * Date: 2020-03-23 23:32
I'd like to take on this issue if no one else is working on it.
msg364918 - (view) Author: Jonathan Hsu (Jonathan Hsu) * Date: 2020-03-24 05:43
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 #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?
msg364921 - (view) Author: Ard Kuijpers (Ard Kuijpers) Date: 2020-03-24 08:53
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.
History
Date User Action Args
2020-03-24 08:53:35Ard Kuijperssetmessages: + msg364921
2020-03-24 05:43:15Jonathan Hsusetmessages: + msg364918
2020-03-23 23:32:03Jonathan Hsusetmessages: + msg364907
2020-03-23 23:13:27Jonathan Hsusetnosy: + Jonathan Hsu
2020-03-23 17:27:54SilentGhostsetstatus: closed -> open

superseder: [Windows] datetime.fromtimestamp(t) when 0 <= t <= 86399 fails on Python 3.6 ->

title: datetime: astimezone() results in OSError: [Errno 22] Invalid argument -> astimezone() fails on Windows for pre-epoch times
nosy: + belopolsky, p-ganssle
versions: + Python 3.8, Python 3.9, - Python 3.6
messages: + msg364866
resolution: duplicate ->
stage: resolved -> test needed
2020-03-22 12:31:06Ard Kuijperssetnosy: + Ard Kuijpers

messages: + msg364801
versions: + Python 3.7
2019-05-19 17:13:23SilentGhostsetstatus: pending -> closed
superseder: [Windows] datetime.fromtimestamp(t) when 0 <= t <= 86399 fails on Python 3.6
messages: + msg342866

resolution: duplicate
stage: resolved
2019-05-11 09:43:09SilentGhostsetstatus: open -> pending
2019-05-02 00:06:37Windson Yangsetmessages: + msg341236
2019-04-30 16:31:16SilentGhostsetnosy: + SilentGhost
messages: + msg341157
2019-04-30 13:23:55Windson Yangsetnosy: + Windson Yang
messages: + msg341152
2019-04-30 11:36:26Snidhicreate