Time Zones
Work with locations.
Time Zones is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Time Carries a Location
Every time.Time has an associated location (time zone). The same instant can be displayed differently depending on the zone.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, 5, 30, 12, 0, 0, 0, time.UTC)
fmt.Println(t.Location())
}UTC and Local
Two built-in locations are always available:
time.UTCfor Coordinated Universal Time.time.Localfor the machine's local zone.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, 5, 30, 12, 0, 0, 0, time.UTC)
fmt.Println(t.UTC().Hour())
}Loading a Named Zone
time.LoadLocation(name) looks up an IANA zone like "Europe/Istanbul" or "America/New_York".
package main
import (
"fmt"
"time"
)
func main() {
loc, err := time.LoadLocation("UTC")
fmt.Println(loc, err)
}Converting Zones with In
t.In(loc) returns the same instant displayed in a different zone. The underlying moment does not change, only its presentation.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, 5, 30, 12, 0, 0, 0, time.UTC)
fmt.Println(t.In(time.UTC).Hour())
}Same Instant, Different Clocks
Noon UTC is a single instant, but a viewer in another zone sees a different wall-clock hour. The Sub difference between them is zero.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, 5, 30, 12, 0, 0, 0, time.UTC)
u := t.UTC()
fmt.Println(t.Sub(u))
}Building Time in a Zone
Pass a location as the last argument to time.Date to create a time directly in that zone.
package main
import (
"fmt"
"time"
)
func main() {
loc, _ := time.LoadLocation("UTC")
t := time.Date(2026, 5, 30, 9, 0, 0, 0, loc)
fmt.Println(t.Hour())
}Reading the Zone Offset
t.Zone() returns the zone's abbreviation and its offset from UTC in seconds.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, 5, 30, 12, 0, 0, 0, time.UTC)
name, offset := t.Zone()
fmt.Println(name, offset)
}Fixed Zones
When you do not need full IANA rules, time.FixedZone(name, offsetSeconds) creates a simple constant-offset zone.
package main
import (
"fmt"
"time"
)
func main() {
loc := time.FixedZone("GMT+3", 3*3600)
t := time.Date(2026, 5, 30, 12, 0, 0, 0, loc)
name, off := t.Zone()
fmt.Println(name, off)
}Formatting Across Zones
Convert with In before formatting to show a timestamp in the reader's zone.
package main
import (
"fmt"
"time"
)
func main() {
loc := time.FixedZone("GMT+3", 3*3600)
t := time.Date(2026, 5, 30, 12, 0, 0, 0, time.UTC)
fmt.Println(t.In(loc).Format("15:04 MST"))
}Store UTC, Display Local
A widely used rule: store and compute in UTC, and convert to a local zone only when showing times to users. This avoids ambiguity.
package main
import (
"fmt"
"time"
)
func main() {
stored := time.Date(2026, 5, 30, 9, 0, 0, 0, time.UTC)
loc := time.FixedZone("GMT+2", 2*3600)
fmt.Println("shown:", stored.In(loc).Hour())
}Handling Load Errors
LoadLocation can fail if the zone database is missing or the name is unknown. Always check the error.
package main
import (
"fmt"
"time"
)
func main() {
_, err := time.LoadLocation("Mars/Olympus")
if err != nil {
fmt.Println("unknown zone")
}
}Quick Check
What does t.In(loc) change?
Recap: Time Zones
You learned to work with locations:
- Every time has a location;
UTCandLocalare built in. - Load named zones with
LoadLocationor build offsets withFixedZone. - Convert with
In; store UTC and display local.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, 5, 30, 15, 0, 0, 0, time.UTC)
loc := time.FixedZone("GMT-5", -5*3600)
fmt.Println(t.In(loc).Format("15:04 MST"))
}Frequently asked questions
Is the “Time Zones” lesson free?
Yes — the full text of “Time Zones” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Time Zones”?
Work with locations. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Time Zones” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.