Skip to content
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

Python 3.12 update, datetime.utcnow() deprecation #508

Open
BerglundDaniel opened this issue Apr 23, 2024 · 2 comments
Open

Python 3.12 update, datetime.utcnow() deprecation #508

BerglundDaniel opened this issue Apr 23, 2024 · 2 comments
Labels
good first issue Good for newcomers

Comments

@BerglundDaniel
Copy link
Member

It seems that Python 3.12 will work mostly directly for us. The only thing we need to update is that datetime.datetime.utcnow() has warnings about being deprecated

@emanuelen5
Copy link
Member

@coderabbitai Do you have any suggestions how to fix the issue? And where in the code do we use utcnow?

Copy link

coderabbitai bot commented May 9, 2024

Tip

For best results, initiate chat on the files or code changes.

@emanuelen5, I've found several occurrences of datetime.datetime.utcnow() in the codebase. Here are some of the files and locations where it is used:

  • api/src/systest/api/box_terminator_test.py
  • api/src/test_aid/test_base.py
  • api/src/systest/api/change_phone_request_test.py (multiple occurrences)
  • api/src/shop/transactions.py
  • api/src/shop/pay.py (multiple occurrences)
  • api/src/service/traffic_logger.py (multiple occurrences)
  • api/src/service/entity.py
  • api/src/service/auth.py (multiple occurrences)
  • api/src/multiaccess/box_terminator.py (multiple occurrences)
  • api/src/migrate.py
  • api/src/init_db.py
  • api/src/dispatch_emails.py (multiple occurrences)
  • api/src/core/auth.py (multiple occurrences)
  • api/src/change_phone_request.py (multiple occurrences)

Regarding the deprecation of datetime.datetime.utcnow(), you can replace it with datetime.datetime.now(datetime.timezone.utc). This method provides the current UTC time with timezone awareness, which is the recommended approach in Python 3.12 and later.

Here's an example of how you can modify the code:

from datetime import datetime, timezone

# Replace this:
now = datetime.utcnow()

# With this:
now = datetime.now(timezone.utc)

You can apply this change throughout your codebase wherever datetime.datetime.utcnow() is used. If you need further assistance with this transition or have any more questions, feel free to ask!

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for all occurrences of `utcnow` in Python files.
rg "datetime.datetime.utcnow()" --type py

Length of output: 43



Script:

#!/bin/bash
# Broaden the search for any usage of `utcnow` in Python files.
rg "utcnow()" --type py

Length of output: 2516

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants