Date & Time
Time Introduction
Scheduling systems and time tracking applications need to work with times of day independent of dates. Python's time class represents hours, minutes, seconds, and microseconds. Learn to create times, access components, and validate business hours for real-world applications.
time from the datetime module represents a time without date (hour, minute, second, microsecond).
Creating Times
create.py
Replay: real traced execution (multi-file project)
# Create specific time
from datetime import time
# Create specific times
# Create with hour and minute
time1 = time(14, 30)
print(f"Time 1 (HH:MM): {time1}")
# Create with hour, minute, second
time2 = time(14, 30, 45)
print(f"Time 2 (HH:MM:SS): {time2}")
# Create with hour, minute, second, microsecond
time3 = time(14, 30, 45, 123456)
print(f"Time 3 (with micros): {time3}")
# Common times
morning = time(9, 0)
lunch_time = time(12, 30)
evening = time(18, 0)
print("\nCommon times:")
print(f"Morning: {morning}")
print(f"Lunch: {lunch_time}")
print(f"Evening: {evening}")
# Midnight
midnight = time(0, 0, 0)
# or
midnight2 = time.min
print(f"\nMidnight: {midnight}")
print(f"Midnight (min): {midnight2}")
# Almost midnight
almost_midnight = time(23, 59, 59, 999999)
# or
almost_midnight2 = time.max
print(f"Almost midnight: {almost_midnight}")
print(f"Almost midnight (max): {almost_midnight2}")
# Modify components
modified = time1.replace(hour=16, minute=45)
print(f"\nModified: {modified}")
# Only change hour
next_hour = time1.replace(hour=time1.hour + 1)
print(f"Next hour: {next_hour}")
# Min and max
min_time = time.min
max_time = time.max
print(f"\nMin: {min_time}")
print(f"Max: {max_time}")
# Noon
noon = time(12, 0)
print(f"Noon: {noon}")
# Generate hourly times
print("\nHourly times (9-17):")
for hour in range(9, 18):
t = time(hour, 0)
print(f" {t}")
# Quarter hours
print("\nQuarter hours:")
for minute in [0, 15, 30, 45]:
t = time(14, minute)
print(f" {t}")
# Create specific time
from datetime import time
# Create specific times
# Create with hour and minute
time1 = time(9, 0)
print(f"Time 1 (HH:MM): {time1}")
# Create with hour, minute, second
time2 = time(14, 30, 45)
print(f"Time 2 (HH:MM:SS): {time2}")
# Create with hour, minute, second, microsecond
time3 = time(14, 30, 45, 123456)
print(f"Time 3 (with micros): {time3}")
# Common times
morning = time(9, 0)
lunch_time = time(12, 30)
evening = time(18, 0)
print("\nCommon times:")
print(f"Morning: {morning}")
print(f"Lunch: {lunch_time}")
print(f"Evening: {evening}")
# Midnight
midnight = time(0, 0, 0)
# or
midnight2 = time.min
print(f"\nMidnight: {midnight}")
print(f"Midnight (min): {midnight2}")
# Almost midnight
almost_midnight = time(23, 59, 59, 999999)
# or
almost_midnight2 = time.max
print(f"Almost midnight: {almost_midnight}")
print(f"Almost midnight (max): {almost_midnight2}")
# Modify components
modified = time1.replace(hour=16, minute=45)
print(f"\nModified: {modified}")
# Only change hour
next_hour = time1.replace(hour=time1.hour + 1)
print(f"Next hour: {next_hour}")
# Min and max
min_time = time.min
max_time = time.max
print(f"\nMin: {min_time}")
print(f"Max: {max_time}")
# Noon
noon = time(12, 0)
print(f"Noon: {noon}")
# Generate hourly times
print("\nHourly times (9-17):")
for hour in range(9, 18):
t = time(hour, 0)
print(f" {t}")
# Quarter hours
print("\nQuarter hours:")
for minute in [0, 15, 30, 45]:
t = time(14, minute)
print(f" {t}")
# Create specific time
from datetime import time
# Create specific times
# Create with hour and minute
time1 = time(18, 45)
print(f"Time 1 (HH:MM): {time1}")
# Create with hour, minute, second
time2 = time(14, 30, 45)
print(f"Time 2 (HH:MM:SS): {time2}")
# Create with hour, minute, second, microsecond
time3 = time(14, 30, 45, 123456)
print(f"Time 3 (with micros): {time3}")
# Common times
morning = time(9, 0)
lunch_time = time(12, 30)
evening = time(18, 0)
print("\nCommon times:")
print(f"Morning: {morning}")
print(f"Lunch: {lunch_time}")
print(f"Evening: {evening}")
# Midnight
midnight = time(0, 0, 0)
# or
midnight2 = time.min
print(f"\nMidnight: {midnight}")
print(f"Midnight (min): {midnight2}")
# Almost midnight
almost_midnight = time(23, 59, 59, 999999)
# or
almost_midnight2 = time.max
print(f"Almost midnight: {almost_midnight}")
print(f"Almost midnight (max): {almost_midnight2}")
# Modify components
modified = time1.replace(hour=16, minute=45)
print(f"\nModified: {modified}")
# Only change hour
next_hour = time1.replace(hour=time1.hour + 1)
print(f"Next hour: {next_hour}")
# Min and max
min_time = time.min
max_time = time.max
print(f"\nMin: {min_time}")
print(f"Max: {max_time}")
# Noon
noon = time(12, 0)
print(f"Noon: {noon}")
# Generate hourly times
print("\nHourly times (9-17):")
for hour in range(9, 18):
t = time(hour, 0)
print(f" {t}")
# Quarter hours
print("\nQuarter hours:")
for minute in [0, 15, 30, 45]:
t = time(14, minute)
print(f" {t}")
time1 ← 14:30:00, time2 ← 14:30:45, time3 ← 14:30:45.123456, morning ← 09:00:00
6# Create with hour and minute7time1→ 14:30:00 = time(14, 30) #@time1=time(9, 0), time(18, 45)8print(f"Time 1 (HH:MM): {time114:30:00}")910# Create with hour, minute, second11time2→ 14:30:45 = time(14, 30, 45)12print(f"Time 2 (HH:MM:SS): {time214:30:45}")1314# Create with hour, minute, second, microsecond15time3→ 14:30:45.123456 = time(14, 30, 45, 123456)16print(f"Time 3 (with micros): {time314:30:45.123456}")1718# Common times19morning→ 09:00:00 = time(9, 0)20lunch_time→ 12:30:00 = time(12, 30)21evening→ 18:00:00 = time(18, 0)2223print("\nCommon times:")24print(f"Morning: {morning09:00:00}")25print(f"Lunch: {lunch_time12:30:00}")26print(f"Evening: {evening18:00:00}")2728# Midnight29midnight→ 00:00:00 = time(0, 0, 0)30# or31midnight2→ 00:00:00 = time.min00:00:0032print(f"\nMidnight: {midnight00:00:00}")33print(f"Midnight (min): {midnight200:00:00}")3435# Almost midnight36almost_midnight→ 23:59:59.999999 = time(23, 59, 59, 999999)37# or38almost_midnight2→ 23:59:59.999999 = time.max23:59:59.99999939print(f"Almost midnight: {almost_midnight23:59:59.999999}")40print(f"Almost midnight (max): {almost_midnight223:59:59.999999}")4142# Modify components43modified→ 16:45:00 = time114:30:00.replace(hour=16, minute=45)44print(f"\nModified: {modified16:45:00}")4546# Only change hour47next_hour→ 15:30:00 = time114:30:00.replace(hour=time1.hour14 + 1)48print(f"Next hour: {next_hour15:30:00}")4950# Min and max51min_time→ 00:00:00 = time.min00:00:0052max_time→ 23:59:59.999999 = time.max23:59:59.99999953print(f"\nMin: {min_time00:00:00}")54print(f"Max: {max_time23:59:59.999999}")5556# Noon57noon→ 12:00:00 = time(12, 0)58print(f"Noon: {noon12:00:00}")5960# Generate hourly times61print("\nHourly times (9-17):")62for hour in range(9, 18):outputTime 1 (HH:MM): 14:30:00 Time 2 (HH:MM:SS): 14:30:45 Time 3 (with micros): 14:30:45.123456 Common times: Morning: 09:00:00 Lunch: 12:30:00 Evening: 18:00:00 Midnight: 00:00:00 Midnight (min): 00:00:00 Almost midnight: 23:59:59.999999 Almost midnight (max): 23:59:59.999999 Modified: 16:45:00 Next hour: 15:30:00 Min: 00:00:00 Max: 23:59:59.999999 Noon: 12:00:00 Hourly times (9-17):t ← 09:00:00
pass 1 of 961print("\nHourly times (9-17):")62for hour9 in range(9, 18):63 t→ 09:00:00 = time(hour9, 0)64 print(f" {t09:00:00}")output 09:00:00All 9 passes — pass 1 is the card above pass hourt1 9 09:00:00 2 10 10:00:00 3 11 11:00:00 4 12 12:00:00 5 13 13:00:00 6 14 14:00:00 7 15 15:00:00 8 16 16:00:00 9 17 17:00:00 print(" Quarter hours:")
66# Quarter hours67print("\nQuarter hours:")68for minute in [0, 15, 30, 45]:output Quarter hours:t ← 14:00:00
pass 1 of 467print("\nQuarter hours:")68for minute0 in [0, 15, 30, 45]:69 t→ 14:00:00 = time(14, minute0)70 print(f" {t14:00:00}")output 14:00:00All 4 passes — pass 1 is the card above pass minutet1 0 14:00:00 2 15 14:15:00 3 30 14:30:00 4 45 14:45:00
time1 ← 09:00:00, time2 ← 14:30:45, time3 ← 14:30:45.123456, morning ← 09:00:00
6# Create with hour and minute7time1→ 09:00:00 = time(9, 0)8print(f"Time 1 (HH:MM): {time109:00:00}")910# Create with hour, minute, second11time2→ 14:30:45 = time(14, 30, 45)12print(f"Time 2 (HH:MM:SS): {time214:30:45}")1314# Create with hour, minute, second, microsecond15time3→ 14:30:45.123456 = time(14, 30, 45, 123456)16print(f"Time 3 (with micros): {time314:30:45.123456}")1718# Common times19morning→ 09:00:00 = time(9, 0)20lunch_time→ 12:30:00 = time(12, 30)21evening→ 18:00:00 = time(18, 0)2223print("\nCommon times:")24print(f"Morning: {morning09:00:00}")25print(f"Lunch: {lunch_time12:30:00}")26print(f"Evening: {evening18:00:00}")2728# Midnight29midnight→ 00:00:00 = time(0, 0, 0)30# or31midnight2→ 00:00:00 = time.min00:00:0032print(f"\nMidnight: {midnight00:00:00}")33print(f"Midnight (min): {midnight200:00:00}")3435# Almost midnight36almost_midnight→ 23:59:59.999999 = time(23, 59, 59, 999999)37# or38almost_midnight2→ 23:59:59.999999 = time.max23:59:59.99999939print(f"Almost midnight: {almost_midnight23:59:59.999999}")40print(f"Almost midnight (max): {almost_midnight223:59:59.999999}")4142# Modify components43modified→ 16:45:00 = time109:00:00.replace(hour=16, minute=45)44print(f"\nModified: {modified16:45:00}")4546# Only change hour47next_hour→ 10:00:00 = time109:00:00.replace(hour=time1.hour9 + 1)48print(f"Next hour: {next_hour10:00:00}")4950# Min and max51min_time→ 00:00:00 = time.min00:00:0052max_time→ 23:59:59.999999 = time.max23:59:59.99999953print(f"\nMin: {min_time00:00:00}")54print(f"Max: {max_time23:59:59.999999}")5556# Noon57noon→ 12:00:00 = time(12, 0)58print(f"Noon: {noon12:00:00}")5960# Generate hourly times61print("\nHourly times (9-17):")62for hour in range(9, 18):outputTime 1 (HH:MM): 09:00:00 Time 2 (HH:MM:SS): 14:30:45 Time 3 (with micros): 14:30:45.123456 Common times: Morning: 09:00:00 Lunch: 12:30:00 Evening: 18:00:00 Midnight: 00:00:00 Midnight (min): 00:00:00 Almost midnight: 23:59:59.999999 Almost midnight (max): 23:59:59.999999 Modified: 16:45:00 Next hour: 10:00:00 Min: 00:00:00 Max: 23:59:59.999999 Noon: 12:00:00 Hourly times (9-17):t ← 09:00:00
pass 1 of 961print("\nHourly times (9-17):")62for hour9 in range(9, 18):63 t→ 09:00:00 = time(hour9, 0)64 print(f" {t09:00:00}")output 09:00:00All 9 passes — pass 1 is the card above pass hourt1 9 09:00:00 2 10 10:00:00 3 11 11:00:00 4 12 12:00:00 5 13 13:00:00 6 14 14:00:00 7 15 15:00:00 8 16 16:00:00 9 17 17:00:00 print(" Quarter hours:")
66# Quarter hours67print("\nQuarter hours:")68for minute in [0, 15, 30, 45]:output Quarter hours:t ← 14:00:00
pass 1 of 467print("\nQuarter hours:")68for minute0 in [0, 15, 30, 45]:69 t→ 14:00:00 = time(14, minute0)70 print(f" {t14:00:00}")output 14:00:00All 4 passes — pass 1 is the card above pass minutet1 0 14:00:00 2 15 14:15:00 3 30 14:30:00 4 45 14:45:00
time1 ← 18:45:00, time2 ← 14:30:45, time3 ← 14:30:45.123456, morning ← 09:00:00
6# Create with hour and minute7time1→ 18:45:00 = time(18, 45)8print(f"Time 1 (HH:MM): {time118:45:00}")910# Create with hour, minute, second11time2→ 14:30:45 = time(14, 30, 45)12print(f"Time 2 (HH:MM:SS): {time214:30:45}")1314# Create with hour, minute, second, microsecond15time3→ 14:30:45.123456 = time(14, 30, 45, 123456)16print(f"Time 3 (with micros): {time314:30:45.123456}")1718# Common times19morning→ 09:00:00 = time(9, 0)20lunch_time→ 12:30:00 = time(12, 30)21evening→ 18:00:00 = time(18, 0)2223print("\nCommon times:")24print(f"Morning: {morning09:00:00}")25print(f"Lunch: {lunch_time12:30:00}")26print(f"Evening: {evening18:00:00}")2728# Midnight29midnight→ 00:00:00 = time(0, 0, 0)30# or31midnight2→ 00:00:00 = time.min00:00:0032print(f"\nMidnight: {midnight00:00:00}")33print(f"Midnight (min): {midnight200:00:00}")3435# Almost midnight36almost_midnight→ 23:59:59.999999 = time(23, 59, 59, 999999)37# or38almost_midnight2→ 23:59:59.999999 = time.max23:59:59.99999939print(f"Almost midnight: {almost_midnight23:59:59.999999}")40print(f"Almost midnight (max): {almost_midnight223:59:59.999999}")4142# Modify components43modified→ 16:45:00 = time118:45:00.replace(hour=16, minute=45)44print(f"\nModified: {modified16:45:00}")4546# Only change hour47next_hour→ 19:45:00 = time118:45:00.replace(hour=time1.hour18 + 1)48print(f"Next hour: {next_hour19:45:00}")4950# Min and max51min_time→ 00:00:00 = time.min00:00:0052max_time→ 23:59:59.999999 = time.max23:59:59.99999953print(f"\nMin: {min_time00:00:00}")54print(f"Max: {max_time23:59:59.999999}")5556# Noon57noon→ 12:00:00 = time(12, 0)58print(f"Noon: {noon12:00:00}")5960# Generate hourly times61print("\nHourly times (9-17):")62for hour in range(9, 18):outputTime 1 (HH:MM): 18:45:00 Time 2 (HH:MM:SS): 14:30:45 Time 3 (with micros): 14:30:45.123456 Common times: Morning: 09:00:00 Lunch: 12:30:00 Evening: 18:00:00 Midnight: 00:00:00 Midnight (min): 00:00:00 Almost midnight: 23:59:59.999999 Almost midnight (max): 23:59:59.999999 Modified: 16:45:00 Next hour: 19:45:00 Min: 00:00:00 Max: 23:59:59.999999 Noon: 12:00:00 Hourly times (9-17):t ← 09:00:00
pass 1 of 961print("\nHourly times (9-17):")62for hour9 in range(9, 18):63 t→ 09:00:00 = time(hour9, 0)64 print(f" {t09:00:00}")output 09:00:00All 9 passes — pass 1 is the card above pass hourt1 9 09:00:00 2 10 10:00:00 3 11 11:00:00 4 12 12:00:00 5 13 13:00:00 6 14 14:00:00 7 15 15:00:00 8 16 16:00:00 9 17 17:00:00 print(" Quarter hours:")
66# Quarter hours67print("\nQuarter hours:")68for minute in [0, 15, 30, 45]:output Quarter hours:t ← 14:00:00
pass 1 of 467print("\nQuarter hours:")68for minute0 in [0, 15, 30, 45]:69 t→ 14:00:00 = time(14, minute0)70 print(f" {t14:00:00}")output 14:00:00All 4 passes — pass 1 is the card above pass minutet1 0 14:00:00 2 15 14:15:00 3 30 14:30:00 4 45 14:45:00
time_creation
Creating time objects with specific hour, minute, second, and microsecond values
Getting Current Time
current.py
Replay: real traced execution (multi-file project)
# Current time
from datetime import datetime, time
# Get current time
# Current time (from datetime)
now = datetime.now().time().replace(hour=14, minute=30, second=45, microsecond=123456)
print(f"Now: {now}")
# Time components
hour = now.hour
minute = now.minute
second = now.second
microsecond = now.microsecond
print("\nTime components:")
print(f"Hour: {hour}")
print(f"Minute: {minute}")
print(f"Second: {second}")
print(f"Microsecond: {microsecond}")
# Formatted output
print(f"\nFormatted: {hour:02d}:{minute:02d}:{second:02d}")
# 12-hour format
hour12 = hour % 12
if hour12 == 0:
hour12 = 12
am_pm = "AM" if hour < 12 else "PM"
print(f"12-hour: {hour12:02d}:{minute:02d} {am_pm}")
# Truncated times
to_minute = now.replace(second=0, microsecond=0)
print(f"\nTruncated to minute: {to_minute}")
to_second = now.replace(microsecond=0)
print(f"Truncated to second: {to_second}")
# Special times
midnight = time.min # 00:00:00
almost_midnight = time.max # 23:59:59.999999
print("\nSpecial times:")
print(f"Midnight (min): {midnight}")
print(f"Almost midnight (max): {almost_midnight}")
# ISO format
iso = now.isoformat()
print(f"\nISO format: {iso}")
# With milliseconds
millis = now.microsecond // 1000
print(f"Milliseconds: {millis}")
# String representation
print(f"\nString repr: {repr(now)}")
print(f"String str: {str(now)}")
now ← 14:30:45.123456, hour ← 14, minute ← 30, second ← 45, microsecond ← 123456
6# Current time (from datetime)7now→ 14:30:45.123456 = datetime<class 'datetime.datetime'>.now().time().replace(hour=14, minute=30, second=45, microsecond=123456)8print(f"Now: {now14:30:45.123456}")910# Time components11hour→ 14 = now.hour1412minute→ 30 = now.minute3013second→ 45 = now.second4514microsecond→ 123456 = now.microsecond1234561516print("\nTime components:")17print(f"Hour: {hour14}")18print(f"Minute: {minute30}")19print(f"Second: {second45}")20print(f"Microsecond: {microsecond123456}")2122# Formatted output23print(f"\nFormatted: {hour14:02d}:{minute30:02d}:{second45:02d}")2425# 12-hour format26hour12→ 2 = hour14 % 1227if hour12 == 0:28 hour12 = 1229am_pm→ PM = "AM" if hour14 < 12 else "PM"30print(f"12-hour: {hour122:02d}:{minute30:02d} {am_pmPM}")3132# Truncated times33to_minute→ 14:30:00 = now14:30:45.123456.replace(second=0, microsecond=0)34print(f"\nTruncated to minute: {to_minute14:30:00}")3536to_second→ 14:30:45 = now14:30:45.123456.replace(microsecond=0)37print(f"Truncated to second: {to_second14:30:45}")3839# Special times40midnight→ 00:00:00 = time.min00:00:00 # 00:00:0041almost_midnight→ 23:59:59.999999 = time.max23:59:59.999999 # 23:59:59.9999994243print("\nSpecial times:")44print(f"Midnight (min): {midnight00:00:00}")45print(f"Almost midnight (max): {almost_midnight23:59:59.999999}")4647# ISO format48iso→ 14:30:45.123456 = now14:30:45.123456.isoformat()49print(f"\nISO format: {iso14:30:45.123456}")5051# With milliseconds52millis→ 123 = now.microsecond123456 // 100053print(f"Milliseconds: {millis123}")5455# String representation56print(f"\nString repr: {repr(now14:30:45.123456)}")57print(f"String str: {str(now14:30:45.123456)}")outputNow: 14:30:45.123456 Time components: Hour: 14 Minute: 30 Second: 45 Microsecond: 123456 Formatted: 14:30:45 12-hour: 02:30 PM Truncated to minute: 14:30:00 Truncated to second: 14:30:45 Special times: Midnight (min): 00:00:00 Almost midnight (max): 23:59:59.999999 ISO format: 14:30:45.123456 Milliseconds: 123 String repr: datetime.time(14, 30, 45, 123456) String str: 14:30:45.123456
current_time
Getting current time from datetime.now()
Time Components
components.py
Replay: real traced execution (multi-file project)
# Time components
from datetime import time
# Extract time components
t = time(14, 30, 45, 123456)
print(f"Time: {t}")
print()
# Hour, minute, second
print(f"Hour: {t.hour}")
print(f"Minute: {t.minute}")
print(f"Second: {t.second}")
print(f"Microsecond: {t.microsecond}")
# Tzinfo (timezone info, if any)
print(f"Timezone: {t.tzinfo}")
# Calculate milliseconds
millis = t.microsecond // 1000
print(f"\nMilliseconds: {millis}")
# 12-hour format components
hour24 = t.hour
hour12 = hour24 % 12
if hour12 == 0:
hour12 = 12
am_pm = "AM" if hour24 < 12 else "PM"
print(f"\n12-hour format:")
print(f"Hour (12): {hour12}")
print(f"AM/PM: {am_pm}")
print(f"Time: {hour12}:{t.minute:02d} {am_pm}")
# Truncate precision
to_minute = t.replace(second=0, microsecond=0)
to_second = t.replace(microsecond=0)
print(f"\nTruncated:")
print(f"Original: {t}")
print(f"To minute: {to_minute}")
print(f"To second: {to_second}")
# ISO format
iso = t.isoformat()
print(f"\nISO format: {iso}")
# String formatting
print(f"\nFormatted:")
print(f" HH:MM:SS - {t.hour:02d}:{t.minute:02d}:{t.second:02d}")
print(f" HH:MM - {t.hour:02d}:{t.minute:02d}")
# Time period
print(f"\nTime period:")
if t.hour < 12:
print("Morning")
elif t.hour < 17:
print("Afternoon")
elif t.hour < 21:
print("Evening")
else:
print("Night")
# Display various times
print("\nVarious times of day:")
times = [
time(6, 0),
time(12, 0),
time(18, 0),
time(23, 59)
]
for time_obj in times:
print(f"{time_obj} - {time_obj.hour}h {time_obj.minute}m")
# Resolution
resolution = time.resolution
print(f"\nTime resolution: {resolution}")
t ← 14:30:45.123456, millis ← 123, hour24 ← 14, hour12 ← 2, am_pm ← PM
5# Extract time components6t→ 14:30:45.123456 = time(14, 30, 45, 123456)78print(f"Time: {t14:30:45.123456}")9print()1011# Hour, minute, second12print(f"Hour: {t.hour14}")13print(f"Minute: {t.minute30}")14print(f"Second: {t.second45}")15print(f"Microsecond: {t.microsecond123456}")1617# Tzinfo (timezone info, if any)18print(f"Timezone: {t.tzinfoNone}")1920# Calculate milliseconds21millis→ 123 = t.microsecond123456 // 100022print(f"\nMilliseconds: {millis123}")2324# 12-hour format components25hour24→ 14 = t.hour1426hour12→ 2 = hour2414 % 1227if hour12 == 0:28 hour12 = 1229am_pm→ PM = "AM" if hour2414 < 12 else "PM"3031print(f"\n12-hour format:")32print(f"Hour (12): {hour122}")33print(f"AM/PM: {am_pmPM}")34print(f"Time: {hour122}:{t.minute30:02d} {am_pmPM}")3536# Truncate precision37to_minute→ 14:30:00 = t14:30:45.123456.replace(second=0, microsecond=0)38to_second→ 14:30:45 = t14:30:45.123456.replace(microsecond=0)3940print(f"\nTruncated:")41print(f"Original: {t14:30:45.123456}")42print(f"To minute: {to_minute14:30:00}")43print(f"To second: {to_second14:30:45}")4445# ISO format46iso→ 14:30:45.123456 = t14:30:45.123456.isoformat()47print(f"\nISO format: {iso14:30:45.123456}")4849# String formatting50print(f"\nFormatted:")51print(f" HH:MM:SS - {t.hour14:02d}:{t.minute30:02d}:{t.second45:02d}")52print(f" HH:MM - {t.hour14:02d}:{t.minute30:02d}")5354# Time period55print(f"\nTime period:")56if t.hour < 12:outputTime: 14:30:45.123456 Hour: 14 Minute: 30 Second: 45 Microsecond: 123456 Timezone: None Milliseconds: 123 12-hour format: Hour (12): 2 AM/PM: PM Time: 2:30 PM Truncated: Original: 14:30:45.123456 To minute: 14:30:00 To second: 14:30:45 ISO format: 14:30:45.123456 Formatted: HH:MM:SS - 14:30:45 HH:MM - 14:30 Time period:elif t.hour < 17:
57 print("Morning")58elif t.hour14 < 17:59 print("Afternoon")60elif t.hour < 21:outputAfternoontimes ← [datetime.time(6, 0), datetime.time(12, 0), datetime.time(18, 0), datetime.time(23, 59)]
65# Display various times66print("\nVarious times of day:")67times→ [datetime.time(6, 0), datetime.time(12, 0), datetime.time(18, 0), datetime.time(23, 59)] = [68 time(6, 0),69 time(12, 0),70 time(18, 0),71 time(23, 59)72]output Various times of day:for time_obj in times:
pass 1 of 474for time_obj06:00:00 in times[datetime.time(6, 0), datetime.time(12, 0), datetime.time(18, 0), datetime.time(23, 59)]:75 print(f"{time_obj06:00:00} - {time_obj.hour6}h {time_obj.minute0}m")output06:00:00 - 6h 0mAll 4 passes — pass 1 is the card above pass time_objtime_obj.hourtime_obj.minute1 06:00:00 6 0 2 12:00:00 12 0 3 18:00:00 18 0 4 23:59:00 23 59 resolution ← 0:00:00.000001
77# Resolution78resolution→ 0:00:00.000001 = time.resolution0:00:00.00000179print(f"\nTime resolution: {resolution0:00:00.000001}")output Time resolution: 0:00:00.000001
time_components
Accessing hour, minute, second, microsecond, and converting to 12-hour format
Comparing Times
compare.py
Replay: real traced execution (multi-file project)
# Compare times
from datetime import time
# Compare times
time1 = time(9, 30)
time2 = time(14, 45)
time3 = time(9, 30)
print(f"Time 1: {time1}")
print(f"Time 2: {time2}")
print(f"Time 3: {time3}")
print()
# Comparison operators
print(f"time1 < time2: {time1 < time2}")
print(f"time1 > time2: {time1 > time2}")
print(f"time1 <= time2: {time1 <= time2}")
print(f"time1 >= time2: {time1 >= time2}")
# Equality
print(f"\ntime1 == time2: {time1 == time2}")
print(f"time1 == time3: {time1 == time3}")
print(f"time1 != time2: {time1 != time2}")
# Find min/max
times_list = [time1, time2, time3]
earliest = min(times_list)
latest = max(times_list)
print(f"\nEarliest: {earliest}")
print(f"Latest: {latest}")
# Check if in time range
check = time(12, 0)
start = time(9, 0)
end = time(17, 0)
in_range = start <= check <= end
print(f"\n{check} in range [{start}, {end}]: {in_range}")
# Sort times
unsorted = [
time(15, 30),
time(9, 0),
time(12, 15)
]
print("\nBefore sort:")
for t in unsorted:
print(f" {t}")
sorted_times = sorted(unsorted)
print("After sort:")
for t in sorted_times:
print(f" {t}")
# Business hours check
business_start = time(9, 0)
business_end = time(17, 0)
print("\nBusiness hours check:")
test_times = [
time(8, 30),
time(12, 0),
time(18, 0)
]
for t in test_times:
is_during_business = business_start <= t < business_end
print(f"{t} during business: {is_during_business}")
# Midnight crossing
late = time(23, 0)
early = time(1, 0)
print(f"\n23:00 < 01:00: {late < early}") # True (earlier in day)
# Special time comparisons
print("\nSpecial comparisons:")
print(f"Midnight: {time.min}")
print(f"Almost midnight: {time.max}")
print(f"Noon: {time(12, 0)}")
noon = time(12, 0)
print(f"10:00 < noon: {time(10, 0) < noon}")
print(f"14:00 > noon: {time(14, 0) > noon}")
# Filter times
all_times = [
time(8, 0),
time(10, 0),
time(14, 0),
time(18, 0)
]
morning_times = [t for t in all_times if t < time(12, 0)]
afternoon_times = [t for t in all_times if t >= time(12, 0)]
print(f"\nMorning times: {morning_times}")
print(f"Afternoon times: {afternoon_times}")
# Compare times
from datetime import time
# Compare times
time1 = time(9, 30)
time2 = time(14, 45)
time3 = time(9, 30)
print(f"Time 1: {time1}")
print(f"Time 2: {time2}")
print(f"Time 3: {time3}")
print()
# Comparison operators
print(f"time1 < time2: {time1 < time2}")
print(f"time1 > time2: {time1 > time2}")
print(f"time1 <= time2: {time1 <= time2}")
print(f"time1 >= time2: {time1 >= time2}")
# Equality
print(f"\ntime1 == time2: {time1 == time2}")
print(f"time1 == time3: {time1 == time3}")
print(f"time1 != time2: {time1 != time2}")
# Find min/max
times_list = [time1, time2, time3]
earliest = min(times_list)
latest = max(times_list)
print(f"\nEarliest: {earliest}")
print(f"Latest: {latest}")
# Check if in time range
check = time(8, 30)
start = time(9, 0)
end = time(17, 0)
in_range = start <= check <= end
print(f"\n{check} in range [{start}, {end}]: {in_range}")
# Sort times
unsorted = [
time(15, 30),
time(9, 0),
time(12, 15)
]
print("\nBefore sort:")
for t in unsorted:
print(f" {t}")
sorted_times = sorted(unsorted)
print("After sort:")
for t in sorted_times:
print(f" {t}")
# Business hours check
business_start = time(9, 0)
business_end = time(17, 0)
print("\nBusiness hours check:")
test_times = [
time(8, 30),
time(12, 0),
time(18, 0)
]
for t in test_times:
is_during_business = business_start <= t < business_end
print(f"{t} during business: {is_during_business}")
# Midnight crossing
late = time(23, 0)
early = time(1, 0)
print(f"\n23:00 < 01:00: {late < early}") # True (earlier in day)
# Special time comparisons
print("\nSpecial comparisons:")
print(f"Midnight: {time.min}")
print(f"Almost midnight: {time.max}")
print(f"Noon: {time(12, 0)}")
noon = time(12, 0)
print(f"10:00 < noon: {time(10, 0) < noon}")
print(f"14:00 > noon: {time(14, 0) > noon}")
# Filter times
all_times = [
time(8, 0),
time(10, 0),
time(14, 0),
time(18, 0)
]
morning_times = [t for t in all_times if t < time(12, 0)]
afternoon_times = [t for t in all_times if t >= time(12, 0)]
print(f"\nMorning times: {morning_times}")
print(f"Afternoon times: {afternoon_times}")
# Compare times
from datetime import time
# Compare times
time1 = time(9, 30)
time2 = time(14, 45)
time3 = time(9, 30)
print(f"Time 1: {time1}")
print(f"Time 2: {time2}")
print(f"Time 3: {time3}")
print()
# Comparison operators
print(f"time1 < time2: {time1 < time2}")
print(f"time1 > time2: {time1 > time2}")
print(f"time1 <= time2: {time1 <= time2}")
print(f"time1 >= time2: {time1 >= time2}")
# Equality
print(f"\ntime1 == time2: {time1 == time2}")
print(f"time1 == time3: {time1 == time3}")
print(f"time1 != time2: {time1 != time2}")
# Find min/max
times_list = [time1, time2, time3]
earliest = min(times_list)
latest = max(times_list)
print(f"\nEarliest: {earliest}")
print(f"Latest: {latest}")
# Check if in time range
check = time(16, 15)
start = time(9, 0)
end = time(17, 0)
in_range = start <= check <= end
print(f"\n{check} in range [{start}, {end}]: {in_range}")
# Sort times
unsorted = [
time(15, 30),
time(9, 0),
time(12, 15)
]
print("\nBefore sort:")
for t in unsorted:
print(f" {t}")
sorted_times = sorted(unsorted)
print("After sort:")
for t in sorted_times:
print(f" {t}")
# Business hours check
business_start = time(9, 0)
business_end = time(17, 0)
print("\nBusiness hours check:")
test_times = [
time(8, 30),
time(12, 0),
time(18, 0)
]
for t in test_times:
is_during_business = business_start <= t < business_end
print(f"{t} during business: {is_during_business}")
# Midnight crossing
late = time(23, 0)
early = time(1, 0)
print(f"\n23:00 < 01:00: {late < early}") # True (earlier in day)
# Special time comparisons
print("\nSpecial comparisons:")
print(f"Midnight: {time.min}")
print(f"Almost midnight: {time.max}")
print(f"Noon: {time(12, 0)}")
noon = time(12, 0)
print(f"10:00 < noon: {time(10, 0) < noon}")
print(f"14:00 > noon: {time(14, 0) > noon}")
# Filter times
all_times = [
time(8, 0),
time(10, 0),
time(14, 0),
time(18, 0)
]
morning_times = [t for t in all_times if t < time(12, 0)]
afternoon_times = [t for t in all_times if t >= time(12, 0)]
print(f"\nMorning times: {morning_times}")
print(f"Afternoon times: {afternoon_times}")
time1 ← 09:30:00, time2 ← 14:45:00, time3 ← 09:30:00, times_list ← [datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)]
5# Compare times6time1→ 09:30:00 = time(9, 30)7time2→ 14:45:00 = time(14, 45)8time3→ 09:30:00 = time(9, 30)910print(f"Time 1: {time109:30:00}")11print(f"Time 2: {time214:45:00}")12print(f"Time 3: {time309:30:00}")13print()1415# Comparison operators16print(f"time1 < time2: {time109:30:00 < time214:45:00}")17print(f"time1 > time2: {time109:30:00 > time214:45:00}")18print(f"time1 <= time2: {time109:30:00 <= time214:45:00}")19print(f"time1 >= time2: {time109:30:00 >= time214:45:00}")2021# Equality22print(f"\ntime1 == time2: {time109:30:00 == time214:45:00}")23print(f"time1 == time3: {time109:30:00 == time309:30:00}")24print(f"time1 != time2: {time109:30:00 != time214:45:00}")2526# Find min/max27times_list→ [datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)] = [time109:30:00, time214:45:00, time309:30:00]28earliest→ 09:30:00 = min(times_list[datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)])29latest→ 14:45:00 = max(times_list[datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)])3031print(f"\nEarliest: {earliest09:30:00}")32print(f"Latest: {latest14:45:00}")3334# Check if in time range35check→ 12:00:00 = time(12, 0) #@check=time(8, 30), time(16, 15)36start→ 09:00:00 = time(9, 0)37end→ 17:00:00 = time(17, 0)3839in_range→ True = start09:00:00 <= check12:00:00 <= end17:00:0040print(f"\n{check12:00:00} in range [{start09:00:00}, {end17:00:00}]: {in_rangeTrue}")4142# Sort times43unsorted→ [datetime.time(15, 30), datetime.time(9, 0), datetime.time(12, 15)] = [44 time(15, 30),45 time(9, 0),46 time(12, 15)47]4849print("\nBefore sort:")50for t in unsorted:outputTime 1: 09:30:00 Time 2: 14:45:00 Time 3: 09:30:00 time1 < time2: True time1 > time2: False time1 <= time2: True time1 >= time2: False time1 == time2: False time1 == time3: True time1 != time2: True Earliest: 09:30:00 Latest: 14:45:00 12:00:00 in range [09:00:00, 17:00:00]: True Before sort:for t in unsorted:
pass 1 of 349print("\nBefore sort:")50for t15:30:00 in unsorted[datetime.time(15, 30), datetime.time(9, 0), datetime.time(12, 15)]:51 print(f" {t15:30:00}")output 15:30:00All 3 passes — pass 1 is the card above pass t1 15:30:00 2 09:00:00 3 12:15:00 sorted_times ← [datetime.time(9, 0), datetime.time(12, 15), datetime.time(15, 30)]
53sorted_times→ [datetime.time(9, 0), datetime.time(12, 15), datetime.time(15, 30)] = sorted(unsorted[datetime.time(15, 30), datetime.time(9, 0), datetime.time(12, 15)])5455print("After sort:")56for t in sorted_times:outputAfter sort:for t in sorted_times:
pass 1 of 355print("After sort:")56for t09:00:00 in sorted_times[datetime.time(9, 0), datetime.time(12, 15), datetime.time(15, 30)]:57 print(f" {t09:00:00}")output 09:00:00All 3 passes — pass 1 is the card above pass t1 09:00:00 2 12:15:00 3 15:30:00 business_start ← 09:00:00, business_end ← 17:00:00, test_times ← [datetime.time(8, 30), datetime.time(12, 0), datetime.time(18, 0)]
59# Business hours check60business_start→ 09:00:00 = time(9, 0)61business_end→ 17:00:00 = time(17, 0)6263print("\nBusiness hours check:")64test_times→ [datetime.time(8, 30), datetime.time(12, 0), datetime.time(18, 0)] = [65 time(8, 30),66 time(12, 0),67 time(18, 0)68]output Business hours check:is_during_business ← False
pass 1 of 370for t08:30:00 in test_times[datetime.time(8, 30), datetime.time(12, 0), datetime.time(18, 0)]:71 is_during_business→ False = business_start09:00:00 <= t08:30:00 < business_end17:00:0072 print(f"{t08:30:00} during business: {is_during_businessFalse}")output08:30:00 during business: FalseAll 3 passes — pass 1 is the card above pass tis_during_business1 08:30:00 False 2 12:00:00 True 3 18:00:00 False late ← 23:00:00, early ← 01:00:00, noon ← 12:00:00, all_times ← [datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)]
74# Midnight crossing75late→ 23:00:00 = time(23, 0)76early→ 01:00:00 = time(1, 0)77print(f"\n23:00 < 01:00: {late23:00:00 < early01:00:00}") # True (earlier in day)7879# Special time comparisons80print("\nSpecial comparisons:")81print(f"Midnight: {time.min00:00:00}")82print(f"Almost midnight: {time.max23:59:59.999999}")83print(f"Noon: {time(12, 0)}")8485noon→ 12:00:00 = time(12, 0)86print(f"10:00 < noon: {time(10, 0) < noon12:00:00}")87print(f"14:00 > noon: {time(14, 0) > noon12:00:00}")8889# Filter times90all_times→ [datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)] = [91 time(8, 0),92 time(10, 0),93 time(14, 0),94 time(18, 0)95]9697morning_times→ [datetime.time(8, 0), datetime.time(10, 0)] = [t for t in all_times[datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)] if t < time(12, 0)]98afternoon_times→ [datetime.time(14, 0), datetime.time(18, 0)] = [t for t in all_times[datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)] if t >= time(12, 0)]99100print(f"\nMorning times: {morning_times[datetime.time(8, 0), datetime.time(10, 0)]}")101print(f"Afternoon times: {afternoon_times[datetime.time(14, 0), datetime.time(18, 0)]}")output 23:00 < 01:00: False Special comparisons: Midnight: 00:00:00 Almost midnight: 23:59:59.999999 Noon: 12:00:00 10:00 < noon: True 14:00 > noon: True Morning times: [datetime.time(8, 0), datetime.time(10, 0)] Afternoon times: [datetime.time(14, 0), datetime.time(18, 0)]
time1 ← 09:30:00, time2 ← 14:45:00, time3 ← 09:30:00, times_list ← [datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)]
5# Compare times6time1→ 09:30:00 = time(9, 30)7time2→ 14:45:00 = time(14, 45)8time3→ 09:30:00 = time(9, 30)910print(f"Time 1: {time109:30:00}")11print(f"Time 2: {time214:45:00}")12print(f"Time 3: {time309:30:00}")13print()1415# Comparison operators16print(f"time1 < time2: {time109:30:00 < time214:45:00}")17print(f"time1 > time2: {time109:30:00 > time214:45:00}")18print(f"time1 <= time2: {time109:30:00 <= time214:45:00}")19print(f"time1 >= time2: {time109:30:00 >= time214:45:00}")2021# Equality22print(f"\ntime1 == time2: {time109:30:00 == time214:45:00}")23print(f"time1 == time3: {time109:30:00 == time309:30:00}")24print(f"time1 != time2: {time109:30:00 != time214:45:00}")2526# Find min/max27times_list→ [datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)] = [time109:30:00, time214:45:00, time309:30:00]28earliest→ 09:30:00 = min(times_list[datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)])29latest→ 14:45:00 = max(times_list[datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)])3031print(f"\nEarliest: {earliest09:30:00}")32print(f"Latest: {latest14:45:00}")3334# Check if in time range35check→ 08:30:00 = time(8, 30)36start→ 09:00:00 = time(9, 0)37end→ 17:00:00 = time(17, 0)3839in_range→ False = start09:00:00 <= check08:30:00 <= end17:00:0040print(f"\n{check08:30:00} in range [{start09:00:00}, {end17:00:00}]: {in_rangeFalse}")4142# Sort times43unsorted→ [datetime.time(15, 30), datetime.time(9, 0), datetime.time(12, 15)] = [44 time(15, 30),45 time(9, 0),46 time(12, 15)47]4849print("\nBefore sort:")50for t in unsorted:outputTime 1: 09:30:00 Time 2: 14:45:00 Time 3: 09:30:00 time1 < time2: True time1 > time2: False time1 <= time2: True time1 >= time2: False time1 == time2: False time1 == time3: True time1 != time2: True Earliest: 09:30:00 Latest: 14:45:00 08:30:00 in range [09:00:00, 17:00:00]: False Before sort:for t in unsorted:
pass 1 of 349print("\nBefore sort:")50for t15:30:00 in unsorted[datetime.time(15, 30), datetime.time(9, 0), datetime.time(12, 15)]:51 print(f" {t15:30:00}")output 15:30:00All 3 passes — pass 1 is the card above pass t1 15:30:00 2 09:00:00 3 12:15:00 sorted_times ← [datetime.time(9, 0), datetime.time(12, 15), datetime.time(15, 30)]
53sorted_times→ [datetime.time(9, 0), datetime.time(12, 15), datetime.time(15, 30)] = sorted(unsorted[datetime.time(15, 30), datetime.time(9, 0), datetime.time(12, 15)])5455print("After sort:")56for t in sorted_times:outputAfter sort:for t in sorted_times:
pass 1 of 355print("After sort:")56for t09:00:00 in sorted_times[datetime.time(9, 0), datetime.time(12, 15), datetime.time(15, 30)]:57 print(f" {t09:00:00}")output 09:00:00All 3 passes — pass 1 is the card above pass t1 09:00:00 2 12:15:00 3 15:30:00 business_start ← 09:00:00, business_end ← 17:00:00, test_times ← [datetime.time(8, 30), datetime.time(12, 0), datetime.time(18, 0)]
59# Business hours check60business_start→ 09:00:00 = time(9, 0)61business_end→ 17:00:00 = time(17, 0)6263print("\nBusiness hours check:")64test_times→ [datetime.time(8, 30), datetime.time(12, 0), datetime.time(18, 0)] = [65 time(8, 30),66 time(12, 0),67 time(18, 0)68]output Business hours check:is_during_business ← False
pass 1 of 370for t08:30:00 in test_times[datetime.time(8, 30), datetime.time(12, 0), datetime.time(18, 0)]:71 is_during_business→ False = business_start09:00:00 <= t08:30:00 < business_end17:00:0072 print(f"{t08:30:00} during business: {is_during_businessFalse}")output08:30:00 during business: FalseAll 3 passes — pass 1 is the card above pass tis_during_business1 08:30:00 False 2 12:00:00 True 3 18:00:00 False late ← 23:00:00, early ← 01:00:00, noon ← 12:00:00, all_times ← [datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)]
74# Midnight crossing75late→ 23:00:00 = time(23, 0)76early→ 01:00:00 = time(1, 0)77print(f"\n23:00 < 01:00: {late23:00:00 < early01:00:00}") # True (earlier in day)7879# Special time comparisons80print("\nSpecial comparisons:")81print(f"Midnight: {time.min00:00:00}")82print(f"Almost midnight: {time.max23:59:59.999999}")83print(f"Noon: {time(12, 0)}")8485noon→ 12:00:00 = time(12, 0)86print(f"10:00 < noon: {time(10, 0) < noon12:00:00}")87print(f"14:00 > noon: {time(14, 0) > noon12:00:00}")8889# Filter times90all_times→ [datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)] = [91 time(8, 0),92 time(10, 0),93 time(14, 0),94 time(18, 0)95]9697morning_times→ [datetime.time(8, 0), datetime.time(10, 0)] = [t for t in all_times[datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)] if t < time(12, 0)]98afternoon_times→ [datetime.time(14, 0), datetime.time(18, 0)] = [t for t in all_times[datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)] if t >= time(12, 0)]99100print(f"\nMorning times: {morning_times[datetime.time(8, 0), datetime.time(10, 0)]}")101print(f"Afternoon times: {afternoon_times[datetime.time(14, 0), datetime.time(18, 0)]}")output 23:00 < 01:00: False Special comparisons: Midnight: 00:00:00 Almost midnight: 23:59:59.999999 Noon: 12:00:00 10:00 < noon: True 14:00 > noon: True Morning times: [datetime.time(8, 0), datetime.time(10, 0)] Afternoon times: [datetime.time(14, 0), datetime.time(18, 0)]
time1 ← 09:30:00, time2 ← 14:45:00, time3 ← 09:30:00, times_list ← [datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)]
5# Compare times6time1→ 09:30:00 = time(9, 30)7time2→ 14:45:00 = time(14, 45)8time3→ 09:30:00 = time(9, 30)910print(f"Time 1: {time109:30:00}")11print(f"Time 2: {time214:45:00}")12print(f"Time 3: {time309:30:00}")13print()1415# Comparison operators16print(f"time1 < time2: {time109:30:00 < time214:45:00}")17print(f"time1 > time2: {time109:30:00 > time214:45:00}")18print(f"time1 <= time2: {time109:30:00 <= time214:45:00}")19print(f"time1 >= time2: {time109:30:00 >= time214:45:00}")2021# Equality22print(f"\ntime1 == time2: {time109:30:00 == time214:45:00}")23print(f"time1 == time3: {time109:30:00 == time309:30:00}")24print(f"time1 != time2: {time109:30:00 != time214:45:00}")2526# Find min/max27times_list→ [datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)] = [time109:30:00, time214:45:00, time309:30:00]28earliest→ 09:30:00 = min(times_list[datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)])29latest→ 14:45:00 = max(times_list[datetime.time(9, 30), datetime.time(14, 45), datetime.time(9, 30)])3031print(f"\nEarliest: {earliest09:30:00}")32print(f"Latest: {latest14:45:00}")3334# Check if in time range35check→ 16:15:00 = time(16, 15)36start→ 09:00:00 = time(9, 0)37end→ 17:00:00 = time(17, 0)3839in_range→ True = start09:00:00 <= check16:15:00 <= end17:00:0040print(f"\n{check16:15:00} in range [{start09:00:00}, {end17:00:00}]: {in_rangeTrue}")4142# Sort times43unsorted→ [datetime.time(15, 30), datetime.time(9, 0), datetime.time(12, 15)] = [44 time(15, 30),45 time(9, 0),46 time(12, 15)47]4849print("\nBefore sort:")50for t in unsorted:outputTime 1: 09:30:00 Time 2: 14:45:00 Time 3: 09:30:00 time1 < time2: True time1 > time2: False time1 <= time2: True time1 >= time2: False time1 == time2: False time1 == time3: True time1 != time2: True Earliest: 09:30:00 Latest: 14:45:00 16:15:00 in range [09:00:00, 17:00:00]: True Before sort:for t in unsorted:
pass 1 of 349print("\nBefore sort:")50for t15:30:00 in unsorted[datetime.time(15, 30), datetime.time(9, 0), datetime.time(12, 15)]:51 print(f" {t15:30:00}")output 15:30:00All 3 passes — pass 1 is the card above pass t1 15:30:00 2 09:00:00 3 12:15:00 sorted_times ← [datetime.time(9, 0), datetime.time(12, 15), datetime.time(15, 30)]
53sorted_times→ [datetime.time(9, 0), datetime.time(12, 15), datetime.time(15, 30)] = sorted(unsorted[datetime.time(15, 30), datetime.time(9, 0), datetime.time(12, 15)])5455print("After sort:")56for t in sorted_times:outputAfter sort:for t in sorted_times:
pass 1 of 355print("After sort:")56for t09:00:00 in sorted_times[datetime.time(9, 0), datetime.time(12, 15), datetime.time(15, 30)]:57 print(f" {t09:00:00}")output 09:00:00All 3 passes — pass 1 is the card above pass t1 09:00:00 2 12:15:00 3 15:30:00 business_start ← 09:00:00, business_end ← 17:00:00, test_times ← [datetime.time(8, 30), datetime.time(12, 0), datetime.time(18, 0)]
59# Business hours check60business_start→ 09:00:00 = time(9, 0)61business_end→ 17:00:00 = time(17, 0)6263print("\nBusiness hours check:")64test_times→ [datetime.time(8, 30), datetime.time(12, 0), datetime.time(18, 0)] = [65 time(8, 30),66 time(12, 0),67 time(18, 0)68]output Business hours check:is_during_business ← False
pass 1 of 370for t08:30:00 in test_times[datetime.time(8, 30), datetime.time(12, 0), datetime.time(18, 0)]:71 is_during_business→ False = business_start09:00:00 <= t08:30:00 < business_end17:00:0072 print(f"{t08:30:00} during business: {is_during_businessFalse}")output08:30:00 during business: FalseAll 3 passes — pass 1 is the card above pass tis_during_business1 08:30:00 False 2 12:00:00 True 3 18:00:00 False late ← 23:00:00, early ← 01:00:00, noon ← 12:00:00, all_times ← [datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)]
74# Midnight crossing75late→ 23:00:00 = time(23, 0)76early→ 01:00:00 = time(1, 0)77print(f"\n23:00 < 01:00: {late23:00:00 < early01:00:00}") # True (earlier in day)7879# Special time comparisons80print("\nSpecial comparisons:")81print(f"Midnight: {time.min00:00:00}")82print(f"Almost midnight: {time.max23:59:59.999999}")83print(f"Noon: {time(12, 0)}")8485noon→ 12:00:00 = time(12, 0)86print(f"10:00 < noon: {time(10, 0) < noon12:00:00}")87print(f"14:00 > noon: {time(14, 0) > noon12:00:00}")8889# Filter times90all_times→ [datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)] = [91 time(8, 0),92 time(10, 0),93 time(14, 0),94 time(18, 0)95]9697morning_times→ [datetime.time(8, 0), datetime.time(10, 0)] = [t for t in all_times[datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)] if t < time(12, 0)]98afternoon_times→ [datetime.time(14, 0), datetime.time(18, 0)] = [t for t in all_times[datetime.time(8, 0), datetime.time(10, 0), datetime.time(14, 0), datetime.time(18, 0)] if t >= time(12, 0)]99100print(f"\nMorning times: {morning_times[datetime.time(8, 0), datetime.time(10, 0)]}")101print(f"Afternoon times: {afternoon_times[datetime.time(14, 0), datetime.time(18, 0)]}")output 23:00 < 01:00: False Special comparisons: Midnight: 00:00:00 Almost midnight: 23:59:59.999999 Noon: 12:00:00 10:00 < noon: True 14:00 > noon: True Morning times: [datetime.time(8, 0), datetime.time(10, 0)] Afternoon times: [datetime.time(14, 0), datetime.time(18, 0)]
time_comparison
Comparing times and checking time ranges
Combining with Dates
combine.py
Replay: real traced execution (multi-file project)
# Combine date and time
from datetime import date, time, datetime
# Combine date and time
d = date(2025, 1, 29)
t = time(14, 30, 0)
# Combine using datetime
dt1 = datetime.combine(d, t)
print(f"Date + Time: {dt1}")
# Shorthand: create datetime directly
dt2 = datetime(2025, 1, 29, 14, 30, 0)
print(f"Direct datetime: {dt2}")
# Today at specific time
today_at_9 = datetime.combine(date(2025, 1, 29), time(9, 0))
print(f"\nToday at 9:00: {today_at_9}")
# Start and end of day
start_of_day = datetime.combine(d, time.min)
end_of_day = datetime.combine(d, time.max)
print(f"\nStart of day: {start_of_day}")
print(f"End of day: {end_of_day}")
# Extract date and time from datetime
now = datetime(2025, 1, 29, 14, 30, 45)
extracted_date = now.date()
extracted_time = now.time()
print(f"\nNow: {now}")
print(f"Extracted date: {extracted_date}")
print(f"Extracted time: {extracted_time}")
# Common datetime patterns
print("\nCommon datetimes:")
# Today at specific time
today = date(2025, 1, 29)
today_at_noon = datetime.combine(today, time(12, 0))
print(f"Today at noon: {today_at_noon}")
# Tomorrow at specific time
from datetime import timedelta
tomorrow = today + timedelta(days=1)
tomorrow_at_9 = datetime.combine(tomorrow, time(9, 0))
print(f"Tomorrow at 9:00: {tomorrow_at_9}")
# Next Monday at 9 AM
days_until_monday = (7 - today.weekday()) % 7
if days_until_monday == 0:
days_until_monday = 7
next_monday = today + timedelta(days=days_until_monday)
next_monday_9am = datetime.combine(next_monday, time(9, 0))
print(f"Next Monday 9 AM: {next_monday_9am}")
# Schedule times
print("\nScheduled times for tomorrow:")
schedule_times = [
time(9, 0),
time(11, 30),
time(14, 0),
time(16, 30)
]
for t in schedule_times:
dt = datetime.combine(tomorrow, t)
print(f" {dt}")
# Replace time in datetime
dt = datetime(2025, 1, 29, 14, 30, 45)
new_time = dt.replace(hour=15, minute=30, second=0, microsecond=0)
print(f"\nOriginal: {dt}")
print(f"New time: {new_time}")
d ← 2025-01-29, t ← 14:30:00, dt1 ← 2025-01-29 14:30:00, dt2 ← 2025-01-29 14:30:00
5# Combine date and time6d→ 2025-01-29 = date(2025, 1, 29)7t→ 14:30:00 = time(14, 30, 0)89# Combine using datetime10dt1→ 2025-01-29 14:30:00 = datetime<class 'datetime.datetime'>.combine(d2025-01-29, t14:30:00)11print(f"Date + Time: {dt12025-01-29 14:30:00}")1213# Shorthand: create datetime directly14dt2→ 2025-01-29 14:30:00 = datetime(2025, 1, 29, 14, 30, 0)15print(f"Direct datetime: {dt22025-01-29 14:30:00}")1617# Today at specific time18today_at_9→ 2025-01-29 09:00:00 = datetime<class 'datetime.datetime'>.combine(date(2025, 1, 29), time(9, 0))19print(f"\nToday at 9:00: {today_at_92025-01-29 09:00:00}")2021# Start and end of day22start_of_day→ 2025-01-29 00:00:00 = datetime<class 'datetime.datetime'>.combine(d2025-01-29, time.min00:00:00)23end_of_day→ 2025-01-29 23:59:59.999999 = datetime<class 'datetime.datetime'>.combine(d2025-01-29, time.max23:59:59.999999)2425print(f"\nStart of day: {start_of_day2025-01-29 00:00:00}")26print(f"End of day: {end_of_day2025-01-29 23:59:59.999999}")2728# Extract date and time from datetime29now→ 2025-01-29 14:30:45 = datetime(2025, 1, 29, 14, 30, 45)30extracted_date→ 2025-01-29 = now2025-01-29 14:30:45.date()31extracted_time→ 14:30:45 = now2025-01-29 14:30:45.time()3233print(f"\nNow: {now2025-01-29 14:30:45}")34print(f"Extracted date: {extracted_date2025-01-29}")35print(f"Extracted time: {extracted_time14:30:45}")3637# Common datetime patterns38print("\nCommon datetimes:")3940# Today at specific time41today→ 2025-01-29 = date(2025, 1, 29)42today_at_noon→ 2025-01-29 12:00:00 = datetime<class 'datetime.datetime'>.combine(today2025-01-29, time(12, 0))43print(f"Today at noon: {today_at_noon2025-01-29 12:00:00}")4445# Tomorrow at specific time46from datetime import timedelta47tomorrow→ 2025-01-30 = today2025-01-29 + timedelta(days=1)48tomorrow_at_9→ 2025-01-30 09:00:00 = datetime<class 'datetime.datetime'>.combine(tomorrow2025-01-30, time(9, 0))49print(f"Tomorrow at 9:00: {tomorrow_at_92025-01-30 09:00:00}")5051# Next Monday at 9 AM52days_until_monday→ 5 = (7 - today2025-01-29.weekday()) % 753if days_until_monday == 0:54 days_until_monday = 755next_monday→ 2025-02-03 = today2025-01-29 + timedelta(days=days_until_monday5)56next_monday_9am→ 2025-02-03 09:00:00 = datetime<class 'datetime.datetime'>.combine(next_monday2025-02-03, time(9, 0))57print(f"Next Monday 9 AM: {next_monday_9am2025-02-03 09:00:00}")5859# Schedule times60print("\nScheduled times for tomorrow:")61schedule_times→ [datetime.time(9, 0), datetime.time(11, 30), datetime.time(14, 0), datetime.time(16, 30)] = [62 time(9, 0),63 time(11, 30),64 time(14, 0),65 time(16, 30)66]outputDate + Time: 2025-01-29 14:30:00 Direct datetime: 2025-01-29 14:30:00 Today at 9:00: 2025-01-29 09:00:00 Start of day: 2025-01-29 00:00:00 End of day: 2025-01-29 23:59:59.999999 Now: 2025-01-29 14:30:45 Extracted date: 2025-01-29 Extracted time: 14:30:45 Common datetimes: Today at noon: 2025-01-29 12:00:00 Tomorrow at 9:00: 2025-01-30 09:00:00 Next Monday 9 AM: 2025-02-03 09:00:00 Scheduled times for tomorrow:dt ← 2025-01-30 09:00:00
pass 1 of 468for t09:00:00 in schedule_times[datetime.time(9, 0), datetime.time(11, 30), datetime.time(14, 0), datetime.time(16, 30)]:69 dt→ 2025-01-30 09:00:00 = datetime<class 'datetime.datetime'>.combine(tomorrow2025-01-30, t09:00:00)70 print(f" {dt2025-01-30 09:00:00}")output 2025-01-30 09:00:00All 4 passes — pass 1 is the card above pass tdt1 09:00:00 2025-01-30 09:00:00 2 11:30:00 2025-01-30 11:30:00 3 14:00:00 2025-01-30 14:00:00 4 16:30:00 2025-01-30 16:30:00 dt ← 2025-01-29 14:30:45, new_time ← 2025-01-29 15:30:00
72# Replace time in datetime73dt→ 2025-01-29 14:30:45 = datetime(2025, 1, 29, 14, 30, 45)74new_time→ 2025-01-29 15:30:00 = dt2025-01-29 14:30:45.replace(hour=15, minute=30, second=0, microsecond=0)75print(f"\nOriginal: {dt2025-01-29 14:30:45}")76print(f"New time: {new_time2025-01-29 15:30:00}")output Original: 2025-01-29 14:30:45 New time: 2025-01-29 15:30:00
datetime_combine
Combining date and time objects into datetime
Common Operations
- Access hour, minute, second components
- Format to string
- Compare times
- Combine with date to make datetime
Immutability
Times are immutable - operations return new instances.
Exercise: practical.py
Validate business hours, round times to intervals, and generate time slots