Working With Time Zones: Everything You Wish You Didn’t Need to Know
Contents
The following are the learnings from the talk on timezones:
- UTC is near monotone(leap seconds involved)
- UTC + offsets
- Non-Integer offsets - Not every place is an integer amount of hours away from UTC
- Base offset changes + Day light saving time changes
- There are irregularities in day light saving time conventions. The dates on which the clocks are moved forward by one hour and moved backwards by one hour are different across years
- Timezones are not clearly separated based on geographical boundaries
- UTC stands for Coordinated Universal time
- Leap seconds are added to UTC but are not added to Unix time
- Since years are represented as 32 bit signed integer, in 2038, the data will overflow an integer and hence has to fixed by 2038
- IANA maintains a database of all the values of time zone offsets. This database is also included in the operating system
- ISO 8601 has come with a standard for communicating the date and time
YYYY-MM-DD HH:MM:SS
- There are there calendars available in
Python
for working with date and timecalendar
,datetime
, time
- The main focus of
datetime
library is to make it less complicated to access the attributes of the time calendar
also returns objects ofdatetime
classtime
is less powerful and less flexible thandatetime
. It also uses a special structure calledstruct_time
datetime
module provides three classes to handle date and timedatetime.date
is an idealized date that assumes that gregorian calendar extends infinitely in to the past and future. The class stores year, month and day as attributesdatetime.time
is an idealized time that assumes there are 86400 seconds per day with out leap seconds. It contains hour, minute and second as attributesdatetime.combine
is a combination ofdatetime.date
anddatetime.time
classes.
datetime
does not provide a direct way to interact with IANA database. One can implement an abstract base class and create timezone objects that can be used along withdatetime
instantiation- naive vs aware instances - the former does not contain timezone instances whereas the latter contains timezone instances
- one can use
tz
class fromdateutil
function to feed in relevant timezone information in to relevant datetime instances - one can use
tz.getattr
to obtain the relevant timezones dateutil
is a parser class that is mainly used to parse strings in to dates. The structure of the string is more flexible than the directives that can be given tostrptime
function- World is divided longitudinally in increments of 15 degrees and hence there are 24 timezones
- Some of the countries have more than one timezone
- France has 12 timezones
- USA and Russia has 11 timezones
Fantastic learning from realpython
article