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

在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?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器