4.1. Datetime Create

4.1.1. Custom Date and Time

Date:

>>> from datetime import date
>>>
>>>
>>> d = date(1969, 7, 21)
>>> print(d)
1969-07-21

Time:

>>> from datetime import time
>>>
>>>
>>> t = time(2, 56, 15)
>>> print(t)
02:56:15

Datetime:

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

4.1.2. Empty Time

>>> from datetime import time
>>>
>>>
>>> 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
>>> from datetime import datetime
>>>
>>>
>>> datetime(1969, 7, 21)
datetime.datetime(1969, 7, 21, 0, 0)

4.1.3. Attributes

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

4.1.4. Current Date and Time

Create date object with current date:

>>> from datetime import date
>>>
>>>
>>> today = date.today()

Create datetime object with current date and time:

>>> from datetime import datetime
>>>
>>>
>>> now = datetime.now()

There is no time.now()

4.1.5. Combine

Create datetime from date and time objects:

>>> 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)

4.1.6. Split

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

4.1.7. Assignments

Code 4.1. Solution
"""
* Assignment: Datetime Create Custom
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 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. Create `date` object representing date of the launch
    3. Create `time` object representing time of the launch
    4. Combine date and time into `datetime` object representing
       exact moment of the launch
    5. 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. Stwórz obiekt `date` reprezentujący datę startu
    3. Stwórz obiekt `time` reprezentujący czas startu
    4. Połącz datę i czas w obiekt `datetime` reprezentujący
       dokładny moment startu
    5. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> 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 type(dt) is datetime, \
    'Variable `dt` has invalid type, must be a datetime'

    >>> str(d)
    '1961-04-12'
    >>> str(t)
    '06:07:00'
    >>> str(dt)
    '1961-04-12 06:07:00'
"""

from datetime import date, datetime, time


# date: representing April 12th, 1961 6:07 a.m.
d = ...

# time: representing April 12th, 1961 6:07 a.m.
t = ...

# datetime: representing April 12th, 1961 6:07 a.m.
dt = ...

Code 4.2. Solution
"""
* Assignment: Datetime Create Current
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 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 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'
"""

from datetime import date, datetime, time


# datetime: representing current moment
dt = ...

# date: representing current moment
d = ...

# time: representing current moment
t = ...