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.

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

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记