Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 1 addition & 0 deletions changelog.d/1492.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``tzstr()`` raising ``TypeError`` when parsing bare ``GMT`` or ``UTC`` names without an explicit offset.
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ def pointer_mock(*args, **kwargs):
r'https://pgp.mit.edu',
# MetaCPAN frequently returns 402 for automated link checkers
r"https://metacpan.org/.*",
# Planetary Rings Node frequently times out for automated link checkers
r"https://pds-rings.seti.org(:443)?/.*",
]

# Reduce problems with ephemeral failures
Expand Down
3 changes: 1 addition & 2 deletions docs/exercises/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ To solve this exercise, copy-paste this script into a document, change anything
Parsing a local tzname
----------------------

Three-character time zone abbreviations are *not* unique in that they do not explicitly map to a time zone. A list of time zone abbreviations in use can be found `here <https://www.timeanddate.com/time/zones/>`_. This means that parsing a datetime string such as ``'2018-01-01 12:30:30 CST'`` is ambiguous without context. Using :mod:`dateutil.parser` and :mod:`dateutil.tz`, it is possible to provide a context such that these local names are converted to proper time zones.
Three-character time zone abbreviations are *not* unique in that they do not explicitly map to a time zone. A list of time zone abbreviations in use can be found `here <https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations>`_. This means that parsing a datetime string such as ``'2018-01-01 12:30:30 CST'`` is ambiguous without context. Using :mod:`dateutil.parser` and :mod:`dateutil.tz`, it is possible to provide a context such that these local names are converted to proper time zones.

Problem 1
*********
Expand Down Expand Up @@ -239,4 +239,3 @@ To solve this exercise, copy-paste this script into a document, change anything
.. raw:: html

</details>

10 changes: 7 additions & 3 deletions src/dateutil/tz/tz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ class tzstr(tzrange):
support is removed in a future version.

.. _`GNU C Library: TZ Variable`:
https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
https://sourceware.org/glibc/manual/latest/html_node/TZ-Variable.html
"""
def __init__(self, s, posix_offset=False):
global parser
Expand All @@ -1088,7 +1088,11 @@ def __init__(self, s, posix_offset=False):

# Here we break the compatibility with the TZ variable handling.
# GMT-3 actually *means* the timezone -3.
if res.stdabbr in ("GMT", "UTC") and not posix_offset:
if (
res.stdabbr in ("GMT", "UTC")
and res.stdoffset is not None
and not posix_offset
):
res.stdoffset *= -1

# We must initialize it first, since _delta() needs
Expand Down Expand Up @@ -1537,7 +1541,7 @@ class GettzFunc(object):


.. _`TZ variable`:
https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
https://sourceware.org/glibc/manual/latest/html_node/TZ-Variable.html

.. _`"same zone" semantics`:
https://blog.ganssle.io/articles/2018/02/aware-datetime-arithmetic.html
Expand Down
8 changes: 8 additions & 0 deletions tests/test_tz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,14 @@ def testPosixOffset(self):
self.assertEqual(datetime(2015, 1, 1, tzinfo=TZ2).utcoffset(),
timedelta(hours=+3))

def testBareUtcGmtOffset(self):
for tz_str in ("GMT", "UTC"):
TZ = tz.tzstr(tz_str)
dt = datetime(2015, 1, 1, tzinfo=TZ)

self.assertEqual(dt.tzname(), tz_str)
self.assertEqual(dt.utcoffset(), timedelta(0))

def testStrInequalityUnsupported(self):
TZS = tz.tzstr('EST5EDT')

Expand Down
Loading