4. Timezones

4.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]

4.1.1. Timezone naive

Listing 4.3. Timezone naive datetimes
from datetime import datetime

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

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

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

4.1.2. Timezone aware

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

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

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

datetime.now(tz=timezone.utc)

4.2. pytz

4.2.1. List of Timezones

Listing 4.9. pytz brings the Olson tz database into Python.
from pytz import timezone, utc as UTC


UTC = UTC
EST = timezone('US/Eastern')
WAW = timezone('Europe/Warsaw')
BAJKONUR = timezone('Asia/Almaty')

4.2.2. From naive to local

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


BAJKONUR = timezone('Asia/Almaty')


# timezone naive
gagarin = datetime(1961, 4, 12, 14, 7)

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

4.2.3. From naive to UTC

Listing 4.11. From naive to local time
from datetime import datetime
from pytz import utc as UTC


# timezone naive
gagarin = datetime(1961, 4, 12, 14, 7)

UTC.localize(gagarin)
# datetime.datetime(1961, 4, 12, 14, 7, tzinfo=<UTC>)

4.2.4. From UTC to local time

Listing 4.12. From UTC to local time
from datetime import datetime
from pytz import timezone, utc as UTC


WARSAW = timezone('Europe/Warsaw')


armstrong = datetime(1969, 7, 21, 14, 56, 15, tzinfo=UTC)

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

4.2.5. Between timezones

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


WARSAW = timezone('Europe/Warsaw')
BAJKONUR = timezone('Asia/Almaty')


gagarin = datetime(1961, 4, 12, 14, 7, tzinfo=BAJKONUR)

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

4.3. Assignments

4.3.1. Time zone converting

  • Filename: datetime_tz.py

  • Lines of code to write: 5 lines

  • Estimated time of completion: 10 min

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

    gagarin = 'April 12, 1961 2: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.123456Z’