1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
## 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:
```go
zone, _ := time.Now().Zone() // try to get my time zone...
loc, _ := time.LoadLocation(zone)
fmt.Println(zone, loc) // prints e.g. CEST UTC -> obviously wrong!
```
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`
```go
package main
import (
"fmt"
"time"
"github.com/thlib/go-timezone-local/tzlocal"
)
func main() {
tzname, err := tzlocal.RuntimeTZ()
fmt.Println(tzname, err)
// example:
// tzname = "Europe/Berlin"
// now you can use tzname to properly set up a location:
loc, _ := time.LoadLocation(tzname)
d0 := time.Date(2021, 10, 30, 20, 0, 0, 0, loc) // DST active:
fmt.Println(d0)
// 2021-10-30 20:00:00 +0200 CEST
d1 := d0.AddDate(0, 0, 1) // add one day, now DST is inactive:
fmt.Println(d1)
// 2021-10-31 20:00:00 +0100 CET
}
```
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 [colm.anseo](https://stackoverflow.com/users/1218512/colm-anseo) and [MrFuppes](https://stackoverflow.com/users/10197418/mrfuppes) for providing the following answers:
* https://stackoverflow.com/a/68938947/175071
* https://stackoverflow.com/a/68966317/175071
|