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)

Posted by ads on Monday, 2024-08-05
Posted in [Python][Software]

One of my older Python scripts is throwing a deprecation warning in the datetime module after the recent switch from Ubuntu to Manjaro. The Python version is 3.12.4 now, previously it was 3.11.4.

Python Calendar

In this script I’m querying the Google Calendar API for upcoming events. The code which is throwing the warning:

1
2
3
4
5
6
7
8
9
now = datetime.datetime.utcnow().isoformat() + 'Z'
# select 200 events, this includes all recurring events
# those will be filtered later
events_result = service.events().list(calendarId=event_calendar['id'],
                                      timeMin=now,
                                      maxResults=200,
                                      singleEvents=True,
                                      orderBy='startTime').execute()
events = events_result.get('items', [])

This code fetches 200 events, starting with the current time in now. Not interested in past events. The time is specified as UTC time, indicated by the Z. The letter Z refers to Zulu-time, which is another name for UTC and originates in the nautical time.

The warning I get:

1
2
3
4
5
6
DEBUG: Making request: POST https://oauth2.googleapis.com/token
DEBUG: Starting new HTTPS connection (1): oauth2.googleapis.com:443
DEBUG: https://oauth2.googleapis.com:443 "POST /token HTTP/11" 200 None
./calendar-events.py:1080: 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).
  now = datetime.datetime.utcnow().isoformat() + 'Z'  # 'Z' indicates UTC time
INFO: Seen 13 events, 0 created, 0 updated

The fix for this is rather easy: replace utcnow() with now(datetime.UTC).

Test it

Quick Python script with the old and the new code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$ cat test-python-datetime.py
#!/usr/bin/env python3

import datetime

now_old = datetime.datetime.utcnow().isoformat() + 'Z'

now_new = datetime.datetime.now(datetime.UTC).isoformat()

print("Time using old code:")
print(now_old)
print("Time using new code:")
print(now_new)

Test run:

1
2
3
4
5
6
7
$ ./test-python-datetime.py
/home/ads/./test-python-datetime.py:5: 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).
  now_old = datetime.datetime.utcnow().isoformat() + 'Z'
Time using old code:
2024-08-05T13:54:52.569305Z
Time using new code:
2024-08-05T13:54:52.569315+00:00

Slightly different times, because both lines are run sequential. Otherwise looking good.

Let me go and fix my script.

Header image

Image by Денис Марчук from Pixabay, modifications by me.


Categories: [Python] [Software]

Comments

With an account on the Fediverse or Mastodon, you can respond to this post. Known replies are displayed below:



Note: This will load data from mastodon.social.

Learn how this is implemented here.


Share: