CKTIS / Python
datetime.time
Represents a time of day independently of a calendar date.
Version: Python 3.14; repository image, production runtime not attested
Syntax
datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)Represents a time of day independently of a calendar date.
The hour must lie between zero and twenty-three; midnight starts the next day and cannot be expressed as hour twenty-four.
Represent a time of day without identifying any calendar day. Hours are 0–23, minutes and seconds 0–59 and microseconds 0–999999. Omitted clock fields default to zero. Neither 24:00 nor a leap second is representable. The sample formats 08:05:00 and raises ValueError for hour 24. Without a date, time calls utcoffset(None). A seasonal tzinfo may return None, leaving the time naive despite its tzinfo attribute.
from datetime import time
assert time().isoformat() == "00:00:00"
assert time(23, 59, 59, 999999) == time.max
for args in [(24,), (1, 60), (1, 2, 60)]:
try:
time(*args)
except ValueError:
pass
else:
raise AssertionError
from datetime import tzinfo
class Seasonal(tzinfo):
def utcoffset(self, dt):
assert dt is None
return None
assert time(8, tzinfo=Seasonal()).utcoffset() is None
Examples
import datetime
print(datetime.time(8, 5).isoformat())
try:
datetime.time(24, 0)
except ValueError as error:
print(type(error).__name__)
Result
08:05:00 ValueError
Represent a time of day without identifying any calendar day. Hours are 0–23, minutes and seconds 0–59 and microseconds 0–999999. Omitted clock fields default to zero. Neither 24:00 nor a leap second is representable. The sample formats 08:05:00 and raises ValueError for hour 24. Without a date, time calls utcoffset(None). A seasonal tzinfo may return None, leaving the time naive despite its tzinfo attribute.