[英]Error while using time.Parse when timezone and offset are together
I have the following code :我有以下代码:
package main
import (
"fmt"
"time"
"log"
)
func main() {
date, err := time.Parse("Monday, 2 January 2006 15:04:05 PM MST-07:00" ,"Thursday, 17 August 2020 13:20:00 PM GMT+08:00")
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(date)
}
And it fails with the following error :它失败并出现以下错误:
2009/11/10 23:00:00 parsing time "Thursday, 17 August 2020 13:20:00 PM GMT+08:00" as "Monday, 2 January 2006 15:04:05 PM MST-07:00": cannot parse ":00" as "-07:00" 2009/11/10 23:00:00 解析时间“2020年8月17日星期四13:20:00 PM GMT+08:00”为“2006年1月2日星期一15:04:05 PM MST-07:00”:无法将“:00”解析为“-07:00”
But it succeeds if I separate MST-07:00 with a space as : "MST -07:00" in both layout sample and actual string.但是如果我在布局示例和实际字符串中用空格将 MST-07:00 分隔为:“MST -07:00”,它就会成功。
What am I doing wrong?我究竟做错了什么?
GMT times undergo special handling by time.Parse
. GMT 时间由time.Parse
进行特殊处理。 The signed offset for GMT in the value must be in the range -23 through +23 excluding zero, and may not include a colon.值中 GMT 的有符号偏移量必须在 -23 到 +23 的范围内(不包括零),并且可能不包括冒号。 The layout should just specify MST
without an offset.布局应该只指定没有偏移的MST
。
For example:例如:
package main
import (
"fmt"
"log"
"time"
)
func main() {
for _, ts := range []string{
"Thursday, 17 August 2020 13:20:00 PM GMT",
"Thursday, 17 August 2020 13:20:00 PM GMT+2",
"Thursday, 17 August 2020 13:20:00 PM GMT-2",
} {
date, err := time.Parse("Monday, 2 January 2006 15:04:05 PM MST", ts)
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(date)
}
}
yields the output:产生输出:
crow@mac:tp$ ./example
2020-08-17 13:20:00 +0000 GMT
2020-08-17 15:20:00 +0200 GMT+2
2020-08-17 11:20:00 -0200 GMT-2
An issue was raised for this a while back, and the outcome was (with reference to an example time string containing GMT+10:00
):不久前为此提出了一个问题,结果是(参考包含GMT+10:00
的示例时间字符串):
The special handling of GMT, which is needed for other things, makes it very difficult to know whether the +10:00 should be considered part of the time zone or left alone to match the layout. GMT 的特殊处理是其他事情所需要的,这使得很难知道 +10:00 是应该被视为时区的一部分还是应该单独保留以匹配布局。
and so the issue was closed without proposed changes.所以这个问题在没有提出更改的情况下就结束了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.