gpt4 book ai didi

go - 如何在 Go 中表示 RFC-3339 `-00:00` 偏移量?

转载 作者:IT王子 更新时间:2023-10-29 00:55:01 28 4
gpt4 key购买 nike

RFC-3339 第 4.3 节 ( https://www.rfc-editor.org/rfc/rfc3339#section-4.3 ) 定义 -00:00 偏移量如下,它不同于 Z+00: 00

4.3. Unknown Local Offset Convention

If the time in UTC is known, but the offset to local time is unknown,
this can be represented with an offset of "-00:00". This differs
semantically from an offset of "Z" or "+00:00", which imply that UTC
is the preferred reference point for the specified time. RFC2822
[IMAIL-UPDATE] describes a similar convention for email.

但是,我不确定如何在 Go 中表示它。当我使用 -00:00 解析时间并对其进行格式化时,我得到了一个 Z 偏移量。例如:

  • 输入:2018-01-01T00:00:00-00:00
  • 输出:2018-01-01T00:00:00Z

这是一些示例代码 ( https://play.golang.org/p/CVmNnhaSiiT ):

package main

import (
"fmt"
"time"
)

func main() {
t := "2018-01-01T00:00:00-00:00"
fmt.Println("Input " + t)
p, err := time.Parse(time.RFC3339, t)
if err != nil {
fmt.Println(err)
} else {
t2 := p.Format(time.RFC3339)

fmt.Println("Output " + t2)
}
}

最佳答案

Package time

import "time"

RFC3339, RFC822, RFC822Z, RFC1123, and RFC1123Z are useful for formatting; when used with time.Parse they do not accept all the time formats permitted by the RFCs.


Go does not accept all the time formats permitted by the RFCs.

Go time.Time 类型使用整数,与 float 不同,整数没有正负零的概念。 -00:00+00:00的偏移量解析结果是一样的。

例如,

package main

import (
"fmt"
"time"
)

func main() {
var err error
var minus, plus time.Time
t := "2018-01-01T00:00:00-00:00"
minus, err = time.Parse(time.RFC3339, t)
if err != nil {
fmt.Println(err)
}
t = "2018-01-01T00:00:00+00:00"
plus, err = time.Parse(time.RFC3339, t)
if err != nil {
fmt.Println(err)
}
fmt.Println(minus, plus, minus.Equal(plus), minus == plus)
}

Playground :https://play.golang.org/p/Urf8VlKYoMH

输出:

2018-01-01 00:00:00 +0000 UTC 2018-01-01 00:00:00 +0000 UTC true true

关于go - 如何在 Go 中表示 RFC-3339 `-00:00` 偏移量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49539236/

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