Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Getting Time Zone–Aware Python Time and Date Objects

00:00 In this lesson, you will learn how to create datetime objects that are time zone–aware. By adding time zone awareness, your datetime objects can be considered unambiguous.

00:08 Working with unambiguous timestamps is especially important when working on projects involving real-time data, such as financial trading apps or a GPS navigation app.

00:18 The datetime module allows you to check the current time zone information associated with your datetime object. To do so, you can print your datetime object followed by the .tzinfo attribute.

00:30 As you can see, Python returns None. This is because datetime objects do not automatically instantiate with any sort of time zone information.

00:38 If you want to add time zone awareness to your object, you can use the datetime module’s .astimezone() method. In order to test this out, you can create a new variable called now_aware and set it equal to your existing datetime object, now, followed by the .astimezone() method, This code creates a new datetime object called now_aware, which has awareness of your current time zone.

01:01 You can now call now_aware to see the current time.

01:04 As you can see in the current example, my code returns UTC time followed by plus one hour (+01:00) because my time zone is one hour ahead of UTC time.

01:14 You can also use the time zone information attribute .tzinfo to check what your current time zone is.

01:21 As you can see, the code returns BST, showing that my time zone is British Standard Time. The results of this code will differ depending on where you live in the world and what time zone you’re in.

01:32 Depending on what sort of project you’re working on, you may want to normalize your timestamps to UTC time. This is especially important if you’re working on a project in a sector which generally works off UTC time as a standard, such as in aviation or meteorology. To normalize timestamps, you can work with both the datetime module as well as the timezone module.

01:52 You can import both by typing from datetime import datetime as dt, timezone as tz.

02:02 For this example, you can print your existing datetime object now, which will show you the current time in your own time zone.

02:10 To normalize your timestamp to UTC time, you can instantiate another datetime object. Let’s call it now_utc, and set it equal to dt.now() to set it equal to a new datetime object.

02:23 However, this time use the timezone module’s .utc attribute as a parameter by putting tz.utc in the parentheses of the dt.now() method.

02:33 By doing this, you can instantiate a new datetime object, which is normalized to UTC time. Next, print out your new datetime object, now_utc, and check if it is normalized to UTC time.

02:46 If you compare my original datetime object, now, to now_utc, you can see that now_utc returns the time in UTC time, while my original object, now, returns the time in British Standard Time.

02:59 The next video will conclude the series summarizing what you have learned across the previous lessons. I’ll also share some additional resources that may be helpful for you to learn more about working with time in Python.

Become a Member to join the conversation.