2.6. Datetime Timezone

2.6.1. Rationale

  • 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 Descriptor Timezone Converter for automated solution

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

../../_images/datetime-compare1.png

Figure 2.6. Comparing datetime works only when all has the same timezone (UTC)

Listing 2.86. 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)
Listing 2.87. 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)
# Traceback (most recent call last):
#     ...
# TypeError: utcnow() takes no keyword arguments

datetime.utcnow(timezone.utc)
# Traceback (most recent call last):
#     ...
# TypeError: utcnow() takes no arguments (1 given)

2.6.3. Standard Library

  • PEP 615 - Support for the IANA Time Zone Database in the Standard Library

from zoneinfo import ZoneInfo
from datetime import datetime, timedelta


dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))  # Daylight saving time
print(dt)
# 2020-10-31 12:00:00-07:00
dt.tzname()
# 'PDT'


dt += timedelta(days=7)  # Standard time
print(dt)
# 2020-11-07 12:00:00-08:00
print(dt.tzname())
# PST

2.6.4. pytz

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


timezone('UTC')
timezone('US/Eastern')
timezone('Europe/Warsaw')
timezone('Asia/Almaty')
Listing 2.89. 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>)
Listing 2.90. 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>)
Listing 2.91. 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>)
Listing 2.92. 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>)

2.6.5. Assignments

2.6.5.1. Datetime Timezone Convert

  • Assignment name: Datetime Timezone Convert

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 13 min

  • Solution: solution/datetime_timezone_convert.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