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.2. 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.3. Current date
from datetime import date


today = date.today()
# datetime.date(2018, 12, 6)

today.year    # 2018
today.month   # 12
today.day     # 6

1.2. Creating time objects

1.2.1. Create time

Code Listing 1.4. Create time
from datetime import time


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

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

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

1.3. Creating datetime objects

1.3.1. Create datetime

Code Listing 1.5. 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. Current datetime in local time

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


now = datetime.now()
# datetime.datetime(2018, 12, 6, 15, 43, 46, 547414)

now.year          # 2018
now.month         # 12
now.day           # 6
now.hour          # 15
now.minute        # 43
now.second        # 46
now.microsecond   # 547414

1.4. datetime methods

Code Listing 1.7. 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)

1.5. Assignments

1.5.1. Create date, time and datetime objects

  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
About:
  • Filename: datetime_create_custom.py
  • Lines of code to write: 3 lines
  • Estimated time of completion: 5 min

1.5.2. Create current date and datetime objects

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

1.5.3. Create datetime object

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