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

后端 未结 3 1591
轮回少年
轮回少年 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:46

    Package time

    func (Time) Local

    func (t Time) Local() Time
    

    Local returns t with the location set to local time.

    func (Time) Zone

    func (t Time) Zone() (name string, offset int)
    

    Zone computes the time zone in effect at time t, returning the abbreviated name of the zone (such as "CET") and its offset in seconds east of UTC.

    type Location

    type Location struct {
            // contains filtered or unexported fields
    }
    

    A Location maps time instants to the zone in use at that time. Typically, the Location represents the collection of time offsets in use in a geographical area, such as CEST and CET for central Europe.

    var Local *Location = &localLoc
    

    Local represents the system's local time zone.

    var UTC *Location = &utcLoc
    

    UTC represents Universal Coordinated Time (UTC).

    func (Time) In

    func (t Time) In(loc *Location) Time
    

    In returns t with the location information set to loc.

    In panics if loc is nil.

    For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        t := time.Now()
    
        // For a time t, offset in seconds east of UTC (GMT)
        _, offset := t.Local().Zone()
        fmt.Println(offset)
    
        // For a time t, format and display as UTC (GMT) and local times.
        fmt.Println(t.In(time.UTC))
        fmt.Println(t.In(time.Local))
    }
    

    Output:

    -18000
    2016-01-24 16:48:32.852638798 +0000 UTC
    2016-01-24 11:48:32.852638798 -0500 EST
    

提交回复
热议问题