Design a parking lot
medium 90 minModel a car park with several levels as classes: vehicles, spaces of different sizes, tickets and a replaceable price rule.
Watch lesson4:48
Pricing that changes
Time of day, clock changes, long stays
The point: count time in UTC, look up the rate in local time, add up whole cents hour by hour. Three sentences, and they prevent nearly every pricing bug.
Segments
A stay that crosses the edge of a tariff
Day 200 cents, weekend day 300, night from 22:00 to 07:00 100. Somebody parks on Friday at 20:30 for three hours. Which rate? Cut the stay into started hours and ask for each one: what was the rate when it began?
Friday 20:30 to 23:30
| Hour | Begins | Local rate | Sum |
|---|---|---|---|
| 1 | 20:30 | 200, day | 200 |
| 2 | 21:30 | 200, day | 400 |
| 3 | 22:30 | 100, night | 500 |
from datetime import datetime, timedelta, timezonefrom typing import CallableWEEKDAY, WEEKEND, NIGHT = 200, 300, 100 # cents per started hourdef rate_at(local: datetime) -> int: if local.hour < 7 or local.hour >= 22: return NIGHT return WEEKEND if local.weekday() >= 5 else WEEKDAYdef price_cents(entered_utc: datetime, left_utc: datetime, to_local: Callable[[datetime], datetime]) -> int: """Time is counted in UTC. The rate of each started hour is looked up in local time.""" total, hour = 0, entered_utc while True: total += rate_at(to_local(hour)) hour += timedelta(hours=1) if hour >= left_utc: return totalutc = timezone.utcsame = lambda t: t # a lot whose local time is UTCfriday = datetime(2026, 7, 3, 20, 30, tzinfo=utc)print(price_cents(friday, friday + timedelta(hours=3), same)) # 200 + 200 + 100: 21:30 is still dayprint(price_cents(friday, friday + timedelta(hours=12), same)) # until Saturday 08:30DST
The night that has 25 hours
On Sunday 25 October 2026 clocks in London go back at 02:00 summer time to 01:00 winter time. A car enters at 00:30 and leaves at 03:30. The wall says three hours. Four hours have passed.
from datetime import datetime, timezonefrom zoneinfo import ZoneInfolondon = ZoneInfo("Europe/London")entered = datetime(2026, 10, 25, 0, 30, tzinfo=london) # summer timeleft = datetime(2026, 10, 25, 3, 30, tzinfo=london) # winter time: 02:00 went back to 01:00print(left - entered) # 3:00:00, the clock on the wallprint(left.astimezone(timezone.utc) - entered.astimezone(timezone.utc)) # 4:00:00, the truthLocalDateTime has the same trap and Instant does not.| Night | Wall clock says | Truth | If you bill the wall |
|---|---|---|---|
| Clocks go back (October) | 3 h | 4 h | You lose an hour, and a stay from 01:40 to 01:10 is negative |
| Clocks go forward (March), 00:30 to 03:30 | 3 h | 2 h | The driver pays an hour that did not exist |
How do you measure a stay?
BreaksLocal times
- How
exit.hour - entry.hour, orLocalDateTime.- Limit
- Wrong twice a year, wrong over midnight, negative in the worst case.
- Choose when
- Never.
WorksUTC for length, one rate
- How
- Instants, subtract, round up.
- Limit
- Cannot do night rates.
- Choose when
- The base design.
StrongUTC for length, local time for the rate
- How
- Walk hour by hour in UTC, convert each start to local time, look up the rate.
- Limit
- A year-long stay is 8760 steps. Fine, or use whole days first.
- Choose when
- Any tariff that knows the time of day.
Long stays
Three weeks at the airport
- 1
Cap per day
Whole days times the cap, plus the rest, which is capped too. From entry or by calendar day: decide. From entry is simpler, and two days never meet at midnight.
- 2
Never let it overflow
One bill will not fill 32 bits: 2000 cents a day for 10 years is 7.3 million. A year of income of all car parks will, because 32 bits end near 2.1 billion cents. Use 64 bits and stop thinking about it.
- 3
Forgotten cars
After 30 days raise an alarm, not a bill of 60000 cents. An abandoned car is a task for a person.
- 4
Clock mistakes
Exit before entry must not give a negative fee. Treat it as an error in the clock: log it, charge the minimum.
49.5 hours with a cap of 2000
Money
Integers, and one place to round
| Step | Rule |
|---|---|
| Store | Whole cents plus the currency code |
| Multiply by a factor | Basis points: 1.5 times is 15000 per 10000 |
| Round | Once, at the end of the bill, with a rule the accountant knows |
| Split tax | Tax first, then net = total minus tax, so that they add up |
| Currency | Not all have two decimals. Yen has none |
| Only here does it become 6.00 |