Skip to content

gh-69142: add %:z strftime format code #95983

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

Merged
merged 17 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 0 additions & 1 deletion Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2390,7 +2390,6 @@ requires, and these work on all platforms with a standard C implementation.
| | ``±HH:MM[:SS[.ffffff]]`` | -04:00, +10:30, | |
| | (empty string if the object is | +06:34:15, | |
| | naive). | -03:07:12.345216 | |
| | .. versionadded:: 3.12 | | |
+-----------+--------------------------------+------------------------+-------+
| ``%Z`` | Time zone name (empty string | (empty), UTC, GMT | \(6) |
| | if the object is naive). | | |
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
add %:z strftime format code (generates tzoffset with colons as separator)
Add ``%:z`` strftime format code (generates tzoffset with colons as separator), see :ref:`strftime-strptime-behavior`.
17 changes: 6 additions & 11 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1511,25 +1511,20 @@ make_somezreplacement(PyObject *object, char *sep, PyObject *tzinfoarg)
{
char buf[100];
PyObject *tzinfo = get_tzinfo_member(object);
PyObject *replacement = PyBytes_FromStringAndSize(NULL, 0);

if (replacement == NULL)
return NULL;
if (tzinfo == Py_None || tzinfo == NULL)
return replacement;

Py_DECREF(replacement);

if (tzinfo == Py_None || tzinfo == NULL) {
return PyBytes_FromStringAndSize(NULL, 0);
}

assert(tzinfoarg != NULL);
if (format_utcoffset(buf,
sizeof(buf),
sep,
tzinfo,
tzinfoarg) < 0)
return NULL;

replacement = PyBytes_FromStringAndSize(buf, strlen(buf));
return replacement;

return PyBytes_FromStringAndSize(buf, strlen(buf));
}

static PyObject *
Expand Down