Skip to content

gh-103982: Set fold for result in datetime.astimezone #130658

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,13 @@ def astimezone(self, tz=None):
utc = (self - myoffset).replace(tzinfo=tz)

# Convert from UTC to tz's local time.
return tz.fromutc(utc)
result = tz.fromutc(utc)

# Set fold if there is discrepancy between timestamp and timezone.
if tz.utcoffset(result) != myoffset:
result = result.replace(fold=1)

return result

# Ways to produce a string.

Expand Down
23 changes: 23 additions & 0 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6754,6 +6754,29 @@ datetime_astimezone(PyObject *op, PyObject *args, PyObject *kw)
PyObject_CallMethodOneArg(tzinfo, &_Py_ID(fromutc), temp);
Py_DECREF(temp);

/* Set fold if there is discrepancy between timestamp and timezone. */
if (result != NULL) {
offset = call_utcoffset(tzinfo, (PyObject *)result);
if (offset == NULL || offset == Py_None || !PyDelta_Check(offset)) {
Py_XDECREF(offset);
goto naive;
}

PyObject *myoffset = call_utcoffset(tzinfo, (PyObject *)self);
if (myoffset == NULL || myoffset == Py_None || !PyDelta_Check(myoffset)) {
Py_XDECREF(offset);
Py_XDECREF(myoffset);
return NULL;
}

if (delta_cmp(offset, myoffset) == 0) {
DATE_SET_FOLD(result, 1);
}

Py_XDECREF(myoffset);
Py_XDECREF(offset);
}

return (PyObject *)result;
}

Expand Down
Loading