Change to local timezone

Avatar of the author Willem Schots
23 Jan, 2024
~2 min.
RSS

You know how you need to set up a timezone for your phone or laptop when you first configure it?

Most computer systems usually have a local timezone configured so that they can coordinate the system clock with the rest of the world via NTP.

In the Go time package this local timezone is modelled using the time.Local location. In this code snippet we will see how to change a time.Time from a different location to the time.Local location.

Before we dive in, it’s good to know how time.Time values represent time instants. Learn more here.

Change Location to Local

Converting a time.Time to time.Local is done using the Local method on the time.Time type.

For example, below we change a time from UTC+3 to the local timezone. The local timezone depends on the system configuration, in the demo environment below it happens to be UTC.

package main
import (
"fmt"
"time"
)
func main() {
// t1 is time in "UTC+3" timezone
eastOfUTC := time.FixedZone("UTC+3", 3*60*60)
t1 := time.Date(2024, 01, 22, 13, 37, 0, 0, eastOfUTC)
// t2 represents the same time instant in the local timezone
t2 := t1.Local()
fmt.Println(t1.String())
fmt.Println(t2.String())
fmt.Printf("same time instant: %v\n", t1.Equal(t2))
}
Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”Χ”
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Go v1.23
output

In this example, we initially have a time value t1 in the UTC+3 location (a fixed timezone).

We then call t1.Local() to create t2 in the local timezone (UTC in this case).

t1 and t2 represent the same time instant, but they will have different clock readings. t2 will read 3 hours before t1.

Run the example to verify this.

Get my free newsletter every second week

Used by 500+ developers to boost their Go skills.

"I'll share tips, interesting links and new content. You'll also get a brief guide to time for developers for free."

Avatar of the author
Willem Schots

Hello! I'm the Willem behind willem.dev

I created this website to help new Go developers, I hope it brings you some value! :)

You can follow me on Bluesky, Twitter/X or LinkedIn.

Thanks for reading!