6.1. Datetime Create

  • datetime.date() - Date

  • datetime.time() - Time

  • datetime.datetime() - Date and time

6.1.1. SetUp

>>> from datetime import date, time, datetime

6.1.2. Custom Date and Time

  • date(year, month, day) - create date

  • time(hour, minute, second, microsecond) - create time

  • datetime(year, month, day, hour, minute, second, microsecond) - create datetime

All date, time and datetime classes resides in datetime module in standard library. At first name may look weird to import class datetime from module with the same name, but this is pretty common Python practice. There is a lot of classes with the same name as the module.

Date objects represents a particular day. Example dates are Yuri Gagarin (first man to fly to space) launch date, or the first day when a person (Neil Armstrong) set foot on the Moon. In order to create a date object:

>>> x = date(1969, 7, 21)
>>> print(x)
1969-07-21

Time objects represents given time. For example our classes starts at 9:00 am. or Armstrong set foot on the Moon at 2:56:15 am.

>>> x = time(2, 56, 15)
>>> print(x)
02:56:15

The most important object is a datetime class. This represents a particular moment in time. A date and a time. For example, Armstrong set his foot on the Moon on: July 21st, 1969 at 2:56:15 am. Datetimes are typically associated with timezones (and the above event happen on that particular time of UTC timezone) but we will cover this topic later on:

>>> x = datetime(1969, 7, 21, 2, 56, 15)
>>> print(x)
1969-07-21 02:56:15

6.1.3. Date Attributes

  • date.year - year

  • date.month - month

  • date.day - day

Lets create a date object:

>>> x = date(1969, 7, 21)

You can access date attributes by . dot notation. Attributes stored in this class are: year, month, day:

>>> print(x.year)
1969
>>> print(x.month)
7
>>> print(x.day)
21

6.1.4. Time Attributes

  • time.hour - hour

  • time.minute - minute

  • time.second - second

  • time.microsecond - microsecond

Lets create a time object:

>>> t = time(2, 56, 15)

Time object, has different attributes than date object. This should be completely understandable since both objects represents different things, a date and time respectively.

With time object you can access hour, minute, second and microsecond attributes:

>>> t.hour
2
>>> t.minute
56
>>> t.second
15
>>> t.microsecond
0

6.1.5. Datetime Attributes

  • datetime.year - year

  • datetime.month - month

  • datetime.day - day

  • datetime.hour - hour

  • datetime.minute - minute

  • datetime.second - second

  • datetime.microsecond - microsecond

Lets create a time object:

>>> dt = datetime(1969, 7, 21, 2, 56, 15)

Datetime object is the most versatile and the most commonly used. It combines all the attributes from both date and time objects. You can access all of those attributes using dot . notation. The attributes are: year, month, day, hour, minute, second, microsecond.

>>> dt.year
1969
>>>
>>> dt.month
7
>>>
>>> dt.day
21
>>> dt.hour
2
>>>
>>> dt.minute
56
>>>
>>> dt.second
15
>>>
>>> dt.microsecond
0

6.1.6. Current Date and Time

  • datetime.now() - current date and time

  • date.today() - current date

  • There is no time.now()

Create date object with current date:

>>> today = date.today()

Create datetime object with current date and time:

>>> now = datetime.now()

There is no time.now()

6.1.7. Combine

  • datetime.combine(d, t) - create datetime from date and time

Create datetime from date and time objects:

>>> d = date(1969, 7, 21)
>>> t = time(2, 56, 15)
>>>
>>> datetime.combine(d, t)
datetime.datetime(1969, 7, 21, 2, 56, 15)

6.1.8. Split

  • datetime.date() - get date from datetime

  • datetime.time() - get time from datetime

>>> dt = datetime(1969, 7, 21, 2, 56, 15)
>>>
>>> dt.date()
datetime.date(1969, 7, 21)
>>>
>>> dt.time()
datetime.time(2, 56, 15)

6.1.9. Empty Time

  • def time(hour=0, minute=0, second=0, microsecond=0) - create time

  • def datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0) - create datetime

  • time() - midnight

>>> time()
datetime.time(0, 0)
>>>
>>> time(0, 0)
datetime.time(0, 0)
>>>
>>> time(0, 0, 0)
datetime.time(0, 0)
>>>
>>> time(12)
datetime.time(12, 0)
>>>
>>> time(12, 0)
datetime.time(12, 0)
>>>
>>> time(12, 0, 0)
datetime.time(12, 0)
>>>
>>> time(24, 0)
Traceback (most recent call last):
ValueError: hour must be in 0..23
>>> datetime(1969, 7, 21)
datetime.datetime(1969, 7, 21, 0, 0)

6.1.10. Use Case - 0x01

>>> from datetime import datetime, date, time
>>>
>>>
>>> d = date(1969, 7, 21)
>>> t = time(2, 56, 15)
>>>
>>> datetime(
...     year=d.year,
...     month=d.month,
...     day=d.day,
...     hour=t.hour,
...     minute=t.minute,
...     second=t.second)
datetime.datetime(1969, 7, 21, 2, 56, 15)
>>>
>>> datetime(d.year, d.month, d.day, t.hour, t. minute, t.second)
datetime.datetime(1969, 7, 21, 2, 56, 15)
>>>
>>> datetime.combine(d, t)
datetime.datetime(1969, 7, 21, 2, 56, 15)

6.1.11. Assignments

"""
* Assignment: Datetime Create Custom
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. First human (Yuri Gagarin) flown to space on
       April 12th, 1961 at 6:07 a.m. from Bajkonur Cosmodrome in Kazahstan
    2. Define `d: date` to represent date of the launch
    3. Define `t: time` to represent time of the launch
    4. Define `dt: datetime` to represent time of the launch
    5. Do not use: `datetime.combine()`, `datetime.date()`, `datetime.time()`
    6. Run doctests - all must succeed

Polish:
    1. Pierwszy człowiek (Juri Gagarin) poleciał w kosmos
       12 kwietnia 1961 roku o 6:07 rano z kosmodromu Bajkonur w Kazachstanie
    2. Zdefiniuj `d: date` do reprezentacji datę startu
    3. Zdefiniuj `t: time` do reprezentacji czas startu
    4. Zdefiniuj `dt: datetime` do reprezentacji czas startu
    5. Nie używaj: `datetime.combine()`, `datetime.date()`, `datetime.time()`
    6. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert dt is not Ellipsis, \
    'Assign result to variable: `dt`'
    >>> assert d is not Ellipsis, \
    'Assign result to variable: `d`'
    >>> assert t is not Ellipsis, \
    'Assign result to variable: `t`'
    >>> assert type(dt) is datetime, \
    'Variable `dt` has invalid type, must be a datetime'
    >>> assert type(d) is date, \
    'Variable `d` has invalid type, must be a date'
    >>> assert type(t) is time, \
    'Variable `t` has invalid type, must be a time'

    >>> print(d)
    1961-04-12

    >>> print(t)
    06:07:00

    >>> print(dt)
    1961-04-12 06:07:00
"""

from datetime import date, datetime, time


# Represent date and time of the Gagarin's launch: April 12th, 1961 6:07 a.m.
# type: datetime
dt = ...

# Represent date of the Gagarin's launch: April 12th, 1961 6:07 a.m.
# type: date
d = ...

# Represent time of the Gagarin's launch: April 12th, 1961 6:07 a.m.
# type: time
t = ...

"""
* Assignment: Datetime Create Attributes
* Complexity: easy
* Lines of code: 7 lines
* Time: 3 min

English:
    1. Use `DATA: datetime`
    2. Define variables:
        a. `result_a: int` with year from `DATA`
        b. `result_b: int` with month from `DATA`
        c. `result_c: int` with day from `DATA`
        d. `result_d: int` with hour from `DATA`
        e. `result_e: int` with minute from `DATA`
        f. `result_f: int` with second from `DATA`
        g. `result_g: int` with microsecond from `DATA`
    3. Run doctests - all must succeed

Polish:
    1. Użyj `DATA: datetime`
    2. Zdefiniuj zmienne:
        a. `result_a: int` z rokiem z `DATA`
        b. `result_b: int` z miesiącem z `DATA`
        c. `result_c: int` z dniem z `DATA`
        d. `result_d: int` z godziną z `DATA`
        e. `result_e: int` z minutą z `DATA`
        f. `result_f: int` z sekundą z `DATA`
        g. `result_g: int` z mikrosekundą z `DATA`
    3. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result_a is not Ellipsis, \
    'Assign result to variable: `result_a`'
    >>> assert type(result_a) is int, \
    'Variable `result_a` has invalid type, must be an int'
    >>> result_a
    1969

    >>> assert result_b is not Ellipsis, \
    'Assign result to variable: `result_b`'
    >>> assert type(result_b) is int, \
    'Variable `result_b` has invalid type, must be an int'
    >>> result_b
    7

    >>> assert result_c is not Ellipsis, \
    'Assign result to variable: `result_c`'
    >>> assert type(result_c) is int, \
    'Variable `result_c` has invalid type, must be an int'
    >>> result_c
    21

    >>> assert result_d is not Ellipsis, \
    'Assign result to variable: `result_d`'
    >>> assert type(result_d) is int, \
    'Variable `result_d` has invalid type, must be an int'
    >>> result_d
    2

    >>> assert result_e is not Ellipsis, \
    'Assign result to variable: `result_e`'
    >>> assert type(result_e) is int, \
    'Variable `result_e` has invalid type, must be an int'
    >>> result_e
    56

    >>> assert result_f is not Ellipsis, \
    'Assign result to variable: `result_f`'
    >>> assert type(result_f) is int, \
    'Variable `result_f` has invalid type, must be an int'
    >>> result_f
    15

    >>> assert result_g is not Ellipsis, \
    'Assign result to variable: `result_g`'
    >>> assert type(result_g) is int, \
    'Variable `result_g` has invalid type, must be an int'
    >>> result_g
    0
"""

from datetime import datetime


DATA = datetime(1969, 7, 21, 2, 56, 15)


# year from `DATA`
# type: int
result_a = ...

# month from `DATA`
# type: int
result_b = ...

# day from `DATA`
# type: int
result_c = ...

# hour from `DATA`
# type: int
result_d = ...

# minute from `DATA`
# type: int
result_e = ...

# second from `DATA`
# type: int
result_f = ...

# microsecond from `DATA`
# type: int
result_g = ...

"""
* Assignment: Datetime Create Split
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. First human (Yuri Gagarin) flown to space on
       April 12th, 1961 at 6:07 a.m. from Bajkonur Cosmodrome in Kazahstan
    2. Use `DATE: datetime`
    3. Define variables:
        a. `result_a: date` to represent date of the launch
        b. `result_b: time` to represent time of the launch
    4. Run doctests - all must succeed

Polish:
    1. Pierwszy człowiek (Juri Gagarin) poleciał w kosmos
       12 kwietnia 1961 roku o 6:07 rano z kosmodromu Bajkonur w Kazachstanie
    2. Użyj `DATE: datetime`
    3. Zdefiniuj zmienne:
        a. `result_a: date` do reprezentacji datę startu
        b. `result_b: time` do reprezentacji czas startu
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result_a is not Ellipsis, \
    'Assign result to variable: `result_a`'
    >>> assert type(result_a) is date, \
    'Variable `result_a` has invalid type, must be a date'
    >>> print(result_a)
    1961-04-12

    >>> assert result_b is not Ellipsis, \
    'Assign result to variable: `result_b`'
    >>> assert type(result_b) is time, \
    'Variable `result_b` has invalid type, must be a time'
    >>> print(result_b)
    06:07:00
"""

from datetime import date, datetime, time


DATA = datetime(1961, 4, 12, 6, 7)

# date of the launch
# type: date
result_a = ...

# time of the launch
# type: time
result_b = ...


"""
* Assignment: Datetime Create Combine
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. First human (Yuri Gagarin) flown to space on
       April 12th, 1961 at 6:07 a.m. from Bajkonur Cosmodrome in Kazahstan
    2. Combine date and time into `datetime` object representing
       exact moment of the launch
    3. Use `datetime.combine()`
    4. Run doctests - all must succeed

Polish:
    1. Pierwszy człowiek (Juri Gagarin) poleciał w kosmos
       12 kwietnia 1961 roku o 6:07 rano z kosmodromu Bajkonur w Kazachstanie
    2. Połącz datę i czas w obiekt `datetime` reprezentujący
       dokładny moment startu
    3. Użyj `datetime.combine()`
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is datetime, \
    'Variable `result` has invalid type, must be a datetime'

    >>> print(result)
    1961-04-12 06:07:00
"""

from datetime import date, datetime, time

d = date(1961, 4, 12)
t = time(6, 7)

# combine d and t to represent April 12th, 1961 6:07 a.m.
# type: datetime
result = ...

"""
* Assignment: Datetime Create Current
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. Create `datetime` object with current date and time
    2. Create `date` object with current date
    3. Create `time` object with current time
    4. Date and time must be from system, not hardcoded in code
    5. Run doctests - all must succeed

Polish:
    1. Stwórz obiekt `datetime` z obecną datą i czasem
    2. Stwórz obiekt `date` z obecną datą
    3. Stwórz obiekt `time` z obecnym czasem
    4. Data i czas ma być pobierana z systemu, nie zapisana w kodzie
    5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert d is not Ellipsis, \
    'Assign result to variable: `d`'
    >>> assert t is not Ellipsis, \
    'Assign result to variable: `t`'
    >>> assert dt is not Ellipsis, \
    'Assign result to variable: `dt`'
    >>> assert type(dt) is datetime, \
    'Variable `dt` has invalid type, must be a datetime'
    >>> assert type(d) is date, \
    'Variable `dt` has invalid type, must be a date'
    >>> assert type(t) is time, \
    'Variable `t` has invalid type, must be a time'
    >>> assert dt != datetime(1, 1, 1)
    >>> assert d != date(1, 1, 1)
    >>> assert t != time()
"""

from datetime import date, datetime, time


# variable representing current date and time
# type: datetime
dt = ...

# variable representing current date
# type: date
d = ...

# variable representing current time
# type: time
t = ...