本文介绍了在Go中,我如何提取当前本地时间偏移的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Go的新手,我努力尝试格式化并显示一些IBM大型机TOD时钟数据。我想在GMT和当地时间格式化数据(作为默认值 - 否则在用户指定的区域)。

为此,我需要获取GMT的本地时间偏移值是一个有符号整数秒。



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

  //区域表示单个时区,例如CEST或CET。 
类型区域结构{
名称字符串//缩写名称,CET
偏移量int // UTC以东秒
isDST bool //是否为此区域夏令时?
}

但我认为这不是导出的,所以这段代码没有work:

  package main 
import(time;fmt)

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

有没有简单的方法来获取这些信息?

  package主要

导入(
fmt
时间


func main(){
t:= time .Now()
_,z:= t.Zone()
fmt.Println(z)
}

Zone在时间t计算有效的时区,返回区域的缩写名称(例如CET)及其在UTC以东秒的偏移量。 / em>


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\n", l.zone.offset)
}

Is there a simple way to get this information?

解决方案

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

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    _, z := t.Zone()
    fmt.Println(z)
}

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.

这篇关于在Go中,我如何提取当前本地时间偏移的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:48