gpt4 book ai didi

go - 如何使用特定时区解析时间

转载 作者:行者123 更新时间:2023-12-05 02:39:58 25 4
gpt4 key购买 nike

我要从字符串中获取时间结构。我正在使用函数 time.ParseTime() 和布局 "2006-01-02 15:04"

当我使用任何有效的时间字符串执行该函数时,我得到一个指向该时间戳的时间结构,但它是 UTC。

如何将其更改为不同的时区?明确地说,我想要相同的时间戳,但时区不同。我不想在时区之间转换;我只想获得相同的时间对象,但不是 UTC。

最佳答案

使用time.ParseInLocation在给定的 Location 中解析时间当没有给出时区时。 time.Local 是您本地的时区,将其作为您的位置传入。

package main

import (
"fmt"
"time"
)

func main() {
// This will honor the given time zone.
// 2012-07-09 05:02:00 +0000 CEST
const formWithZone = "Jan 2, 2006 at 3:04pm (MST)"
t, _ := time.ParseInLocation(formWithZone, "Jul 9, 2012 at 5:02am (CEST)", time.Local)
fmt.Println(t)

// Lacking a time zone, it will use your local time zone.
// Mine is PDT: 2012-07-09 05:02:00 -0700 PDT
const formWithoutZone = "Jan 2, 2006 at 3:04pm"
t, _ = time.ParseInLocation(formWithoutZone, "Jul 9, 2012 at 5:02am", time.Local)
fmt.Println(t)
}

关于go - 如何使用特定时区解析时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68853119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com