duanchai0028 2017-06-27 13:17
浏览 25
已采纳

在操场上工作时,Golang时间解析无法正常工作

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条回答 默认 最新

  • dongzuo4666 2017-07-29 15:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂