8.2. Datetime Time

  • datetime.time() - Time

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

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.

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

8.2.1. SetUp

>>> from datetime import time

8.2.2. 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

8.2.3. 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, not 24

8.2.4. Assignments

# %% About
# - Name: Datetime Time Create
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. First human (Yuri Gagarin) flown to space on
#    April 12th, 1961 at 6:07 a.m. from Bajkonur Cosmodrome in Kazakhstan
# 2. Define `result: time` to represent time of the launch
# 3. Use `datetime.time()`
# 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. Zdefiniuj `result: time` do reprezentacji czasu startu
# 3. Użyj `datetime.time()`
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result
# 06:07:00

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'


>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is time, \
'Variable `result` has an invalid type; expected: `time`.'

>>> print(result)
06:07:00
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
from datetime import time

# %% Types
result: time

# %% Data

# %% Result
result = ...

# %% About
# - Name: Datetime Time Attributes
# - Difficulty: easy
# - Lines: 4
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Use `DATA: time`
# 2. Define variables:
#    - `result1: int` with hour from `DATA`
#    - `result2: int` with minute from `DATA`
#    - `result3: int` with second from `DATA`
#    - `result4: int` with microsecond from `DATA`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Użyj `DATA: time`
# 2. Zdefiniuj zmienne:
#    - `result1: int` z godziną z `DATA`
#    - `result2: int` z minutą z `DATA`
#    - `result3: int` z sekundą z `DATA`
#    - `result4: int` z mikrosekundą z `DATA`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Expected
# >>> result1
# 2
#
# >>> result2
# 56
#
# >>> result3
# 15
#
# >>> result4
# 0

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'


>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'

>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'

>>> assert type(result1) is int, \
'Variable `result1` has an invalid type; expected: `int`.'
>>> result1
2

>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'

>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'

>>> assert type(result2) is int, \
'Variable `result2` has an invalid type; expected: `int`.'
>>> result2
56

>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'

>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'

>>> assert type(result3) is int, \
'Variable `result3` has an invalid type; expected: `int`.'
>>> result3
15

>>> assert 'result4' in globals(), \
'Variable `result4` is not defined; assign result of your program to it.'

>>> assert result4 is not Ellipsis, \
'Variable `result4` has an invalid value; assign result of your program to it.'

>>> assert type(result4) is int, \
'Variable `result4` has an invalid type; expected: `int`.'
>>> result4
0
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
from datetime import time

# %% Types
result1: int
result2: int
result3: int
result4: int

# %% Data
DATA = time(2, 56, 15)

# %% Result
result1 = ...
result2 = ...
result3 = ...
result4 = ...