Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeZone to Offset?

Tags:

timezone

go

I want to get the offset in seconds from a specified time zone. That is exactly what tz_offset() in Perl's Time::Zone does: "determines the offset from GMT in seconds of a specified timezone".

Is there already a way of doing this in Go? The input is a string that has the time zone name and that's it, but I know that Go has LoadLocation() in the time package, so string => offset or location => offset should be fine.

Input: "MST"

Output: -25200

like image 288
OmarOthman Avatar asked Oct 25 '25 23:10

OmarOthman


1 Answers

This should do the trick:

location, err := time.LoadLocation("MST")
if err != nil {
    panic(err)
}

tzName, tzOffset := time.Now().In(location).Zone()

fmt.Printf("name: [%v]\toffset: [%v]\n", tzName, tzOffset)

Will print:

name: [MST] offset: [-25200]

Go Playground: https://play.golang.org/p/GVTgnpe1mB1

like image 195
isapir Avatar answered Oct 27 '25 15:10

isapir