Skip to content

fix(parser): preserve sign for UTC/GMT/Z offset notation#1479

Open
BlocksecPHD wants to merge 3 commits intodateutil:masterfrom
BlocksecPHD:fix/utc-offset-sign-1478
Open

fix(parser): preserve sign for UTC/GMT/Z offset notation#1479
BlocksecPHD wants to merge 3 commits intodateutil:masterfrom
BlocksecPHD:fix/utc-offset-sign-1478

Conversation

@BlocksecPHD
Copy link
Copy Markdown

Problem

When parsing datetime strings like '2026-03-11 14:32:45 UTC-4', the parser incorrectly reversed the offset sign, resulting in +04:00 instead of the expected -04:00.

Root Cause

The parser applied POSIX timezone convention to UTC zone names (UTC, GMT, Z). In POSIX, EST5 means the timezone is UTC-5 (not UTC+5), and GMT+3 would mean UTC-3. However, modern UTC offset notation like 'UTC-4' should mean UTC minus 4 hours literally, not POSIX convention.

Fix

This PR distinguishes between UTC zone names (UTC, GMT, Z) and other timezone abbreviations:

  • For UTC/GMT/Z: preserve the literal offset sign ('UTC-4'-04:00)
  • For other abbreviations: keep POSIX convention as before

Test Cases

parse('2026-03-11 14:32:45 UTC-4')  # → -04:00 (fixed)
parse('2026-03-11 14:32:45 UTC+4')  # → +04:00 (fixed)
parse('2026-03-11 14:32:45 GMT-5')  # → -05:00 (fixed)
parse('2026-03-11 14:32:45 Z+4')    # → +04:00 (fixed)

Added regression test test_utc_offset_not_reversed covering various UTC/GMT/Z offset combinations.

Fixes: #1478

Funan Zhou added 3 commits April 9, 2026 05:10
Previously, UTC/GMT/Z followed by +/- offset incorrectly used
POSIX convention reversal, causing 'UTC-4' to be parsed as +04:00
instead of -04:00.

For timezone abbreviations like 'BRST+3', the POSIX convention
is correct (the timezone is UTC-3). But for UTC offset notation
like 'UTC-4', the sign should be literal (UTC minus 4 hours).

This fix distinguishes between UTC zone names (UTC, GMT, Z) and
other timezone abbreviations, preserving the literal sign for
the former while keeping POSIX convention for the latter.

Fixes dateutil#1478
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.

1 participant