• Python Snacks
  • Posts
  • Python's datetime.utcnow method is being depreciated

Python's datetime.utcnow method is being depreciated

See how a simple bash script can help you fix this in 3.12+

I frequently use datetime.utcnow() from the datetime module since I work with UTC time relatively frequently (it’s actually the standard timezone in the meteorology industry).

Unfortunately, the core Python developers are depreciating this method.

Here’s the short reason why this is happening: the object returned from this method is not timezone aware (“native”). Here’s what I mean by this:

Timezone aware object vs not

In Python 3.12, you’ll see depreciation warnings if you use datetime.utcnow() - instead, you should use datetime.now(UTC)

So, how do we fix this? No matter the solution, you’re going to have to find and replace code if you use datetime.utcnow().

  1. Use a wrapper function: This is especially useful if you are maintaining multiple Python versions.

Here’s an example of what this may look like:

from datetime import datetime, UTC
import sys

# Use datetime.now() for Python 3.12 and older.
# Use the non-depreciated version for new versions.

def utcnow(func, *args, **kwargs):
    """Wrapper to help remain backwards compatible
    for Python < 3.12"""
    def get_current_utctime(*args, **kwargs):
        # Check system version
        if sys.version_info < (3, 12):
            dt = datetime.utcnow()
        else:
            dt = datetime.now(UTC)

        # Run the function/method user has defined.
        return func(dt = dt, *args, **kwargs)
    return get_current_utctime

@utcnow
def get_utc_time(*args, **kwargs):
    utcnow = kwargs.get('dt', None)
    print(f'Current UTC time: {utcnow}')
  1. Directly replace it: if you’d prefer to opt for a much simpler solution, you could take the wrap function from above, make it it’s own function, then look for all instances where you have datetime.now(UTC). While this is an easy solution, it could cause problems if your code base is relatively large and has different kinds of imports.

If you want a deeper dive into this topic, check out Miguel’s blog post about this.

Happy coding!

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

This article may contain affiliate links. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you. All purchases go to support the newsletter and blog.

Reply

or to participate.