time.parse:为什么golang不正确地解析时间? [英] time.parse : why does golang parses the time incorrectly?

查看:802
本文介绍了time.parse:为什么golang不正确地解析时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一个字符串,但不幸的是,它会出错月(一月,而不是六月)

I'm trying to parse a string as time with but unfortunately go gets the wrong month ( January instead of June)

package main

import "fmt"
import "time"

func main() {

    t := "2014-06-23T20:29:39.688+01:00"
    tc, _ := time.Parse("2006-01-02T15:04:05.000+01:00", t)

    fmt.Printf("t was %v and tc was %v", t, tc)

}

Play

推荐答案

问题在于你的时区偏移在布局中定义不明确:引用偏移量是 -0700 。您将您定义为 +01:00 ,因此 01 被解释为月份并删除之前定义的月份。因为你的工作偏移量也是 01 ,所以它被解析为一月份。

The problem is that your timezone offset is ill-defined in the layout: the reference offset is -0700. You defined yours as +01:00, so the 01 is interpreted as the month and erase the previously defined one. And as your working offset is 01 as well, it is parsed as january.

下面的例子适用于我操场

package main

import "fmt"
import "time"

func main() {

    t := "2014-06-23T20:29:39.688+01:00"
    tc, _ := time.Parse("2006-01-02T15:04:05.000-07:00", t)

    fmt.Printf("t was %v and tc was %v", t, tc)

}

这篇关于time.parse:为什么golang不正确地解析时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆