Timezones of pytz
Actually, don’t do things manually. The pytz library is here to the rescue.
If you don’t already have it, install it with pip.
pip install pytz
A pytz Timezone Example:
>>> pacific = pytz.timezone('US/Pacific')
>>> pacific
<DstTzInfo 'US/Pacific' LMT-1 day, 16:07:00 STD>
Timezone List
All Timezones
If you don’t know all of the time zones – and nobody does – use the all_timezones list.
>>> pytz.all_timezones
['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Asmera',
'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul', 'Africa/Bissau', 'Africa/Blantyre', 'Africa/Brazzaville',
'Africa/Bujumbura', 'Africa/Cairo', 'Africa/Casablanca', 'Africa/Ceuta', 'Africa/Conakry', 'Africa/Dakar',
....... , 'Pacific/Honolulu']
By Country
Or get the timezone by country with pytz’s country_timezones timezone dictionary
>>> pytz.country_timezones['us'] ['America/New_York', 'America/Detroit', 'America/Kentucky/Louisville', ... , 'Pacific/Honolulu'] >>> pytz.country_timezones['sy'] ['Asia/Damascus'] >>> pytz.country_timezones['tr'] ['Europe/Istanbul']
Create pytz Timezones
import datetime import pytz pacific = pytz.timezone('US/Pacific') eastern = pytz.timezone('US/Eastern') fmt = '%Y-%m-d %H:%M:%S %Z %z'
%Z Timezone name (e.g. ‘US/Pacific’)
%z Timezone offset (e.g. -5:00, +8:00)
Localize Naive datetimes
Make your naive datetime objects aware with localize() using pytz timezones.
The localize() method takes a datetime and returns it with a certain timezone.
import datetime import pytz pacific = pytz.timezone('US/Pacific') eastern = pytz.timezone('US/Eastern') start = pacific.localize(datetime.datetime(2014, 4, 21, 9)) start.strftime(fmt) # OUTPUT: '2014-04-21 09:00:00 PDT-0700'
Convert Timezones
UTC Is Key
UTC stands for Coordinated Universal Time.
Prior to 1972, this time was called Greenwich Mean Time (GMT) but is now referred to as Coordinated Universal Time or Universal Time Coordinated (UTC). It is a coordinated time scale, maintained by the Bureau International des Poids et Mesures (BIPM).
So long as you start with UTC dates and times for your datetime objects, you can always generate new dates and times for anywhere else in the world without worrying about getting incorrect dates and times.
To convert timezones, first you need to create a variable that holds on to UTC timezone pytz.utc, which is a class data type.
>>> pytz.utc <UTC> >>> type(pytz.utc) <class 'pytz.UTC'>
Creating datetime with a UTC timezone
>>> utc = pytz.utc
>>> start_utc = datetime.datetime(2014, 4, 21, 1, tzinfo=utc)
>>> start_utc
datetime.datetime(2014, 4, 21, 1, 0, tzinfo=<UTC>)
How?
How do you go about converting timezones using pytz? The typical steps go as follows:
- Make your timezone datetime aware with.
- Create a pytz.utc object.
- Convert your aware datetime to utc.
- From there onwards, you can convert your date to any timezone you choose.
The following example demonstrates the conversion of the date and time of the Apollo 13 mission launch to various timezones
# Step 1 apollo_13_naive = datetime.datetime(1970,4,11,14,13) eastern = pytz.timezone('US/Eastern') apollo_13_eastern = eastern.localize(apollo_13_naive) utc = pytz.utc # Step2 apollo_13_utc = apollo_13_eastern.astimezone(utc) # Step3 # Step 4 >>> fmt = '%Y-%m-%d %H:%M:%S %Z%z' >>> pacific = pytz.timezone('US/Pacific') >>> apollo_13_utc.astimezone(pacific).strftime(fmt) '1970-04-11 11:13:00 PST-0800' >>> auckland = pytz.timezone('Pacific/Auckland') >>> apollo_13_utc.astimezone(auckland).strftime(fmt) '1970-04-12 07:13:00 NZST+1200' >>> mumbai = pytz.timezone('Asia/Calcutta') >>> apollo_13_utc.astimezone(mumbai).strftime(fmt) '1970-04-12 00:43:00 IST+0530'