Work with UTC and locations
Work with UTC and locations
A location maps instants to local calendar readings. UTC has offset zero. FixedZone creates a constant offset (seconds east of UTC), while LoadLocation loads named time-zone rules, usually from IANA zone data. Use the zone of the event, not the machine default, when interpreting a local appointment.
package main
import ( "fmt" "time")
func check(e error) { if e != nil { panic(e) }}
func main() { loc := time.FixedZone("Demo", 3*3600+30*60) t, e := time.ParseInLocation("2006-01-02 15:04", "2026-01-15 09:00", loc) check(e) fmt.Println(t.Format(time.RFC3339)) fmt.Println(t.UTC().Format(time.RFC3339))}ParseInLocation interprets text without a zone using loc. FixedZone here is a teaching fixture, not the rules of any country. In and UTC preserve the instant and change the display location. Equal compares instants rather than location identity.
Expected output:
2026-01-15T09:00:00+03:302026-01-15T05:30:00ZLoad real historical zone rules
package main
import ( "fmt" "time")
func check(e error) { if e != nil { panic(e) }}
/* Zone database required; historical 2024 fixture. */func main() { loc, e := time.LoadLocation("America/New_York") check(e) for _, m := range []time.Month{time.January, time.July} { t := time.Date(2024, m, 15, 12, 0, 0, 0, loc) fmt.Println(t.Format(time.RFC3339)) fmt.Println(t.Equal(t.UTC())) }}The historical New York offsets differ between January and July. Loading can fail if zone data is unavailable. Handle that error instead of silently substituting UTC. For minimal deployments consider importing time/tzdata (which embeds a fallback database); plan updates when rules change. Store an instant and, where future local scheduling matters, the named zone and local scheduling intent.
Expected output:
2024-01-15T12:00:00-05:00true2024-07-15T12:00:00-04:00trueTry it locally
Convert 2026-01-15T10:00:00Z to a fixed +02:00 location and print RFC3339 and the result of Equal against the original.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Time and scheduling 03: Work with UTC and locations
Check the lesson objective before moving on.
What does t.In(loc) change?
Why prefer a named location for future local schedules?