dqtok88424
2016-01-24 11:08
浏览 241
已采纳

在Go中,如何提取当前本地时间偏移的值?

I'm a Go newbie, and 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 zone the user specifies).

For this, I need to get the value of the local time offset from GMT as a signed integer number of seconds.

In zoneinfo.go (which I confess I don't fully understand), I can see

// A zone represents a single time zone such as CEST or CET.
type zone struct {
    name   string // abbreviated name, "CET"
    offset int    // seconds east of UTC
    isDST  bool   // is this zone Daylight Savings Time?
}

but this is not, I think, exported, so this code doesn't work:

package main
import ( "time"; "fmt" )

func main() {
    l, _ := time.LoadLocation("Local")
    fmt.Printf("%v
", l.zone.offset)
}

Is there a simple way to get this information?

图片转代码服务由CSDN问答提供 功能建议

我是Go新手,我在尝试格式化和显示一些IBM大型机TOD时钟时遇到了困难 数据。 我想同时在GMT和本地时间中设置数据格式(默认设置-否则在用户指定的区域中)。

为此,我需要获取本地值 GMT时间偏移量,以秒为单位的有符号整数。

在zoneinfo.go(我承认我并不完全了解)中,我可以看到

  //区域表示单个时区,例如CEST或CET。
type zone struct {
 name string //缩写名称,“ CET” 
 offset int // UTC以东的秒数
  isDST bool //是该区域的夏时制吗?
} 
   
 
 

,但是我认为这没有导出,因此该代码不起作用:< / p>

 包main 
import(“ time”;“ fmt”)
 
func main(){
l,_:= time.LoadLocation(“ Local”)
  fmt.Printf(“%v 
”,l.zone.offset)
} 
   
 
 

是否有一种简单的方法来获取此信息?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dourui7186 2016-01-24 12:41
    已采纳

    You can use the Zone() method on the time type:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        t := time.Now()
        zone, offset := t.Zone()
        fmt.Println(zone, offset)
    }
    

    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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douzachan4578 2016-01-24 12:42

    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
    
    评论
    解决 无用
    打赏 举报
  • douzhenao6515 2016-01-24 12:56

    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))
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题