Skip to content

Instantly share code, notes, and snippets.

@crazyoptimist
Created July 25, 2024 17:24
Show Gist options
  • Save crazyoptimist/ae988a41364c147b36bd261edca9734f to your computer and use it in GitHub Desktop.
Save crazyoptimist/ae988a41364c147b36bd261edca9734f to your computer and use it in GitHub Desktop.
Guide for using time zone in go.
package main
import (
"fmt"
"time"
_ "time/tzdata" // Embed the timezone database
)
func main() {
utcTime := time.Date(2024, time.Now().Month(), 26, 1, 30, 0, 0, time.UTC)
loc, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
fmt.Println("error loading location: ", err)
return
}
localTime := utcTime.In(loc)
fmt.Println("UTC Day: ", utcTime.Day())
fmt.Println("PDT Day: ", localTime.Day())
fmt.Println("Are those two times the same?", utcTime.Equal(localTime))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment