Python provides the
pytz module for timezone information and related operations. Timezones are used to describe different time zones over the world by using city names. Generally, a country has a single time zone but some large countries like Russia, the USA, and China have multiple timezones depicted with the city names. In this tutorial, we examine how to list and use these time zones.
Import Timzone Module
The timezone information is provided by the
pytz which is the short form of the
python timezone . We should import the pytz module like below to use time zones.
List All Timezones
pytz.all_timezones returns all timezones as a list. We can print this list to list all available time zones.
import pytz print(pytz.all_timezones)
We can use the timezone information with the datetime methods. The time zone information is provided to the
datetime.now() method as parameter. The
pytz.timezone() method is populated with the timezone name.
from datetime import datetime import pytz current_date_time = datetime.now(pytz.timezone("Europe/Istanbul"))