gpt4 book ai didi

time - 如何解析 Go 中的非标准日期/时间?中欧夏令时

转载 作者:IT王子 更新时间:2023-10-29 02:34:29 26 4
gpt4 key购买 nike

我正在尝试解析电子邮件标题中收到的日期。最近我被困在这个 Thu, 7 Aug 2014 14:03:05 +0200 (Mitteleuropäische Sommerzeit) 上。我应该使用什么样的布局? Mon, 02 Jan 2006 15:04:05 -0700 (MST) 没用。

我也尝试了下面的解决方法,但它仍然不起作用。我不确定为什么 Mitt... 没有被替换。

if strings.Contains(d, "Mitteleuropäische Sommerzeit") {
d = strings.Replace(d, "Mitteleuropäische Sommerzeit", "CEST", 1)
}

最佳答案

Mitteleuropäische Sommerzeit 部分确实无法被 time 包识别。但是当你用 CEST 替换它时它会完美地工作:

var d = "Thu, 7 Aug 2014 14:03:05 +0200 (Mitteleuropäische Sommerzeit)"
_, err := time.Parse("Mon, _2 Jan 2006 15:04:05 -0700 (MST)", d)
if err != nil {
fmt.Println(err) // There is indeed an error
}

d = strings.Replace(d, "Mitteleuropäische Sommerzeit", "CEST", 1)
t, err := time.Parse("Mon, _2 Jan 2006 15:04:05 -0700 (MST)", d)
if err != nil {
fmt.Println(err) // No error this time
}
fmt.Println(t) // 2014-08-07 14:03:05 +0200 CEST

关于 playground .

不要忘记在布局中写 _2 而不是 2,这样也可以解析包含两个数字的日期。

关于time - 如何解析 Go 中的非标准日期/时间?中欧夏令时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25182585/

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