如何提取当前本地时间偏移量的值? [英] How can I extract the value of my current local time offset?

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

问题描述

我在尝试格式化和显示一些IBM大型机TOD时钟数据方面有些费力.我想同时在GMT和本地时间设置数据格式(默认设置-否则在用户指定的区域中).

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).

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

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

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

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?

推荐答案

您可以在时间类型上使用Zone()方法:

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计算在时间t生效的时区,返回时区的缩写名称(例如"CET")及其在UTC以东的秒数内的偏移量.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆