Skip to content

Resolve datetime deprecation warnings #2589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged

Conversation

emmanuel-ferdman
Copy link
Contributor

PR Summary

This PR fixes the datetime deprecation warnings that can be viewed in the CI logs:

test/sasl/test_msk.py::test_aws_msk_iam_client_temporary_credentials
  /home/runner/work/kafka-python/kafka-python/test/sasl/test_msk.py:13: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
    now = datetime.datetime.utcfromtimestamp(1629321911)

Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
@dpkp
Copy link
Owner

dpkp commented Apr 16, 2025

Can we modify to keep support for python2.7 ?

@emmanuel-ferdman
Copy link
Contributor Author

@dpkp If Python2.7 support is required, I think we could go with something like:

import sys

if sys.version_info >= (3, 3):
    # Use timezone-aware datetime for Python 3.3+
    now = datetime.datetime.fromtimestamp(1629321911, datetime.timezone.utc)
else:
    # Fallback to utcfromtimestamp for Python 2.7
    now = datetime.datetime.utcfromtimestamp(1629321911)

Not the most elegant solution, but it should get the job done while supporting Python 2.7 and future Python versions. What do you think?

@dpkp
Copy link
Owner

dpkp commented Apr 16, 2025

That's great, thanks!

Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
@dpkp dpkp merged commit e2c3b80 into dpkp:master Apr 16, 2025
18 checks passed
@dpkp
Copy link
Owner

dpkp commented Apr 16, 2025

Thanks!

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