Skip to content

Commit bc441ed

Browse files
karlcowmiss-islington
authored andcommitted
bpo-22377: Fixes documentation for %Z in datetime (GH-16507)
This fixes the issue discussed in https://bugs.python.org/issue22377 and fixes it according to the comments made by Paul Ganssle @pganssle * It clarifies which values are acceptable in the table * It extends the note with a clearer information on the valid values https://bugs.python.org/issue22377
1 parent 036fe85 commit bc441ed

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Doc/library/datetime.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ requires, and these work on all platforms with a standard C implementation.
23622362
| | string if the object is | +063415, | |
23632363
| | naive). | -030712.345216 | |
23642364
+-----------+--------------------------------+------------------------+-------+
2365-
| ``%Z`` | Time zone name (empty string | (empty), UTC, EST, CST | |
2365+
| ``%Z`` | Time zone name (empty string | (empty), UTC, GMT | \(6) |
23662366
| | if the object is naive). | | |
23672367
+-----------+--------------------------------+------------------------+-------+
23682368
| ``%j`` | Day of the year as a | 001, 002, ..., 366 | \(9) |
@@ -2533,9 +2533,18 @@ Notes:
25332533
In addition, providing ``'Z'`` is identical to ``'+00:00'``.
25342534

25352535
``%Z``
2536-
If :meth:`tzname` returns ``None``, ``%Z`` is replaced by an empty
2537-
string. Otherwise ``%Z`` is replaced by the returned value, which must
2538-
be a string.
2536+
In :meth:`strftime`, ``%Z`` is replaced by an empty string if
2537+
:meth:`tzname` returns ``None``; otherwise ``%Z`` is replaced by the
2538+
returned value, which must be a string.
2539+
2540+
:meth:`strptime` only accepts certain values for ``%Z``:
2541+
2542+
1. any value in ``time.tzname`` for your machine's locale
2543+
2. the hard-coded values ``UTC`` and ``GMT``
2544+
2545+
So someone living in Japan may have ``JST``, ``UTC``, and ``GMT`` as
2546+
valid values, but probably not ``EST``. It will raise ``ValueError`` for
2547+
invalid values.
25392548

25402549
.. versionchanged:: 3.2
25412550
When the ``%z`` directive is provided to the :meth:`strptime` method, an
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improves documentation of the values that :meth:`datetime.datetime.strptime` accepts for ``%Z``.
2+
Patch by Karl Dubost.

0 commit comments

Comments
 (0)