6. Timezones

6.1. Time zones in datetime library

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

  • Datetimes should be converted to localtime only when displaying to user

  • Computerphile Time & Time Zones [VideoCom19]

6.1.1. Timezone naive

Listing 194. Timezone naive datetimes
from datetime import datetime

datetime.now()
Listing 195. Timezone naive datetimes. This is potentially dangerous!
from datetime import datetime

datetime.utcnow()
Listing 196. Timezone naive datetimes
from datetime import datetime

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

6.1.2. Timezone aware

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

datetime(1957, 10, 4, 19, 28, 34, tzinfo=timezone.utc)
Listing 198. Timezone aware datetime
from datetime import datetime, timezone

dt = datetime(1957, 10, 4, 19, 28, 34)
d.replace(tzinfo=timezone.utc)
Listing 199. Timezone aware datetime
from datetime import datetime, timezone

datetime.now(tz=timezone.utc)

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


# timezone naive
my_date = datetime(1961, 4, 12, 6, 7)

timezone('Asia/Almaty').localize(my_date)
# datetime.datetime(1961, 4, 12, 14, 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


# timezone naive
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

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

  1. Dane są dwie następujące daty w formacie jak poniżej:

    gagarin = 'April 12, 1961 6:07 local time'  # Asia/Almaty
    armstrong = '"07/21/69 2:56:15 AM UTC"'
    
  2. Przedstaw daty jako obiekt datetime ze strefą czasową UTC

  3. Wyświetl obie daty w formacie ISO w strefie czasowej Europe/Warsaw

Hint
  • Wpisz "local time" jako zwykły tekst w parametrze fmt funkcji .strptime()

  • Wpisz znaki cudzysłowia " jako zwykły tekst w parametrze fmt funkcji .strptime()

  • Standard ISO:

    • '1961-04-12'

    • '1961-04-12T06:07:00Z'

    • '1961-04-12T06:07:00.000000Z'