Skip to content

fix(parser): recognize UTC and GMT zone names case-insensitively - #1615

Open
saadkhantareen wants to merge 1 commit into
dateutil:masterfrom
saadkhantareen:fix/parser-utc-gmt-case-insensitive
Open

saadkhantareen wants to merge 1 commit into
dateutil:masterfrom
saadkhantareen:fix/parser-utc-gmt-case-insensitive

Conversation

@saadkhantareen

Copy link
Copy Markdown

Summary

parser.parse() rejected lowercase and mixed-case UTC zone designations, even though the same designations in uppercase parse fine and every other name lookup in the parser (months, weekdays, AM/PM, and parserinfo.utczone() itself) is case-insensitive:

>>> from dateutil.parser import parse
>>> parse("2021-04-19 09:00:42 UTC")
datetime.datetime(2021, 4, 19, 9, 0, 42, tzinfo=tzutc())
>>> parse("2021-04-19 09:00:42 utc")
ParserError: Unknown string format: 2021-04-19 09:00:42 utc

The root cause is in the parser's timezone-name recognition: _could_be_tzname() only accepted tokens that are entirely uppercase (or literally listed in parserinfo.UTCZONE), so utc, gmt, Utc and similar forms never reached the timezone handling at all. In addition, parserinfo.tzoffset() compared the name against the lowercased _utczone keys without normalizing case, so it returned 0 for utc but None for UTC.

Fixes #1117

Changes

  • src/dateutil/parser/_parser.py: _could_be_tzname() now also accepts a token when parserinfo.utczone() matches it, so UTC zone designations (UTC, GMT, Z) are recognized in any case. Other lowercase tokens (for example est or pst) are still not treated as timezone names, exactly as before.
  • src/dateutil/parser/_parser.py: parserinfo.tzoffset() now uses the case-insensitive utczone() helper instead of a case-sensitive membership test.
  • tests/test_parser.py: added parametrized regression tests covering bare designations in eight case variants (UTC, GMT, utc, gmt, Utc, Gmt, uTc, gMt) and signed-offset forms (utc+3, gmt-5) checked against their uppercase equivalents.
  • changelog.d/1117.bugfix.rst: added a news fragment.
  • AUTHORS.md: added the author.

Tests

  • python -m pytest tests/test_parser.py -k utczone -q: 12 passed.
  • python -m pytest tests/test_parser.py -q: 243 passed, 14 xfailed.
  • python -m pytest tests/ -q (excluding tests/property, which fails to collect in this environment because the zoneinfo data file is not built): 1989 passed, 47 skipped, 17 xfailed, 49 failed. The 49 failures are all in tests/test_tz.py, are caused by the same missing zoneinfo data file, and are identical on the unmodified master branch (49 failed, 1977 passed), so this change introduces no new failures.

Pull Request Checklist

  • Changes have tests
  • Authors have been added to AUTHORS.md
  • News fragment added in changelog.d. See CONTRIBUTING.md for details

Copilot AI balanced review requested due to automatic review settings October 17, 2026 10:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@saadkhantareen
saadkhantareen force-pushed the fix/parser-utc-gmt-case-insensitive branch from 9e60310 to 49333c1 Compare October 17, 2026 10:08

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: parse breaks when presented with a date in this format: %Y-%m-%d %H:%M:%S %Z

2 participants