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

前端 未结 2 1938
有刺的猬
有刺的猬 2020-12-07 03:12

Given an arbitrary time offset, how does one go about creating a usable time.Location object that represents that time offset?

The following code parses

2条回答
  •  一生所求
    2020-12-07 03:31

    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())
        }
    }
    

提交回复
热议问题