【发布时间】:2018-03-28 16:01:53
【问题描述】:
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)
}
}
【问题讨论】:
-
我不认为 Go 的
time包有未知偏移量的概念;它没有什么特别的用处。如果偏移量确实未知,则时间本身有些无用,因为任何解释都可能不准确 +/- 12 小时 - 足以使偶数日期可能偏离 +/- 1 天。 -
当使用
-00:00时,时间是准确的,因为 RFC 说“UTC 时间是已知的”。只是偏移量是未知的,例如,如果我们有一个时间2018-01-01T12:00:00-00:00,我们知道确切的时间,但不知道偏移量,例如时间是2018-01-01T11:00:00-01:00还是2018-01-01T13:00:00+01:00。 -
在这种情况下,它的语言环境是 UTC。除了及时识别实例之外,提供任何信息不是时间的义务。说“在 UTC 中午,但我不知道它实际发生在哪个地区”的时间是指与
time.Time无关的“它”。