Home > Blockchain >  Parsing time offset in Go using time package
Parsing time offset in Go using time package

Time:11-02

I am trying to use Time package in Go to extract the time details as under. I have been able to successfully parse values such as year, month, date, hour, minutes and seconds. Unfortunately, when I try to use Zone to extract offset, I seem to be getting an incorrect offset.

When I tried to view my Time object, I see two entries for offset, not sure what am I doing incorrectly.

 serverTime := "2021-10-31T22:17:03.996-0700"
    fmt.Println("Server time is: ", serverTime)
    t, _ := time.Parse("2006-01-02T15:04:05.999-0700", serverTime)
    zone, offset := t.Zone()
    fmt.Println("Printing time object: ",t)
    fmt.Println("Year", t.Year())
    fmt.Println("Month", t.Month().String())
    fmt.Println("Date", t.Day())
    fmt.Println("Hour", t.Hour())
    fmt.Println("Minutes", t.Minute())
    fmt.Println("Seconds",t.Second())

    fmt.Println("Zone:", zone)
    fmt.Println("Offset", offset)

The output that is see for offset is: Offset -25200 and I expect it to be -0700

Here is the link to playground

CodePudding user response:

The func(Time) Zone returns the second argument offset which is the seconds east of UTC. So your offset of -0700 is returned as -25200 which is - (7 * 60 * 60)

  • Related