Converting between time zones
Dealing with time zones is hard. A good way to handle the different time zones is to keep one timezone as referential in the system and convert the others if needed. This recipe shows you how the conversion of time between time zones is done.
How to do it...
- Open the console and create the folder
chapter04/recipe08
. - Navigate to the directory.
- Create the
timezones.go
file with the following content:
package main import ( "fmt" "time" ) func main() { eur, err := time.LoadLocation("Europe/Vienna") if err != nil { panic(err) } t := time.Date(2000, 1, 1, 0, 0, 0, 0, eur) fmt.Printf("Original Time: %v\n", t) phx, err := time.LoadLocation("America/Phoenix") if err != nil { panic(err) } t2 := t.In(phx) fmt.Printf("Converted Time: %v\n", t2) }
- Execute the code by running
go run timezones...