6. Timezones

6.1. Time zones in datetime library

  • Always keep dates and times only in UTC (important!)

  • Datetimes should be converted to local time only when displaying to user

  • Computerphile Time & Time Zones [VideoCom19]

  • Refer to Timezone Conversion for automated solution

6.1.1. Timezone naive

  • datetime.utcnow() - produces timezone naive date!

Listing 198. Timezone naive datetimes.
from datetime import datetime


datetime(1957, 10, 4, 19, 28, 34)
# datetime.datetime(1957, 10, 4, 19, 28, 34)

datetime.now()
# datetime.datetime(1957, 10, 4, 19, 28, 34)

datetime.utcnow()
# datetime.datetime(1957, 10, 4, 17, 28, 34)

6.1.2. Timezone aware

Listing 199. Timezone aware datetime
from datetime import datetime, timezone


datetime.now(tz=timezone.utc)
# datetime.datetime(1957, 10, 4, 19, 28, 34, tzinfo=datetime.timezone.utc)

datetime(1957, 10, 4, 19, 28, 34, tzinfo=timezone.utc)
# datetime.datetime(1957, 10, 4, 19, 28, 34, tzinfo=datetime.timezone.utc)

dt = datetime(1957, 10, 4, 19, 28, 34)
dt.replace(tzinfo=timezone.utc)
# datetime.datetime(1957, 10, 4, 19, 28, 34, tzinfo=datetime.timezone.utc)

datetime.utcnow(tz=timezone.utc)
# TypeError: utcnow() takes no keyword arguments

datetime.utcnow(timezone.utc)
# TypeError: utcnow() takes no arguments (1 given)

6.2. pytz

6.2.1. List of Timezones

Listing 200. pytz brings the Olson tz database into Python.
from pytz import timezone


timezone('UTC')
timezone('US/Eastern')
timezone('Europe/Warsaw')
timezone('Asia/Almaty')

6.2.2. From naive to local

Listing 201. From naive to local time
from datetime import datetime
from pytz import timezone


my_date = datetime(1961, 4, 12, 6, 7)

timezone('Asia/Almaty').localize(my_date)
# datetime.datetime(1961, 4, 12, 6, 7, tzinfo=<DstTzInfo 'Asia/Almaty' +06+6:00:00 STD>)

6.2.3. From naive to UTC

Listing 202. From naive to local time
from datetime import datetime
from pytz import timezone


my_date = datetime(1969, 7, 21, 2, 56, 15)

timezone('UTC').localize(my_date)
# datetime.datetime(1969, 7, 21, 2, 56, 15, tzinfo=<UTC>)

6.2.4. From UTC to local time

Listing 203. From UTC to local time
from datetime import datetime
from pytz import timezone


my_date = datetime(1969, 7, 21, 2, 56, 15, tzinfo=timezone('UTC'))

my_date.astimezone(timezone('Europe/Warsaw'))
# datetime.datetime(1969, 7, 21, 3, 56, 15, tzinfo=<DstTzInfo 'Europe/Warsaw' CET+1:00:00 STD>)

6.2.5. Between timezones

  • Problem with precision

Listing 204. Between timezones
from datetime import datetime
from pytz import timezone


my_date = datetime(1961, 4, 12, 6, 7, tzinfo=timezone('Asia/Almaty'))

my_date.astimezone(timezone('Europe/Warsaw'))
# datetime.datetime(1961, 4, 12, 1, 59, tzinfo=<DstTzInfo 'Europe/Warsaw' CET+1:00:00 STD>)

6.3. Assignments

6.3.1. Time zone converting

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 10 min

  • Filename: solution/datetime_tz.py

English
  1. Convert given date to datetime objects

  2. What was the time in:

    1. London, United Kingdom

    2. Moscow, Russian Federation

    3. Warsaw, Poland

    4. Tokyo, Japan

    5. Sydney, Australia

    6. Auckland, New Zealand

Polish
  1. Przekonwertuj podaną datę do obiektu datetime

  2. Wyświetl datę jaka była w:

    1. London, Wielka Brytania

    2. Moscow, Rosja

    3. Warsaw, Polska

    4. Tokyo, Japan

    5. Sydney, Australia

    6. Auckland, Nowa Zelandia

Input
1969-07-21 02:56:15 UTC
Extra Task
  1. Kosmodrom Bajkonur, Kazachstan

  2. Cape Canaveral, FL, USA

  3. Houston, TX, USA

  4. New York, USA

  5. South Pole

  6. North Pole