Dealing with Timezones and Unix Timestamp in Golang

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

Dealing with Timezones and Unix Timestamp in Golang

问题

给定以下代码:

https://go.dev/play/p/moLVHXIc4ba

它显示了以下结果:

现在时间:2009-11-10 23:00:00 +0000 UTC m=+0.000000001 | Lima时间:2009-11-10 18:00:00 -0500 -05unix | 现在时间戳:1257894000 | Lima时间戳:1257894000

我不明白为什么当我对在利马地区计算的日期调用.Unix()时,我得到的时间戳与原始的GMT日期相同。

我期望得到与原始GMT时间戳相对应的时间戳,减去5小时,因为利马的时区是UTC-5。

英文:

Given the following code:

https://go.dev/play/p/moLVHXIc4ba

It shows the next result:

now: 2009-11-10 23:00:00 +0000 UTC m=+0.000000001 | inLoc: 2009-11-10 18:00:00 -0500 -05unix | now: 1257894000 | inLoc: 1257894000

I don't get why when I call .Unix() to the date evaluated in Lima location, I get the same timestamp that original date in GMT.

I expect to get the timestamp corresponding to the original timestamp in GMT with 5 hours less as Lima has UTC -5.

答案1

得分: 3

你似乎在寻求这个inLocTimestamp函数。五个小时等于18,000秒(5 * 60 * 60)。

package main

import (
	"fmt"
	"time"
)

func inLocTimestamp(inLoc time.Time) int64 {
	_, offset := inLoc.Zone()
	ts := inLoc.Add(time.Duration(offset) * time.Second)
	return ts.Unix()
}

func inLocTimestampMilli(inLoc time.Time) int64 {
	_, offset := inLoc.Zone()
	ts := inLoc.Add(time.Duration(offset) * time.Second)
	return ts.UnixMilli()
}

func main() {
	loc, _ := time.LoadLocation("America/Lima")
	now := time.Now().Round(0)
	inLoc := now.In(loc)
	fmt.Printf("time       | now: %v | inLoc: %v\n", now, inLoc)
	fmt.Printf("unix       | now: %v | inLoc: %v\n", now.Unix(), inLoc.Unix())
	fmt.Printf("timestamp  | now: %v | inLoc: %v\n", now.Unix(), inLocTimestamp(inLoc))
	fmt.Printf("difference | inLoc: timestamp - unix = %v\n", inLocTimestamp(inLoc)-inLoc.Unix())
}

你可以在这里找到代码的运行示例:https://go.dev/play/p/IyzA4qZLLuS

输出结果如下:

time       | now: 2009-11-10 23:00:00 +0000 UTC | inLoc: 2009-11-10 18:00:00 -0500 -05
unix       | now: 1257894000 | inLoc: 1257894000
timestamp  | now: 1257894000 | inLoc: 1257876000
difference | inLoc: timestamp - unix = -18000

这段代码可以将给定的时间转换为指定时区的时间,并返回对应的时间戳。你可以使用inLocTimestamp函数来获取比原始时间戳早5个小时的时间戳,然后将其传递给前端。

英文:

> I expect to get the timestamp corresponding to the original timestamp in GMT [UTC] with 5 hours less as Lima has UTC -5.
>
> how can I get the timestamp with 5 hours less to pass to frontend?


You appear to be asking for this inLocTimestamp function. Five hours is 18,000 (5 * 60 * 60) seconds.

package main

import (
	"fmt"
	"time"
)

func inLocTimestamp(inLoc time.Time) int64 {
	_, offset := inLoc.Zone()
	ts := inLoc.Add(time.Duration(offset) * time.Second)
	return ts.Unix()
}

func inLocTimestampMilli(inLoc time.Time) int64 {
	_, offset := inLoc.Zone()
	ts := inLoc.Add(time.Duration(offset) * time.Second)
	return ts.UnixMilli()
}

func main() {
	loc, _ := time.LoadLocation("America/Lima")
	now := time.Now().Round(0)
	inLoc := now.In(loc)
	fmt.Printf("time       | now: %v | inLoc: %v\n", now, inLoc)
	fmt.Printf("unix       | now: %v | inLoc: %v\n", now.Unix(), inLoc.Unix())
	fmt.Printf("timestamp  | now: %v | inLoc: %v\n", now.Unix(), inLocTimestamp(inLoc))
	fmt.Printf("difference | inLoc: timestamp - unix = %v\n", inLocTimestamp(inLoc)-inLoc.Unix())
}

https://go.dev/play/p/IyzA4qZLLuS

time       | now: 2009-11-10 23:00:00 +0000 UTC | inLoc: 2009-11-10 18:00:00 -0500 -05
unix       | now: 1257894000 | inLoc: 1257894000
timestamp  | now: 1257894000 | inLoc: 1257876000
difference | inLoc: timestamp - unix = -18000

huangapple
  • 本文由 发表于 2022年3月11日 19:14:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/71437656.html
匿名

发表评论

匿名网友

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

确定