Skip to content

Replace deprecated datetime.utcnow() with timezone-aware equivalent - #3

Merged
ulikoehler merged 1 commit into
ulikoehler:masterfrom
mpasternak:fix/datetime-utcnow-deprecation
May 4, 2026
Merged

Replace deprecated datetime.utcnow() with timezone-aware equivalent#3
ulikoehler merged 1 commit into
ulikoehler:masterfrom
mpasternak:fix/datetime-utcnow-deprecation

Conversation

@mpasternak

Copy link
Copy Markdown
Collaborator

datetime.datetime.utcnow() is deprecated since Python 3.12 and scheduled for removal in a future version:

DeprecationWarning: datetime.datetime.utcnow() is deprecated and
scheduled for removal in a future version. Use timezone-aware
objects to represent datetimes in UTC:
datetime.datetime.now(datetime.UTC).

datetime_to_datestamp() in oaipmh/datestamp.py asserts that the input has tzinfo is None, so we convert the timezone-aware UTC value back to naive with .replace(tzinfo=None) before passing it in. The resulting responseDate string in the OAI-PMH response is unchanged.

This is the only datetime.utcnow() call in the codebase; after this change, pyoai no longer emits DeprecationWarning for it on Python 3.12+.

`datetime.datetime.utcnow()` is deprecated since Python 3.12 and
scheduled for removal in a future version:

    DeprecationWarning: datetime.datetime.utcnow() is deprecated and
    scheduled for removal in a future version. Use timezone-aware
    objects to represent datetimes in UTC:
    datetime.datetime.now(datetime.UTC).

`datetime_to_datestamp()` in `oaipmh/datestamp.py` asserts that the
input has `tzinfo is None`, so we convert the timezone-aware UTC
value back to naive with `.replace(tzinfo=None)` before passing it
in. The resulting `responseDate` string in the OAI-PMH response is
unchanged.

This is the only `datetime.utcnow()` call in the codebase; after
this change, pyoai no longer emits `DeprecationWarning` for it on
Python 3.12+.
@mpasternak

Copy link
Copy Markdown
Collaborator Author

Sup @ulikoehler long time no see, I'm bringing some updates to this package thanks to the power of LLM / AI, stay tuned.

@ulikoehler
ulikoehler merged commit 9f3d80e into ulikoehler:master May 4, 2026
0 of 4 checks passed
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.

2 participants