go 时间:time.Parse无法解析具有不寻常UTC偏移量的时戳

tzcvj98z  于 6个月前  发布在  Go
关注(0)|答案(7)|浏览(42)

我将系统中425个时区的每个时间转储到一个文件中,并尝试解析时间戳。我得到了17个错误,都是由于time.Parse无法解析不寻常的偏移量,如+0545(在Asia/Kathmandu中使用)或+13(在Pacific/Fakaofo中使用)。
完整的失败日志:

Timezone:	 America/Scoresbysund
zdump time:	 Sun Jun 24 10:43:46 2018 +00
time.Parse:	 parsing time "Sun Jun 24 10:43:46 2018 +00": extra text: +00 

Timezone:	 Asia/Colombo
zdump time:	 Sun Jun 24 16:13:46 2018 +0530
time.Parse:	 parsing time "Sun Jun 24 16:13:46 2018 +0530": extra text: +0530 

Timezone:	 Asia/Kabul
zdump time:	 Sun Jun 24 15:13:46 2018 +0430
time.Parse:	 parsing time "Sun Jun 24 15:13:46 2018 +0430": extra text: +0430 

Timezone:	 Asia/Kathmandu
zdump time:	 Sun Jun 24 16:28:46 2018 +0545
time.Parse:	 parsing time "Sun Jun 24 16:28:46 2018 +0545": extra text: +0545 

Timezone:	 Asia/Tehran
zdump time:	 Sun Jun 24 15:13:46 2018 +0430
time.Parse:	 parsing time "Sun Jun 24 15:13:46 2018 +0430": extra text: +0430 

Timezone:	 Asia/Yangon
zdump time:	 Sun Jun 24 17:13:46 2018 +0630
time.Parse:	 parsing time "Sun Jun 24 17:13:46 2018 +0630": extra text: +0630 

Timezone:	 Atlantic/Azores
zdump time:	 Sun Jun 24 10:43:46 2018 +00
time.Parse:	 parsing time "Sun Jun 24 10:43:46 2018 +00": extra text: +00 

Timezone:	 Australia/Eucla
zdump time:	 Sun Jun 24 19:28:46 2018 +0845
time.Parse:	 parsing time "Sun Jun 24 19:28:46 2018 +0845": extra text: +0845 

Timezone:	 Australia/Lord_Howe
zdump time:	 Sun Jun 24 21:13:46 2018 +1030
time.Parse:	 parsing time "Sun Jun 24 21:13:46 2018 +1030": extra text: +1030 

Timezone:	 Indian/Cocos
zdump time:	 Sun Jun 24 17:13:46 2018 +0630
time.Parse:	 parsing time "Sun Jun 24 17:13:46 2018 +0630": extra text: +0630 

Timezone:	 Pacific/Apia
zdump time:	 Sun Jun 24 23:43:46 2018 +13
time.Parse:	 parsing time "Sun Jun 24 23:43:46 2018 +13": extra text: +13 

Timezone:	 Pacific/Chatham
zdump time:	 Sun Jun 24 23:28:46 2018 +1245
time.Parse:	 parsing time "Sun Jun 24 23:28:46 2018 +1245": extra text: +1245 

Timezone:	 Pacific/Enderbury
zdump time:	 Sun Jun 24 23:43:46 2018 +13
time.Parse:	 parsing time "Sun Jun 24 23:43:46 2018 +13": extra text: +13 

Timezone:	 Pacific/Fakaofo
zdump time:	 Sun Jun 24 23:43:46 2018 +13
time.Parse:	 parsing time "Sun Jun 24 23:43:46 2018 +13": extra text: +13 

Timezone:	 Pacific/Kiritimati
zdump time:	 Mon Jun 25 00:43:46 2018 +14
time.Parse:	 parsing time "Mon Jun 25 00:43:46 2018 +14": extra text: +14 

Timezone:	 Pacific/Marquesas
zdump time:	 Sun Jun 24 01:13:46 2018 -0930
time.Parse:	 parsing time "Sun Jun 24 01:13:46 2018 -0930": extra text: -0930 

Timezone:	 Pacific/Tongatapu
zdump time:	 Sun Jun 24 23:43:46 2018 +13
time.Parse:	 parsing time "Sun Jun 24 23:43:46 2018 +13": extra text: +13

https://gist.github.com/ALTree/de33561c1cc00ac46e2e9e6d6cca52fe

uyhoqukh

uyhoqukh1#

https://golang.org/cl/120558提到了这个问题:time: accept UTC offsets +13,+14 in Parse, reject -13,-14

wgeznvg7

wgeznvg72#

https://golang.org/cl/130696提到了这个问题:time: allow +00 as numeric timezone name and GMT offset

uqxowvwt

uqxowvwt3#

+14+00 现在已正确解析,降至10次失败,所有失败均由非整数UTC偏移量引起:

$ gotip run parse_timezones.go 
Timezone:    Indian/Cocos
zdump time:  Sun Aug 5 18:25:10 2018 +0630
time.Parse:  parsing time "Sun Aug 5 18:25:10 2018 +0630" as "Mon Jan 2 15:04:05 2006 MST": cannot parse "+0630" as "MST" 

Timezone:    Asia/Kabul
zdump time:  Sun Aug 5 16:25:10 2018 +0430
time.Parse:  parsing time "Sun Aug 5 16:25:10 2018 +0430" as "Mon Jan 2 15:04:05 2006 MST": cannot parse "+0430" as "MST" 

Timezone:    Australia/Lord_Howe
zdump time:  Sun Aug 5 22:25:10 2018 +1030
time.Parse:  parsing time "Sun Aug 5 22:25:10 2018 +1030" as "Mon Jan 2 15:04:05 2006 MST": cannot parse "+1030" as "MST" 

Timezone:    Australia/Eucla
zdump time:  Sun Aug 5 20:40:10 2018 +0845
time.Parse:  parsing time "Sun Aug 5 20:40:10 2018 +0845" as "Mon Jan 2 15:04:05 2006 MST": cannot parse "+0845" as "MST" 

Timezone:    Asia/Kathmandu
zdump time:  Sun Aug 5 17:40:10 2018 +0545
time.Parse:  parsing time "Sun Aug 5 17:40:10 2018 +0545" as "Mon Jan 2 15:04:05 2006 MST": cannot parse "+0545" as "MST" 

Timezone:    Pacific/Chatham
zdump time:  Mon Aug 6 00:40:10 2018 +1245
time.Parse:  parsing time "Mon Aug 6 00:40:10 2018 +1245" as "Mon Jan 2 15:04:05 2006 MST": cannot parse "+1245" as "MST" 

Timezone:    Pacific/Marquesas
zdump time:  Sun Aug 5 02:25:10 2018 -0930
time.Parse:  parsing time "Sun Aug 5 02:25:10 2018 -0930" as "Mon Jan 2 15:04:05 2006 MST": cannot parse "-0930" as "MST" 

Timezone:    Asia/Colombo
zdump time:  Sun Aug 5 17:25:10 2018 +0530
time.Parse:  parsing time "Sun Aug 5 17:25:10 2018 +0530" as "Mon Jan 2 15:04:05 2006 MST": cannot parse "+0530" as "MST" 

Timezone:    Asia/Yangon
zdump time:  Sun Aug 5 18:25:10 2018 +0630
time.Parse:  parsing time "Sun Aug 5 18:25:10 2018 +0630" as "Mon Jan 2 15:04:05 2006 MST": cannot parse "+0630" as "MST" 

Timezone:    Asia/Tehran
zdump time:  Sun Aug 5 16:25:10 2018 +0430
time.Parse:  parsing time "Sun Aug 5 16:25:10 2018 +0430" as "Mon Jan 2 15:04:05 2006 MST": cannot parse "+0430" as "MST" 

Failures:    10
z4bn682m

z4bn682m4#

我刚刚遇到了一些格式为 2019-08-22T10:41:59.006928+0000 的时间戳,我知道时间解析很复杂,但我相信上面的是符合ISO8601标准的。我相信下面的命令应该返回与 date 相同的时间,而且肯定不会出现解析失败:
| 格式 | 命令 | 结果 |
| ------------ | ------------ | ------------ |
| 2019-08-22T10:41:59.006928+0000 | date | Thu Aug 22 03:41:59 MST 2019 |
| 2019-08-22T10:41:59.006928+0000 | go | parsing time ""2019-08-22T10:41:59.006928+0000"" as ""2006-01-02T15:04:05Z07:00"": cannot parse "+0000"" as "Z07:00" |
| 2019-08-22T10:41:59.006928+00:00 | date | Thu Aug 22 03:41:59 MST 2019 |
| 2019-08-22T10:41:59.006928+00:00 | go | 2019-08-22 10:41:59.006928 +0000 +0000 |
| 2019-08-22T10:41:59.006928Z | date | Thu Aug 22 03:41:59 MST 2019 |
| 2019-08-22T10:41:59.006928Z | go | 2019-08-22 10:41:59.006928 +0000 UTC |
Go 1.8-1.12.9 的所有结果都相同。
编辑:对不起,我忘了提到它正在解析JSON。如果只是普通的字符串,我可以轻松解决这个问题。但是在没有将其 Package 在自定义类型的情况下修复JSON时间解析是没有干净的方法的,这使得处理数据结构变得不幸,因为你必须解包类型才能对其进行任何操作,或者在父类型中有多个步骤的 json.Unmarshal,这对于嵌套的出现并不容易扩展。话虽如此,我认为即使是一个简单的特殊情况,也值得在 json.Unmarshal 中修复它,即使它可以接受另一个 00 代替 :

4c8rllxm

4c8rllxm5#

@cstockton 似乎可以正常工作(如果你使用正确的解析格式):

package main

import (
	"fmt"
	"time"
)

func main() {
	tstamp := "2019-08-22T10:41:59.006928+0000"
	t, err := time.Parse("2006-01-02T15:04:05-0700", tstamp)
	if err != nil {
		panic(err)
	}
	fmt.Println(t.Format(time.UnixDate))
}

https://play.golang.org/p/RjhP0xB6AtX
我错过了什么?

r7knjye2

r7knjye26#

我正在使用JSON反序列化。

8dtrkrch

8dtrkrch7#

https://golang.org/cl/357450提到了这个问题:time: allow +0430,-0930 UTC offsets in Parse

相关问题