8.6. Datetime Timezones

8.6.1. Standard 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

8.6.1.1. Timezone Naive

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

Listing 8.96. 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)

8.6.1.2. Timezone Aware

Listing 8.97. 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)

8.6.2. pytz

8.6.2.1. List of Timezones

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


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

8.6.2.2. From Naive to UTC

Listing 8.99. 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>)

8.6.2.3. From Naive to Local

Listing 8.100. 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>)

8.6.2.4. From UTC to local time

Listing 8.101. 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>)

8.6.2.5. Between timezones

  • Problem with precision

Listing 8.102. 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>)

8.6.3. Assignments

8.6.3.1. Time zone converting

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 10 min

  • Solution: solution/datetime_tz.py

English
  1. Use data from "Input" section (see below)

  2. Convert given date to datetime objects

  3. What was the time in:

    • London, United Kingdom

    • Moscow, Russian Federation

    • Warsaw, Poland

    • Tokyo, Japan

    • Sydney, Australia

    • Auckland, New Zealand

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Przekonwertuj podaną datę do obiektu datetime

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

    • London, Wielka Brytania

    • Moscow, Rosja

    • Warsaw, Polska

    • Tokyo, Japan

    • Sydney, Australia

    • Auckland, Nowa Zelandia

Input
DATA = '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