How do you convert a time offset to a location/timezone in Go

Use:

loc := time.FixedZone("UTC+11", +11*60*60)

Then set to this location:

t = t.In(loc)

Try this:

package main

import (
    "fmt"
    "time"
)

func main() {
    loc := time.FixedZone("UTC+11", +11*60*60)

    t := time.Now()
    fmt.Println(t)
    fmt.Println(t.Location())

    t = t.In(loc)
    fmt.Println(t)
    fmt.Println(t.Location())

    fmt.Println(t.UTC())
    fmt.Println(t.Location())
}

Output:

2009-11-10 23:00:00 +0000 UTC m=+0.000000001
UTC
2009-11-11 10:00:00 +1100 UTC+11
UTC+11
2009-11-10 23:00:00 +0000 UTC
UTC+11

if len(offset) == 5 {
    hours, ok1 := strconv.ParseInt(offset[:3], 10, 0)
    mins, ok2 := strconv.ParseInt(offset[3:5], 10, 0)
    if ok1 == nil && ok2 == nil {
        t = t.In(time.FixedZone("Fixed", int((hours*60+mins)*60)))
        fmt.Println(t)
        fmt.Println(t.Location())
    }
}

Tags:

Timezone

Go