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

huangapple go评论91阅读模式
英文:

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

问题

我在尝试格式化和显示一些IBM主机TOD时钟数据时遇到了一些困难。我想要将数据格式化为GMT时间和本地时间(默认情况下为用户指定的时区)。

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

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

// 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?
}

但是,我认为这个结构体没有被导出,所以下面的代码无法工作:

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

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

有没有一种简单的方法来获取这个信息呢?

英文:

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?

答案1

得分: 47

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

package main

import (
	"fmt"
	"time"
)

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

Zone()方法计算时间t所在的时区,返回时区的缩写名称(例如"CET")和相对于UTC的偏移量(以秒为单位)。

英文:

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

得分: 11

Package time

func (Time) Local

func (t Time) Local() Time

Local函数返回一个时间t,其位置设置为本地时间。

func (Time) Zone

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

Zone函数计算时间t生效的时区,返回时区的缩写名称(例如"CET")和相对于UTC的偏移量(以秒为单位)。

type Location

type Location struct {
    // 包含已过滤或未导出的字段
}

Location类型将时间点映射到该时间点使用的时区。通常,Location表示一个地理区域中使用的时间偏移集合,例如中欧的CEST和CET。

var Local *Location = &localLoc

Local表示系统的本地时区。

var UTC *Location = &utcLoc

UTC表示协调世界时(UTC)。

func (Time) In

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

In函数返回一个时间t,其位置信息设置为loc。

如果loc为nil,则会引发panic。

例如,

package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Now()

	// 对于时间t,获取相对于UTC(GMT)的偏移量(以秒为单位)
	_, offset := t.Local().Zone()
	fmt.Println(offset)

	// 对于时间t,以UTC(GMT)和本地时间的格式显示。
	fmt.Println(t.In(time.UTC))
	fmt.Println(t.In(time.Local))
}

输出:

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

> 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

答案3

得分: 8

我不认为手动将时间转换为另一个时区是有意义的。可以使用time.Time.In函数来实现:

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))
}
英文:

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

huangapple
  • 本文由 发表于 2016年1月24日 19:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/34975007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定