Dates and Times Checkpoint
25 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Real date code mixes all three ideas: parse text, calculate with durations, and show the result in the right zone. This checkpoint builds a small expiry check and then tests your choices.
Parse at the edge, work in real types
Convert text to dates as soon as it enters the program. Inside, use date, datetime and timedelta only. Turn values back into text at the very end, when you print or store them.
from datetime import date, timedelta
start = date.fromisoformat("2026-11-08")print(start + timedelta(days=30))Example 1 · Reading a log with bad rows
from datetime import datetime
rows = ["2026-11-08 09:15", "bad", "2026-11-08 10:45"]stamps = []for row in rows: try: stamps.append(datetime.strptime(row, "%Y-%m-%d %H:%M")) except ValueError: print("skipped:", row)print(stamps[-1] - stamps[0])skipped: bad 1:30:00
What this code does
- A bad row raises
ValueError, which is caught so the loop continues. - The gap between the first and the last valid time is 1 hour 30 minutes.
This keeps string handling in one place, and the rest of the code works with real dates.
Pass the date in
A function that calls now() inside cannot be tested. Give it the current date as a parameter. Production code passes the real date, and tests pass a fixed one.
from datetime import date
def is_expired(expiry, today): return today > expiry
print(is_expired(date(2026, 12, 8), date(2026, 12, 9)))Example 2 · Subscription expiry with a passed-in date
from datetime import date, timedelta
def expiry(start_text, days): return date.fromisoformat(start_text) + timedelta(days=days)
def status(exp, today): left = (exp - today).days return "expired" if left < 0 else f"{left} days left"
exp = expiry("2026-11-08", 30)print(exp)print(status(exp, date(2026, 12, 1)))print(status(exp, date(2026, 12, 20)))2026-12-08 7 days left expired
What this code does
expiryparses text once and adds atimedelta.statusreceivestodayas a parameter, so each call can be checked with a fixed date.
The same call gives the same answer every time, so a test never fails by surprise.
Store UTC, show local
Keep moments as aware UTC datetimes. When you show one to a person, convert it with astimezone to their zone. Do exact arithmetic in UTC. Keep naive datetimes out of shared code.
Example 3 · Show a UTC moment in two zones
from datetime import datetimefrom zoneinfo import ZoneInfo
event = datetime.fromisoformat("2026-11-08T09:30:00+00:00")for name in ["Asia/Kolkata", "America/New_York"]: print(name, event.astimezone(ZoneInfo(name)).strftime("%d %b %H:%M"))Asia/Kolkata 08 Nov 15:00 America/New_York 08 Nov 04:30
What this code does
- The stored text carries an offset, so the parsed value is already aware.
- Each viewer sees the same moment on their own clock.
Finish up
Mark complete & continue
Next: The logging Module and Log Levels