Get the full name of your local time zone (OS setting)
Works on windows, linux and macos
Why?
built-in functionality sometimes won't suffice:
zone, _ := time.Now().Zone()
loc, _ := time.LoadLocation(zone)
fmt.Println(zone, loc)
localizing a date with obtained loc
will cause
panic: time: missing Location in call to Date
Package Usage
go get github.com/thlib/go-timezone-local/tzlocal
See it in action:
Open your project folder
Create a file main.go
package main
import (
"fmt"
"time"
"github.com/thlib/go-timezone-local/tzlocal"
)
func main() {
tzname, err := tzlocal.RuntimeTZ()
fmt.Println(tzname, err)
loc, _ := time.LoadLocation(tzname)
d0 := time.Date(2021, 10, 30, 20, 0, 0, 0, loc)
fmt.Println(d0)
d1 := d0.AddDate(0, 0, 1)
fmt.Println(d1)
}
Run the following commands:
go mod init example.com/yourpackage
go mod vendor
go run main.go
It should print the go runtime timezone.
For contributors to update the list of time zones on windows
Clone github.com/thlib/go-timezone-local
Change directory to go-timezone-local
cd go-timezone-local
go generate ./...
Credits
All credit goes to the various contributors, colm.anseo and MrFuppes for providing the following answers: