GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-30155: Add macros to get tzinfo from datetime instances by ZackerySpytz · Pull Request #21633 · python/cpython

bpo-30155: Add macros to get tzinfo from datetime instances #21633

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.

Sign up for GitHub

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

Merged
pganssle merged 2 commits into python:master from ZackerySpytz:bpo-30155-tzinfo-macros
Sep 23, 2020

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-30155: Add macros to get tzinfo from datetime instances #21633

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
e018229
bpo-30155: Add macros to get tzinfo from datetime instances
ZackerySpytz Jul 27, 2020
83db28d
Use timezone.utc
ZackerySpytz Jul 28, 2020
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

11 changes: 11 additions & 0 deletions 11 Doc/c-api/datetime.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ must not be ``NULL``, and the type is not checked:

Return the microsecond, as an int from 0 through 999999.

.. c:function:: PyObject* PyDateTime_DATE_GET_TZINFO(PyDateTime_DateTime *o)

Return the tzinfo (which may be ``None``).

.. versionadded:: 3.10

Macros to extract fields from time objects. The argument must be an instance of
:c:data:`PyDateTime_Time`, including subclasses. The argument must not be ``NULL``,
Expand All @@ -209,6 +214,12 @@ and the type is not checked:

Return the microsecond, as an int from 0 through 999999.

.. c:function:: PyObject* PyDateTime_TIME_GET_TZINFO(PyDateTime_Time *o)

Return the tzinfo (which may be ``None``).

.. versionadded:: 3.10


Macros to extract fields from time delta objects. The argument must be an
instance of :c:data:`PyDateTime_Delta`, including subclasses. The argument must
Expand Down
6 changes: 6 additions & 0 deletions 6 Doc/whatsnew/3.10.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ New Features
Python executable.
(Contributed by Victor Stinner in :issue:`23427`.)

* The :c:func:`PyDateTime_DATE_GET_TZINFO` and
:c:func:`PyDateTime_TIME_GET_TZINFO` macros have been added for accessing
the ``tzinfo`` attributes of :class:`datetime.datetime` and
:class:`datetime.time` objects.
(Contributed by Zackery Spytz in :issue:`30155`.)

Porting to Python 3.10
----------------------

Expand Down
8 changes: 8 additions & 0 deletions 8 Include/datetime.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ typedef struct


/* Apply for date and datetime instances. */

// o is a pointer to a time or a datetime object.
#define _PyDateTime_HAS_TZINFO(o) (((_PyDateTime_BaseTZInfo *)(o))->hastzinfo)

#define PyDateTime_GET_YEAR(o) ((((PyDateTime_Date*)o)->data[0] << 8) | \
((PyDateTime_Date*)o)->data[1])
#define PyDateTime_GET_MONTH(o) (((PyDateTime_Date*)o)->data[2])
Expand All @@ -128,6 +132,8 @@ typedef struct
(((PyDateTime_DateTime*)o)->data[8] << 8) | \
((PyDateTime_DateTime*)o)->data[9])
#define PyDateTime_DATE_GET_FOLD(o) (((PyDateTime_DateTime*)o)->fold)
#define PyDateTime_DATE_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
((PyDateTime_DateTime *)(o))->tzinfo : Py_None)

/* Apply for time instances. */
#define PyDateTime_TIME_GET_HOUR(o) (((PyDateTime_Time*)o)->data[0])
Expand All @@ -138,6 +144,8 @@ typedef struct
(((PyDateTime_Time*)o)->data[4] << 8) | \
((PyDateTime_Time*)o)->data[5])
#define PyDateTime_TIME_GET_FOLD(o) (((PyDateTime_Time*)o)->fold)
#define PyDateTime_TIME_GET_TZINFO(o) (_PyDateTime_HAS_TZINFO(o) ? \
((PyDateTime_Time *)(o))->tzinfo : Py_None)

/* Apply for time delta instances */
#define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days)
Expand Down
14 changes: 10 additions & 4 deletions 14 Lib/test/datetimetester.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5991,30 +5991,36 @@ class DateTimeSubclass(datetime):

for klass in [datetime, DateTimeSubclass]:
for args in [(1993, 8, 26, 22, 12, 55, 99999),
(1993, 8, 26, 22, 12, 55, 99999)]:
(1993, 8, 26, 22, 12, 55, 99999,
timezone.utc)]:
d = klass(*args)
with self.subTest(cls=klass, date=args):
hour, minute, second, microsecond = _testcapi.PyDateTime_DATE_GET(d)
hour, minute, second, microsecond, tzinfo = \
_testcapi.PyDateTime_DATE_GET(d)

self.assertEqual(hour, d.hour)
self.assertEqual(minute, d.minute)
self.assertEqual(second, d.second)
self.assertEqual(microsecond, d.microsecond)
self.assertIs(tzinfo, d.tzinfo)

def test_PyDateTime_TIME_GET(self):
class TimeSubclass(time):
pass

for klass in [time, TimeSubclass]:
for args in [(12, 30, 20, 10), (12, 30, 20, 10)]:
for args in [(12, 30, 20, 10),
(12, 30, 20, 10, timezone.utc)]:
d = klass(*args)
with self.subTest(cls=klass, date=args):
hour, minute, second, microsecond = _testcapi.PyDateTime_TIME_GET(d)
hour, minute, second, microsecond, tzinfo = \
_testcapi.PyDateTime_TIME_GET(d)

self.assertEqual(hour, d.hour)
self.assertEqual(minute, d.minute)
self.assertEqual(second, d.second)
self.assertEqual(microsecond, d.microsecond)
self.assertIs(tzinfo, d.tzinfo)

def test_timezones_offset_zero(self):
utc0, utc1, non_utc = _testcapi.get_timezones_offset_zero()
Expand Down
3 changes: 3 additions & 0 deletions 3 Misc/NEWS.d/next/C API/2020-07-26-19-39-45.bpo-30155.rHZRJ_.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add :c:func:`PyDateTime_DATE_GET_TZINFO` and
:c:func:`PyDateTime_TIME_GET_TZINFO` macros for accessing the ``tzinfo``
attributes of :class:`datetime.datetime` and :class:`datetime.time` objects.
11 changes: 3 additions & 8 deletions 11 Modules/_datetimemodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,9 @@ class datetime.IsoCalendarDate "PyDateTime_IsoCalendarDate *" "&PyDateTime_IsoCa
#define SET_TD_SECONDS(o, v) ((o)->seconds = (v))
#define SET_TD_MICROSECONDS(o, v) ((o)->microseconds = (v))

/* p is a pointer to a time or a datetime object; HASTZINFO(p) returns
* p->hastzinfo.
*/
#define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo)
#define GET_TIME_TZINFO(p) (HASTZINFO(p) ? \
((PyDateTime_Time *)(p))->tzinfo : Py_None)
#define GET_DT_TZINFO(p) (HASTZINFO(p) ? \
((PyDateTime_DateTime *)(p))->tzinfo : Py_None)
#define HASTZINFO _PyDateTime_HAS_TZINFO
#define GET_TIME_TZINFO PyDateTime_TIME_GET_TZINFO
#define GET_DT_TZINFO PyDateTime_DATE_GET_TZINFO
/* M is a char or int claiming to be a valid month. The macro is equivalent
* to the two-sided Python test
* 1 <= M <= 12
Expand Down
6 changes: 4 additions & 2 deletions 6 Modules/_testcapimodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2677,8 +2677,9 @@ test_PyDateTime_DATE_GET(PyObject *self, PyObject *obj)
minute = PyDateTime_DATE_GET_MINUTE(obj);
second = PyDateTime_DATE_GET_SECOND(obj);
microsecond = PyDateTime_DATE_GET_MICROSECOND(obj);
PyObject *tzinfo = PyDateTime_DATE_GET_TZINFO(obj);

return Py_BuildValue("(llll)", hour, minute, second, microsecond);
return Py_BuildValue("(llllO)", hour, minute, second, microsecond, tzinfo);
}

static PyObject *
Expand All @@ -2690,8 +2691,9 @@ test_PyDateTime_TIME_GET(PyObject *self, PyObject *obj)
minute = PyDateTime_TIME_GET_MINUTE(obj);
second = PyDateTime_TIME_GET_SECOND(obj);
microsecond = PyDateTime_TIME_GET_MICROSECOND(obj);
PyObject *tzinfo = PyDateTime_TIME_GET_TZINFO(obj);

return Py_BuildValue("(llll)", hour, minute, second, microsecond);
return Py_BuildValue("(llllO)", hour, minute, second, microsecond, tzinfo);
}

static PyObject *
Expand Down
4 changes: 1 addition & 3 deletions 4 Modules/_zoneinfo.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,7 @@ zoneinfo_tzname(PyObject *self, PyObject *dt)
return tti->tzname;
}

#define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo)
#define GET_DT_TZINFO(p) \
(HASTZINFO(p) ? ((PyDateTime_DateTime *)(p))->tzinfo : Py_None)
#define GET_DT_TZINFO PyDateTime_DATE_GET_TZINFO
Comment thread Outdated

@pganssle pganssle Jul 27, 2020

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we can replace all the instances of GET_DT_TZINFO with PyDateTime_DATE_GET_TZINFO. I think either way works, because in the backport I'm either going to do:

#ifdef PyDateTime_DATE_GET_TZINFO
#define GET_DT_TZINFO PyDateTime_DATE_GET_TZINFO
#else
#define HASTZINFO ...

or I'll do this:

#ifndef PyDateTime_DATE_GET_TZINFO
#define _PyDateTime_HAS_TZINFO ...
#define PyDateTime_DATE_GET_TZINFO (_PyDateTime_HAS_TZINFO(p) ? ...

Then the rest of the file will be the same.

I was originally liking the first one, but upon further reflection, I like the second one. What do you think? (It's also OK to say that you like the second one more but you want to keep it out of scope of this PR).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ZackerySpytz ZackerySpytz Jul 29, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the second one more, but I think it is out of scope for this PR. :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

static PyObject *
zoneinfo_fromutc(PyObject *obj_self, PyObject *dt)
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.