# Time 9, Exercise 3: a flight across zones, the date line and daylight saving from datetime import datetime, timedelta, timezone, date # Part 1: Tokyo (+9) to Los Angeles (-8 in winter). Leave 17:00 Monday, flight takes 10 hours. tokyo = timezone(timedelta(hours=9)) los_angeles = timezone(timedelta(hours=-8)) leave = datetime(2027, 1, 11, 17, 0, tzinfo=tokyo) # 11 January 2027 is a Monday arrive = leave + timedelta(hours=10) print("leave Tokyo: ", leave.strftime("%a %d %b %H:%M")) print("arrive Los Angeles:", arrive.astimezone(los_angeles).strftime("%a %d %b %H:%M")) print("clock hours between the two clocks:", 9 - (-8)) # Part 2: Samoa moved across the date line on 30 December 2011 and skipped that whole day. last_day = date(2011, 12, 29) next_day = date(2011, 12, 31) print("Samoa went from", last_day.strftime("%A %d %B"), "to", next_day.strftime("%A %d %B")) print("Samoa days in 2011:", 365 - 1) # Part 3: William Willett's 1907 plan: put the clocks forward 20 minutes on each of four # Sundays in April, and back again in September. How much is that in total? steps = 4 step_min = 20 total = steps * step_min print("total shift:", total, "minutes =", total // 60, "h", total % 60, "min") # Daylight saving in the usual form: one hour. On a day with sunset at 17:30 sun time, # the clock then shows sunset at what time, and how many evening daylight minutes are gained? sunset = 17 * 60 + 30 shown = sunset + 60 print("clock shows sunset at", shown // 60, ":", shown % 60) print("evening daylight gained after a 17:00 finish: ", (shown - 17 * 60), "min instead of", (sunset - 17 * 60)) # Output when run: # leave Tokyo: Mon 11 Jan 17:00 # arrive Los Angeles: Mon 11 Jan 10:00 # clock hours between the two clocks: 17 # Samoa went from Thursday 29 December to Saturday 31 December # Samoa days in 2011: 364 # total shift: 80 minutes = 1 h 20 min # clock shows sunset at 18 : 30 # evening daylight gained after a 17:00 finish: 90 min instead of 30 # Reading the output: leaving Monday evening and arriving Monday morning is possible because # the clock gap is 17 hours and the flight lasts only 10, so the traveller "arrives before # leaving" by 7 clock hours. Samoa moved from UTC-11 to UTC+13 and skipped Friday 30 December # 2011. The sunset is not later; only the label on the clock changed.