Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
8 changes: 4 additions & 4 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2547,8 +2547,8 @@ requires, and these work on all platforms with a standard C implementation.
| | digits. | | |
+-----------+--------------------------------+------------------------+-------+
| ``%z`` | UTC offset in the form | (empty), +0000, | \(6) |
| | ``±HHMM[SS[.ffffff]]`` (empty | -0400, +1030, | |
| | string if the object is | +063415, | |
| | ``±HH[MM[SS[.ffffff]]]`` | -0400, +1030, | |
| | (empty string if the object is | +063415, | |
| | naive). | -030712.345216 | |
+-----------+--------------------------------+------------------------+-------+
| ``%Z`` | Time zone name (empty string | (empty), UTC, GMT | \(6) |
Expand Down Expand Up @@ -2609,7 +2609,7 @@ convenience. These parameters all correspond to ISO 8601 date values.
| | Jan 4. | | |
+-----------+--------------------------------+------------------------+-------+
| ``%:z`` | UTC offset in the form | (empty), +00:00, | \(6) |
| | ``±HH:MM[:SS[.ffffff]]`` | -04:00, +10:30, | |
| | ``±HH[:MM[:SS[.ffffff]]]`` | -04:00, +10:30, | |
| | (empty string if the object is | +06:34:15, | |
| | naive). | -03:07:12.345216 | |
+-----------+--------------------------------+------------------------+-------+
Expand Down Expand Up @@ -2707,7 +2707,7 @@ Notes:

``%z``
:meth:`~.datetime.utcoffset` is transformed into a string of the form
``±HHMM[SS[.ffffff]]``, where ``HH`` is a 2-digit string giving the number
``±HH[MM[SS[.ffffff]]]``, where ``HH`` is a 2-digit string giving the number
of UTC offset hours, ``MM`` is a 2-digit string giving the number of UTC
offset minutes, ``SS`` is a 2-digit string giving the number of UTC offset
seconds and ``ffffff`` is a 6-digit string giving the number of UTC
Expand Down
6 changes: 3 additions & 3 deletions Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def __init__(self, locale_time=None):
# W is set below by using 'U'
'y': r"(?P<y>\d\d)",
'Y': r"(?P<Y>\d\d\d\d)",
'z': r"(?P<z>([+-]\d\d:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?)|(?-i:Z))?",
'z': r"(?P<z>([+-]\d\d(:?[0-5]\d(:?[0-5]\d(\.\d{1,6})?)?)?)|(?-i:Z))?",
'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),
'B': self.__seqToRE(_fixmonths(self.locale_time.f_month[1:]), 'B'),
Expand Down Expand Up @@ -668,15 +668,15 @@ def parse_int(s):
if z == 'Z':
gmtoff = 0
else:
if z[3] == ':':
if len(z) != 3 and z[3] == ':':
z = z[:3] + z[4:]
if len(z) > 5:
if z[5] != ':':
msg = f"Inconsistent use of : in {found_dict['z']}"
raise ValueError(msg)
z = z[:5] + z[6:]
hours = int(z[1:3])
minutes = int(z[3:5])
minutes = int(z[3:5] or 0)
seconds = int(z[5:7] or 0)
gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds
gmtoff_remainder = z[8:]
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2889,6 +2889,8 @@ def test_strptime(self):

strptime = self.theclass.strptime

self.assertEqual(strptime("+01", "%z").utcoffset(), 1 * HOUR)
self.assertEqual(strptime("-10", "%z").utcoffset(), -10 * HOUR)
self.assertEqual(strptime("+0002", "%z").utcoffset(), 2 * MINUTE)
self.assertEqual(strptime("-0002", "%z").utcoffset(), -2 * MINUTE)
self.assertEqual(
Expand Down Expand Up @@ -4035,6 +4037,8 @@ def test_strptime(self):

def test_strptime_tz(self):
strptime = self.theclass.strptime
self.assertEqual(strptime("+01", "%z").utcoffset(), 1 * HOUR)
self.assertEqual(strptime("-10", "%z").utcoffset(), -10 * HOUR)
self.assertEqual(strptime("+0002", "%z").utcoffset(), 2 * MINUTE)
self.assertEqual(strptime("-0002", "%z").utcoffset(), -2 * MINUTE)
self.assertEqual(
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ def test_offset(self):
one_hour = 60 * 60
half_hour = 30 * 60
half_minute = 30
(*_, offset), _, offset_fraction = _strptime._strptime("+09", "%z")
self.assertEqual(offset, 9 * one_hour)
self.assertEqual(offset_fraction, 0)
(*_, offset), _, offset_fraction = _strptime._strptime("-01", "%z")
self.assertEqual(offset, -one_hour)
self.assertEqual(offset_fraction, 0)
(*_, offset), _, offset_fraction = _strptime._strptime("+0130", "%z")
self.assertEqual(offset, one_hour + half_hour)
self.assertEqual(offset_fraction, 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Update input time zone format from ``±HHMM[SS[.ffffff]]`` to
``±HH[MM[SS[.ffffff]]]`` for :meth:`datetime.date.strptime`,
:meth:`datetime.datetime.strptime` and :meth:`datetime.time.strptime` methods.
Patch by Semyon Moroz.
Loading