How can I extract the value of my current local time offset?

后端 未结 3 1694
轮回少年
轮回少年 2021-02-19 00:22

I\'m struggling a bit trying to format and display some IBM mainframe TOD clock data. I want to format the data in both GMT and local time (as the default -- otherwise in the zo

3条回答
  •  不要未来只要你来
    2021-02-19 00:51

    I don't think it makes sense to manually convert time to another TZ. Use time.Time.In function:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func printTime(t time.Time) {
        zone, offset := t.Zone()
        fmt.Println(t.Format(time.Kitchen), "Zone:", zone, "Offset UTC:", offset)
    }
    
    func main() {
        printTime(time.Now())
        printTime(time.Now().UTC())
    
        loc, _ := time.LoadLocation("America/New_York")
        printTime(time.Now().In(loc))
    }
    

提交回复
热议问题