Exercise 3 solution from datetime import datetime, timezone, timedelta EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc) def to_us(dt): d = dt - EPOCH return (d.days * 86400 + d.seconds) * 1_000_000 + d.microseconds def from_us(us): return EPOCH + timedelta(microseconds=us) moments = [ datetime(2026, 11, 2, 12, 0, 0, 123456, tzinfo=timezone.utc), datetime(1969, 7, 20, 20, 17, 40, tzinfo=timezone.utc), datetime(2000, 1, 1, tzinfo=timezone.utc), datetime(1970, 1, 1, tzinfo=timezone.utc), ] for m in sorted(moments): us = to_us(m) print(us, from_us(us) == m) m = moments[0] secs, us = divmod(to_us(m), 1_000_000) days, rem = divmod(secs, 86400) print("Days since epoch:", days, "seconds into day:", rem, "microseconds:", us) a = datetime(2016, 12, 31, tzinfo=timezone.utc) b = datetime(2017, 1, 1, tzinfo=timezone.utc) print("Days between:", (to_us(b) - to_us(a)) // 1_000_000 // 86400) print("Seconds between:", (b - a).total_seconds()) try: datetime(2016, 12, 31, 23, 59, 60, tzinfo=timezone.utc) except ValueError as e: print("ValueError:", e) Output (run, and exactly as shown): -14182940000000 True 0 True 946684800000000 True 1793620800123456 True Days since epoch: 20759 seconds into day: 43200 microseconds: 123456 Days between: 1 Seconds between: 86400.0 ValueError: second must be in 0..59, not 60 Explanation: integer microseconds round-trip exactly, which a float of seconds cannot promise at nanosecond scale. Dates before 1970 give negative values and still sort correctly. divmod splits a count into whole days, seconds into the day and microseconds. Unix time pretends every day has exactly 86400 seconds: the day containing the 2016 leap second is still 86400 seconds long by the timestamps, and Python's datetime refuses a second value of 60, so a leap second can never appear as its own moment.