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
Add ability to get tzinfo from a datetime instance in C API #74341
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=<Date2020-10-21.19:51:20.376>created_at=<Date2017-04-24.15:27:26.203>labels= ['expert-C-API', 'type-feature', 'library', '3.10']
title='Add ability to get tzinfo from a datetime instance in C API'updated_at=<Date2020-10-21.19:51:20.376>user='https://bugs.python.org/atuining'
Right now there is no documented way to create a datetime instance with a tzinfo instance. The documented macros all hard code the value Py_None for the tzinfo parameter. Using the PyObject_Call() method instead of the macro for creating a datetime instance is ~5x slower.
In addition, there is no macro or method for getting the tzinfo from an existing datetime instance. Perhaps creating DATE_GET_TZINFO and TIME_GET_TZINFO would be acceptable?
Hmm. I never noticed this. In the past I have used the (undocumented) PyDateTimeAPI struct, which the official macros wrap. I'm not sure how official that struct is considering it doesn't show up in the documentation.
I agree that it should be possible to construct TZ-aware datetimes using the official C API, so I guess the question is whether to add a bunch more macros or document the existing struct.
An official accessor for the tzinfo component of an existing datetime, which I think is very reasonable in light of the fact that there are official accessors for all the other components of a datetime.
An official constructor for a timezone-aware datetime, which I think basically exists in the form of PyDatetime_CAPI->PyDateTimeAPI->DateTime_FromDateAndTime / ->DateTime_FromDateAndTimeAndFold, and we just need to document it. I think this is basically a separate issue, and I have opened bpo-39604 to track it.
I'm going to rename this bug to focus only on issue #1. I think we can accept a PR adding two new macros. I would suggest calling them:
PyDateTime_DATE_GET_TZINFO
PyDateTime_TIME_GET_TZINFO
Please make sure to add tests to any PR you make. See the CapiTest case (
) for examples. You may want to look at the git blame for a few of those tests to see the PRs that they were added in, since part of the tests are defined in a C file.
(As an aside: I don't love that the accessor methods are not available on the struct, since all the "macro-only" code needs to be re-implemented in all other-language bindings. Since the accessors are already all macro-only, though, might as well keep with the tradition for now :P)
changed the title [-]Add ability to get/set tzinfo on datetime instances in C API[/-][+]Add ability to get tzinfo from a datetime instance in C API[/+]on Feb 10, 2020
Activity
atuining commentedon Apr 24, 2017
Right now there is no documented way to create a datetime instance with a tzinfo instance. The documented macros all hard code the value Py_None for the tzinfo parameter. Using the PyObject_Call() method instead of the macro for creating a datetime instance is ~5x slower.
In addition, there is no macro or method for getting the tzinfo from an existing datetime instance. Perhaps creating DATE_GET_TZINFO and TIME_GET_TZINFO would be acceptable?
The enhancement 10381 (http://bugs.python.org/issue10381) would also be needed.
I can provide a GitHub PR if that would be helpful. I first want to make sure that such an effort would be appreciated!
pganssle commentedon Jul 5, 2018
Hmm. I never noticed this. In the past I have used the (undocumented) PyDateTimeAPI struct, which the official macros wrap. I'm not sure how official that struct is considering it doesn't show up in the documentation.
I agree that it should be possible to construct TZ-aware datetimes using the official C API, so I guess the question is whether to add a bunch more macros or document the existing struct.
atuining commentedon Feb 10, 2020
Any progress on this?
pganssle commentedon Feb 10, 2020
So this bug is asking for two things:
An official accessor for the
tzinfocomponent of an existing datetime, which I think is very reasonable in light of the fact that there are official accessors for all the other components of a datetime.An official constructor for a timezone-aware datetime, which I think basically exists in the form of PyDatetime_CAPI->PyDateTimeAPI->DateTime_FromDateAndTime / ->DateTime_FromDateAndTimeAndFold, and we just need to document it. I think this is basically a separate issue, and I have opened bpo-39604 to track it.
I'm going to rename this bug to focus only on issue #1. I think we can accept a PR adding two new macros. I would suggest calling them:
Please make sure to add tests to any PR you make. See the CapiTest case (
cpython/Lib/test/datetimetester.py
Line 5914 in d68e0a8
(As an aside: I don't love that the accessor methods are not available on the struct, since all the "macro-only" code needs to be re-implemented in all other-language bindings. Since the accessors are already all macro-only, though, might as well keep with the tradition for now :P)
[-]Add ability to get/set tzinfo on datetime instances in C API[/-][+]Add ability to get tzinfo from a datetime instance in C API[/+]pitrou commentedon Jul 20, 2020
+1 for this.
ZackerySpytz commentedon Jul 27, 2020
I have created PR 21633 to add these macros. Please consider taking a look.
pganssle commentedon Sep 23, 2020
New changeset 2e4dd33 by Zackery Spytz in branch 'master':
bpo-30155: Add macros to get tzinfo from datetime instances (GH-21633)
2e4dd33