Skip to content

Behavior of datetime.datetime.strptime differs from documented behavior on %z #122781

Closed
@Thomasillo

Description

@Thomasillo

Bug report

Bug description:

import datetime
datetime.datetime.strptime("2024-03-21T15:49:51", '%Y-%m-%dT%H:%M:%S%z')

will fail with ValueError: time data '2024-03-21T15:49:51' does not match format '%Y-%m-%dT%H:%M:%S%z'

From the documentation for strptime, empty string should be allowed here.

%z UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive). (empty), +0000, -0400, +1030, +063415, -030712.345216 (6)

NB: strftime behaves correctly.

 >>> datetime.datetime.strftime(datetime.datetime(2024, 3, 21, 15, 49, 51),  '%Y-%m-%dT%H:%M:%S%z')
'2024-03-21T15:49:51'

So strptime(strftime(datetime.datetime(2024, 3, 21, 15, 49, 51), '%Y-%m-%dT%H:%M:%S%z'), '%Y-%m-%dT%H:%M:%S%z') would fail.

CPython 3.12.4, WSL2 (Ubuntu 20.04)

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Activity

pygeek

pygeek commented on Aug 8, 2024

@pygeek
Contributor
midnightstardust

midnightstardust commented on Aug 8, 2024

@midnightstardust

Linked a PR where I made %z allow an optional utc offset instead of mandating a utc offset, would appreciate it if someone could take a look, thanks!

#122829

zuo

zuo commented on Oct 3, 2024

@zuo
Contributor

Making %z-parsing accept an empty string looks to me like a serious behavioral change, as it is almost obvious to me that non-negligible number of users rely on %z to distinguish and accept only a timezone-aware date+time (rejecting timezone-less aka naive ones).

cc @pganssle

zuo

zuo commented on Oct 3, 2024

@zuo
Contributor

(I believe that rejecting %z when formatting a naive (tz-unaware) datetime would be a better idea – but now, in 2024, is probably a non-starter, because of the incompatibility impact. Although, perhaps after full 5-years deprecation period it would be acceptable?)

added a commit that references this issue on May 20, 2025

gh-122781: Allow empty offset for `%z` in `strptime` (#132922)

99b5808
StanFromIreland

StanFromIreland commented on May 20, 2025

@StanFromIreland
Member

cc @pganssle This can be closed

added a commit that references this issue on May 26, 2025

pythongh-122781: Allow empty offset for `%z` in `strptime` (python#13…

1 remaining item

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Behavior of datetime.datetime.strptime differs from documented behavior on %z · Issue #122781 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant