3

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

OmarOthman
  • 1,718
  • 2
  • 19
  • 36
  • 2
    http://stackoverflow.com/questions/34975007/in-go-how-can-i-extract-the-value-of-my-current-local-time-offset – alex vasi Jan 27 '16 at 12:16
  • This is not the same question by any means. I edited my question to make it clearer, though. – OmarOthman Jan 27 '16 at 12:36
  • 4
    @OmarOthman You can't get offset by string because offset depends on time as well. Think about [DST](https://en.wikipedia.org/wiki/Daylight_saving_time), or the fact that [time zones change](http://play.golang.org/p/6BHVVE9aIT). – Ainar-G Jan 27 '16 at 12:46
  • @Ainar-G Thank you for your comment, I am a beginner in that world. But take a look [here](https://metacpan.org/source/GAAS/HTTP-Date-6.02/lib/HTTP/Date.pm#L71), this is exactly what is happening! – OmarOthman Jan 28 '16 at 11:45
  • @OmarOthman hm, [time.Time.Zone](https://golang.org/pkg/time/#Time.Zone) function returns offset of the current zone from UTC (GMT basically). `loc, err := time.LoadLocation("MST"); _, offset := Time.Now().In(loc).Zone()` – alex vasi Jan 29 '16 at 09:41

2 Answers2

4

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

isapir
  • 21,295
  • 13
  • 115
  • 116
  • Be careful with the short timezone identifiers. I wanted Eastern Time, so I put EST, but that gave me Eastern Standard Time when it's currently Eastern Daylight Time in New York. I had to put "America/New_York" to get the _current_ timezone, rather than an exact daylight-savings-shifted timezone. – Milos Ivanovic Jul 15 '22 at 05:58
0

Here is the code, that calculates current offset between local and specified timezones. I agree with Ainar-G's comment that offset makes sense only with relation to specified moment in time:

package main

import (
    "fmt"
    "time"
)

func main() {
    loc, err := time.LoadLocation("MST")
    if err != nil {
        fmt.Println(err)
    }

    now := time.Now()
    _, destOffset := now.In(loc).Zone()
    _, localOffset := now.Zone()

    fmt.Println("Offset:", destOffset-localOffset)
}
Community
  • 1
  • 1
alex vasi
  • 5,304
  • 28
  • 31
  • I don't want the offset from the local time zone, I want it from GMT. Does that change anything? Could you please change your code to reflect this? – OmarOthman Jan 28 '16 at 12:16
  • Also, could you please take a look at my last comment on the question itself? – OmarOthman Jan 28 '16 at 12:17