Skip to content

Commit 8cc0d75

Browse files
Fix stuff
1 parent c72d219 commit 8cc0d75

3 files changed

Lines changed: 12 additions & 17 deletions

File tree

Doc/library/datetime.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,12 +1601,12 @@ Instance methods:
16011601
'2015-01-01T12:30:59.000000'
16021602

16031603
If the optional argument *use_utc_designator* is set to :const:`True` and
1604-
:meth:`tzname` returns exactly ``"UTC"``, then "Z" will be given as the UTC
1604+
:meth:`tzname` returns exactly ``'UTC'``, then 'Z' will be given as the
16051605
offset in the formatted string.
16061606

1607-
.. versionadded:: 3.6
1607+
.. versionchanged:: 3.6
16081608
Added the *timespec* argument.
1609-
.. versionadded:: next
1609+
.. versionchanged:: next
16101610
Added the *use_utc_designator* argument.
16111611

16121612
.. method:: datetime.__str__()
@@ -2008,7 +2008,7 @@ Instance methods:
20082008

20092009
.. versionchanged:: 3.6
20102010
Added the *timespec* parameter.
2011-
.. versionadded:: next
2011+
.. versionchanged:: next
20122012
Added the *use_utc_designator* argument.
20132013

20142014

Lib/_pydatetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ def isoformat(self, timespec='auto', use_utc_designator=False):
16141614
s = _format_time(self._hour, self._minute, self._second,
16151615
self._microsecond, timespec)
16161616

1617-
if use_utc_designator and self.tzname() == 'UTC':
1617+
if use_utc_designator and (self.tzinfo is timezone.utc or self.tzname() == 'UTC'):
16181618
s += 'Z'
16191619
else:
16201620
tz = self._tzstr()

Modules/_datetimemodule.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,8 +1700,8 @@ static PyObject *delta_negative(PyObject *op);
17001700

17011701
/* Add formatted UTC offset string to buf. buf has no more than
17021702
* buflen bytes remaining. If use_utc_designator is true,
1703-
* tzinfo.tzname(tzinfoarg) will be called, and if it returns "UTC",
1704-
* only "Z\0" will be added. Otherwise, the UTC offset is gotten by calling
1703+
* tzinfo.tzname(tzinfoarg) will be called, and if it returns 'UTC',
1704+
* only 'Z\0' will be added. Otherwise, the UTC offset is gotten by calling
17051705
* tzinfo.utcoffset(tzinfoarg). If that returns None, \0 is stored into
17061706
* *buf, and that's all. Else the returned value is checked for sanity (an
17071707
* integer in range), and if that's OK it's converted to an hours & minutes
@@ -1712,8 +1712,8 @@ static PyObject *delta_negative(PyObject *op);
17121712
*/
17131713
static int
17141714
format_utcoffset(char *buf, size_t buflen, const char *sep,
1715-
int use_utc_designator,
1716-
PyObject *tzinfo, PyObject *tzinfoarg)
1715+
int use_utc_designator,
1716+
PyObject *tzinfo, PyObject *tzinfoarg)
17171717
{
17181718
PyObject *offset;
17191719
int hours, minutes, seconds, microseconds;
@@ -1722,14 +1722,8 @@ format_utcoffset(char *buf, size_t buflen, const char *sep,
17221722
assert(buflen >= 1);
17231723

17241724
if (use_utc_designator) {
1725-
PyObject* name = PyObject_CallMethod(tzinfo, "tzname", "O", tzinfoarg);
1726-
if (name == NULL)
1727-
return -1;
1728-
int tz_is_utc = (PyUnicode_Check(name) &&
1729-
0 == strcmp("UTC", PyUnicode_AsUTF8(name)));
1730-
Py_DECREF(name);
1731-
1732-
if (tz_is_utc) {
1725+
PyObject *name = PyObject_CallMethod(tzinfo, "tzname", "O", tzinfoarg);
1726+
if (PyUnicode_Check(name) && strcmp("UTC", PyUnicode_AsUTF8(name))) {
17331727
PyOS_snprintf(buf, buflen, "Z");
17341728
return 0;
17351729
}
@@ -1787,6 +1781,7 @@ make_somezreplacement(PyObject *object, char *sep, PyObject *tzinfoarg)
17871781
if (format_utcoffset(buf,
17881782
sizeof(buf),
17891783
sep,
1784+
0,
17901785
tzinfo,
17911786
tzinfoarg) < 0)
17921787
return NULL;

0 commit comments

Comments
 (0)