search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
chevron_leftDatetime
Cookbooks5 topics
Documentation13 topics
Timezone-Aware and Naive
check_circle
Mark as learned
thumb_up
6
thumb_down
0
chat_bubble_outline
1
Comment
auto_stories Bi-column layout
settings

Python Datetime | Timezone-Aware and Naive

schedule Aug 10, 2023
Last updated
local_offer
Python
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Timezone-Aware datetime object: does have a timezone associated with it.

Timezone-Naïve datetime object: does not have any timezone associated with it.

Rules of thumb

  • Always work with "timezone-aware" datetime objects.

  • Always store datetime in UTC and leave rendering of timezones to the front-end.

NOTE

You may often hear the term “offset” when dealing with timezones. Offset of a timezone refers to the difference in hours from Coordinated Universal Time (UTC).

Examples

Checking whether a datetime object is timezone-naive

Python’s tzinfo abstract class can be used to check whether there is timezone information associated with a datetime object. It will return None if the object is timezone-naïve.

import datetime

naive = datetime.datetime.now()
print(naive.tzinfo)
None

As this example shows, datetime objects instantiated using datetime class are timezone-naïve.

Making a naive datetime object aware

The localize() method within the pytz module is used to make a naïve datetime object timezone aware.

from datetime import datetime
from pytz import timezone
# Set the time to noon on 2020-03-22
naive = datetime(2020, 3, 22, 12, 00)
print("Naive timezone = ", naive.tzinfo)
# Let's treat this time as being in the UTC timezone
aware = timezone('UTC').localize(naive)
print("Aware timezone = ", aware.tzinfo)
Naive timezone = None
Aware timezone = UTC

Localize a datetime object using timezone other than UTC

First, you have to instantiate a timezone object, and then use that timezone object to localize a datetime object.

import datetime
import pytz

naive = datetime.datetime.now()
print("Naive = ", naive)
timezone = pytz.timezone("Asia/Tokyo")
aware = timezone.localize(naive)
print("Aware = ", aware)
Naive = 2020-03-22 09:46:11.056746
Aware = 2020-03-22 09:46:11.056746+09:00

We have now attached timezone information (UTC +09:00) to the timezone-naïve datetime object to make it an timezone-aware object.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
6
thumb_down
0
chat_bubble_outline
1
settings
Enjoy our search
Hit / to insta-search docs and recipes!