Go · Chapter 14 of 26
Time
By the end of this chapter you can work with time.Time and time.Duration, measure how long something took, compare and move times, read and write times with Go's reference layout, give the hour in IST on a server whose clock is in UTC, and write functions that take now so that every one of them can be tested. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real programs
- 4 tasks
- Go
- In the all labs plan
The first 2 chapters of Go are free. This one comes with the all labs plan.
Why you need this
Almost every question at BijliGo has a time in it. How long did that session last? How many sessions started between 6 pm and 7 pm, Nandini's favourite chart? Is it the evening peak right now, when a kWh costs a quarter more? Which chargers have not sent a heartbeat in the last 90 seconds, so Tenzin's pager should go off?
The data's times are in IST, written 2026-10-14T19:32:00+05:30. The servers that run BijliGo's Go are not: like most servers, their clocks are set to UTC, five and a half hours behind. Code that asks "what hour is it?" without saying where gives 13 when Bengaluru says 19, and the peak price starts at half past eleven at night. And a function that calls the real clock inside gives a different answer every time you run it, so no test can say what it should return.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
duration.go: how long a session has lasted
Write SessionMinutes(s bijli.Session, now time.Time) int: the whole minutes from the session's StartedAt to its StoppedAt, with the seconds dropped. A session still charging has a zero StoppedAt; then count until now instead. Use the now you are given, never the real clock.
The Check expects 57 for S902619 (19:57 to 20:54), 31 for a session from 10:00:00 to 10:31:45, 73 for a session started at 19:57 and still charging with now at 21:10:30 IST, 133 for the same session with now an hour later, and 19 for a session from 23:40 IST (given in UTC) to 23:59 IST.
- Task 2
hours.go: sessions in each hour of the day, in IST
Write SessionsPerHour(sessions []bijli.Session) [24]int: element h counts the sessions whose StartedAt falls in hour h of the day in IST (element 0 is midnight to 1 am, element 18 is 6 pm to 7 pm, element 23 is 11 pm on).
For Wednesday's 3600 sessions the counts are 0 for hours 0 to 5, then 29, 24, 29, 35, 17, 33, 30, 33, 31, 44, 44, 50 for hours 6 to 17, and 285, 382, 626, 835, 704, 369 for hours 18 to 23.
The Check runs as if the server's clock were set to UTC, and also gives every start time in UTC; the counts must be the same, by the hour in IST.
- Task 3
peak.go: the peak-hour tariff
Write PriceAt(base int, at time.Time) int: the price in paise a kWh at the moment at. From 6 pm up to (not including) 10 pm IST it is base times 5 divided by 4; at every other time it is base.
The Check expects 2000 at 17:59:59 IST, 2500 at 18:00:00, 2500 at 21:59:59, 2000 at 22:00:00 for base 2000; 1125 for base 900 at 19:00 IST; 1400 for base 1400 at 03:00 IST; 3000 for base 2400 at 12:30 UTC (18:00 IST); 2400 at 12:29 UTC and at 16:30 UTC (22:00 IST); and 2500 for base 2000 at 10:30 at UTC-4 (20:00 IST). It runs as if the server's clock were set to UTC.
- Task 4 · interview question
Interview: pager.go, the silent-charger pager
last maps a charger's id to the time of its last heartbeat. Write Silent(last map[string]time.Time, now time.Time) []string: the ids whose last heartbeat is more than 90 seconds before now, sorted. Exactly 90 seconds is not silent, and a heartbeat a little after now is not silent. At 19:12:00 IST with last heartbeats 20 s, 150 s, 91 s, 90 s and an hour ago (given in UTC), and 5 s in the future, for C1042, C1057, C1019, C1003, C1101 and C1008, it returns [C1019 C1057 C1101]; ten minutes later, all six.
Write PageLine(id string, last, now time.Time) string: the pager's line, with the last heartbeat's time of day in IST on a 24-hour clock and how long ago in a duration's own printed form:
C1057 silent since 19:09:30 IST, for 2m30s
The Check also expects C1101 silent since 18:12:00 IST, for 1h0m0s when both times are given in UTC, and C1019 silent since 23:59:50 IST, for 1m35s.
Joseph will ask: why is Go's layout 2006-01-02 15:04:05, and why should these functions take now instead of calling time.Now?
What you will learn
- time.Time: a moment, with the time zone it is shown in.
- time.Duration: a length of time, in nanoseconds: Sub, Add, Minutes.
- Comparing times: Before, After and Equal, and why == is a trap.
- Format and Parse: Go's reference time, 2006-01-02 15:04:05.
- Time zones: UTC, In, time.LoadLocation("Asia/Kolkata") and FixedZone.
- Truncate: rounding a time down, and why it fails in IST.
- Passing now in: functions that never call time.Now.
More in Real programs
Build "Time" yourself
BijliGo, a Bengaluru EV charging network, drops chargers at the evening peak. You rewrite its backend in Go. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Go are free. This one comes with the all labs plan.