Golang时间解析在Playground上无法正常工作。

huangapple go评论81阅读模式
英文:

Golang time parsing does not work properly while working on playground

问题

我需要解析一个由伪随机日期时间生成器生成的日期时间字符串,该生成器使用Unix()函数。生成的日期时间在2010年至2017年之间采用各种日期和时间对。

问题是,这个代码版本在我的计算机和playground上都可以工作:

package main

import (
	"fmt"
	"time"
)

func main() {
	a := time.Date(2016, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
	kk := time.Unix(a, 0)
	t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", kk.String())
	if err != nil {
		panic(err)
	}
	fmt.Println(t)
}

但是下面的版本在playground上可以工作,但在我的计算机上会引发错误:

package main

import (
	"fmt"
	"time"
)

func main() {
	a := time.Date(2018, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
	kk := time.Unix(a, 0)
	t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", kk.String())
	if err != nil {
		panic(err)
	}
	fmt.Println(t)
}

错误信息如下:

panic: parsing time "2017-12-31 03:00:00 +0300 +03" as "2006-01-02 15:04:05.999999999 -0700 MST": cannot parse "+03" as "MST"

我使用的Go版本是go1.8.3 darwin/amd64

有什么想法吗?

英文:

I need to parse a datetime string that is generated by a pseudo random datetime generator that uses Unix() function. The generated datetime takes various date and time pairs between 2010 and 2017.
Here's the problem, this version of the code works on both my computer and playground,

package main

import (
	"fmt"
	"time"
)

func main() {
	a := time.Date(2016, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
	kk := time.Unix(a, 0)
	t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", kk.String())
	if err != nil {
		panic(err)
	}
	fmt.Println(t)
}

But the version below works on playground and panics on my computer,

package main

import (
	"fmt"
	"time"
)

func main() {
	a := time.Date(2018, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
	kk := time.Unix(a, 0)
	t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", kk.String())
	if err != nil {
		panic(err)
	}
	fmt.Println(t)
}

Here's the error,

panic: parsing time "2017-12-31 03:00:00 +0300 +03" as "2006-01-02 15:04:05.999999999 -0700 MST": cannot parse "+03" as "MST"

The Go version I use is go1.8.3 darwin/amd64.

Any ideas?

答案1

得分: 0

这里的问题与Europe/Istanbul的缩写更改有关。

> 土耳其从EET/EEST(+02/+03)切换到永久+03,从2016-09-07开始生效。(感谢Burak AYDIN。)使用“+03”而不是新时间的发明缩写。

Time包目前不接受+03作为EET/EEST,当使用包含+03作为时区的日期时间字符串进行解析(格式为2006-01-02 15:04:05.999999999 -0700 MST)时,就会出现这个问题。

英文:

The problem here is related with the abbreviation change for Europe/Istanbul.

> Turkey switched from EET/EEST (+02/+03) to permanent +03,
effective 2016-09-07. (Thanks to Burak AYDIN.) Use "+03" rather
than an invented abbreviation for the new time.

Time package currently does not accept +03 as EET/EEST and this problem occurs when using a datetime string that contains +03 as timezone to parse in form of 2006-01-02 15:04:05.999999999 -0700 MST.

huangapple
  • 本文由 发表于 2017年6月27日 21:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/44781327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定