1. Dates and Time

Warning

If you’re thinking about implementing your own time calculator or system, watch Computerophile Time & Time Zones https://www.youtube.com/watch?v=-5wpm-gesOY

1.1. Creating date objects

1.1.1. Create date

Code Listing 1.3. Create date
from datetime import date


gagarin = date(1961, 4, 12)
# datetime.date(1961, 4, 12)

gagarin.year    # 1961
gagarin.month   # 4
gagarin.day     # 12

1.1.2. Current date

Code Listing 1.4. Current date
from datetime import date


today = date.today()
# datetime.date(2019, 1, 5)

today.year    # 2019
today.month   # 1
today.day     # 5

1.2. Creating time objects

1.2.1. Create time

Code Listing 1.5. Create time
from datetime import time


now = time(12, 33, 44)
# datetime.time(12, 33, 44)

now.hour            # 12
now.minute          # 33
now.second          # 44
now.microsecond     # 0

1.2.2. Create empty time (midnight)

Code Listing 1.6. Create empty time - midnight
from datetime import time


midnight = time()
# datetime.time(0, 0)

1.3. Creating datetime objects

1.3.1. Create datetime

Code Listing 1.7. Create datetime
from datetime import datetime


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

armstrong.year          # 1969
armstrong.month         # 7
armstrong.day           # 21
armstrong.hour          # 14
armstrong.minute        # 56
armstrong.second        # 15
armstrong.microsecond   # 0

1.3.2. Create datetime with empty time (midnight)

Code Listing 1.8. Create datetime with empty time
from datetime import datetime


armstrong = datetime(1969, 7, 21)

armstrong.year          # 1969
armstrong.month         # 7
armstrong.day           # 21
armstrong.hour          # 0
armstrong.minute        # 0
armstrong.second        # 0
armstrong.microsecond   # 0

1.3.3. Create datetime from date and time objects

Code Listing 1.9. Create datetime from date and time objects
from datetime import datetime, date, time


d1 = date(1969, 7, 21)
t1 = time(14, 56, 15)

dt1 = datetime(
    year=d1.year,
    month=d1.month,
    day=d1.day,
    hour=t1.hour,
    minute=t1. minute,
    second=t1.second)
# datetime.datetime(1969, 7, 21, 14, 56, 15)


dt2 = datetime(d1.year, d1.month, d1.day, t1.hour, t1. minute, t1.second)
# datetime.datetime(1969, 7, 21, 14, 56, 15)

1.3.4. Current datetime in local time

Code Listing 1.10. Current datetime in local timezone
from datetime import datetime


now = datetime.now()
# datetime.datetime(2019, 1, 5, 20, 15, 0, 547414)

now.year          # 2019
now.month         # 1
now.day           # 5
now.hour          # 20
now.minute        # 15
now.second        # 0
now.microsecond   # 547414

1.4. datetime methods

Code Listing 1.11. datetime methods
from datetime import datetime


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

armstrong.date()        # datetime.date(1969, 7, 21)
armstrong.time()        # datetime.time(14, 56, 15)
armstrong.weekday()     # 0  # in US week starts with Sunday

1.5. Assignments

1.5.1. Create date, time and datetime objects

  • Filename: datetime_create_custom.py
  • Lines of code to write: 3 lines
  • Estimated time of completion: 5 min
  1. Stwórz obiekt date z datą Twojego urodzenia
  2. Stwórz obiekt time z czasem Twojego urodzenia
  3. Stwórz obiekt datetime z datą i czasem Twojego urodzenia

1.5.2. Create current date and datetime objects

  • Filename: datetime_create_current.py
  • Lines of code to write: 3 lines
  • Estimated time of completion: 5 min
  1. Stwórz obiekt date z obecną datą
  2. Stwórz obiekt datetime z datą i czasem
  3. Jak stworzyć obiekt time obecnym czasem?

1.5.3. Create datetime object

  • Filename: datetime_create.py
  • Lines of code to write: 1 lines
  • Estimated time of completion: 5 min
  1. Stwórz obiekt datetime z datą “1970-01-01T00:00:00.000000+0000”
  2. Co to za data?