No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Python Timezone List Tutorial

by Khaleel O.
June 7, 2021
in Python
Reading Time: 5 mins read
A A
Python Timezone List
Python Timezone List

The following tutorial will show you have to get a Python Timezone List. We will be using the powerful pytz library for our code examples.

We will be using Python 3.8.10 in this tutorial.

SECTIONS

How Do We Get A List Of All Time Zones In pytz?

How Do We Get the Current Time Zone In pytz?

How Do We Find Current Time In Another Time Zone In pytz?

How Do I Set a Time Zone In pytz?


How Do We Get A List Of All Time Zones In pytz?

First we must install the pytz library. You can install it with the command pip install pytz at the command line if you don’t already have it. Find more information HERE.

After that, we simply run the following command:

import pytz

print(pytz.all_timezones)
print(len(pytz.all_timezones))

#outputs: ['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Asmera', 'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul'....]
#593

The first print statement will give us an output of type LazyList of all the time zones available in the library, but with their long name.

The second print statement will give us a count of the items in the list: 593

How Do We Get the Current Time Zone In pytz?

import pytz 
from tzlocal import get_localzone 

print(get_localzone())
#outputs: America/La_Paz

Using the datetime library and tzlocal library (you can find information on both HERE and HERE), this can be easily done. If you don’t have tzlocal you can install it with pip install tzlocal at the command line. The above code will give us the information we are looking for.

How Do We Find Current Time In Another Time Zone In pytz?

You can easily do this with the following code:

from datetime import datetime
from pytz import timezone

tz = timezone('America/Los_Angeles')
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %z'))
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %Z'))

#Outputs
#Mon 07 Jun 2021 06:37:21.808512 AM -0700
#Mon 07 Jun 2021 06:37:21.808512 AM PDT

tz = timezone('Europe/London')
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %z'))
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %Z'))

#Outputs
#Mon 07 Jun 2021 02:37:21.824101 PM +0100
#Mon 07 Jun 2021 02:37:21.824943 PM BST

tz = timezone('Europe/Paris')
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %z'))
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %Z'))

#Outputs
#Mon 07 Jun 2021 03:37:21.825939 PM +0200
#Mon 07 Jun 2021 03:37:21.825939 PM CEST

tz = timezone('Australia/Sydney')
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %z'))
print(datetime.now(tz).strftime('%a %d %b %Y %I:%M:%S.%f %p %Z'))

#Outputs
#Mon 07 Jun 2021 11:37:21.826935 PM +1000
#Mon 07 Jun 2021 11:37:21.826935 PM AEST

Note that using %Z vs %z format code will give a different output. Consult the datetime documentation for a full list of these formatting codes. You can get a full list of time zones HERE.

How Do I Set a Time Zone In pytz?

We have to set a timezone in cases where the DateTime value provided does not have Timezone information. We call this a NAIVE datetime value.

from datetime import datetime
from pytz import timezone

tz = timezone('America/Los_Angeles')

dt= datetime(2024,7,26)
print(dt.strftime('%z'))
#prints nothing because there is no timezone information

dt = tz.localize(dt).strftime('%z')
print(dt)
#outputs: -0700

Using localize() and an explicitly defined time zone we can convert NAIVE datetimes to time zone aware date times.

Conclusion

Be sure to include the pytz library in your projects where you need to cater for world time zones. This is a powerful tool to have indeed! Good luck. ๐Ÿ‘Œ๐Ÿ‘Œ๐Ÿ‘Œ.

Previous Post

Python DateTime Starter Guide

Next Post

for i in range Python 3 Tutorial With Examples

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! ๐Ÿ”ฅ ๐Ÿ”ฅ Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue ยฉ 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue ยฉ 2022 All Rights Reserved Privacy Policy