dongzaotiao2863 2017-10-31 02:31
浏览 75
已采纳

Golang time.Parse()行为-000时下降精度

In Go, I am confused as to the behavior of time.Parse() I observe when parsing date strings to time.Time.

In the event that a given time string's milliseconds are equal to exactly "000", e.g. "2017-01-02T01:02:03.000Z", time.Parse() will truncate the "000".

It would seem preferable to include, as opposed to omit this for consistency in logging and other use cases.

Is this the expected behavior? If so, why?

Bonus: How do we retain the "000"?

Playground

package main

import (
    "log"
    "time"
)

func main() {

    const (
        ISO_8601 = "2006-01-02T15:04:05.999Z"
    )

    tMap := map[string]string{
        "t00": "2017-01-02T01:02:03.000Z",
        "t01": "2017-01-02T01:02:03.000Z",
        "t02": "2017-01-02T01:02:03.456Z",
        "t03": "2017-01-03T01:02:03.123Z",
        "t04": "2013-10-04T01:02:03.456Z",
        "t05": "2012-02-02T01:02:03.321Z",
        "t06": "2011-03-07T01:02:03.849Z",
    }

    for _, v := range tMap {
        t, err := time.Parse(ISO_8601, v)
        logFatal("0H N03Z!!1", err)

        // Dude where's my 000?
        log.Printf("Key: %s | lenTimeString: %d | Val: %s 
", t.String(), len(t.String()), t)
    }
}

func logFatal(hint string, e error) {
    if e != nil {
        log.Fatal(hint, e)
    }
}

Output:

2009/11/10 23:00:00 Key: t05 | lenTimeString: 33 | timeString: 2012-02-02 01:02:03.321 +0000 UTC 
2009/11/10 23:00:00 Key: t06 | lenTimeString: 33 | timeString: 2011-03-07 01:02:03.849 +0000 UTC 
2009/11/10 23:00:00 Key: t00 | lenTimeString: 29 | timeString: 2017-01-02 01:02:03 +0000 UTC 
2009/11/10 23:00:00 Key: t01 | lenTimeString: 29 | timeString: 2017-01-02 01:02:03 +0000 UTC 
2009/11/10 23:00:00 Key: t02 | lenTimeString: 33 | timeString: 2017-01-02 01:02:03.456 +0000 UTC 
2009/11/10 23:00:00 Key: t03 | lenTimeString: 33 | timeString: 2017-01-03 01:02:03.123 +0000 UTC 
2009/11/10 23:00:00 Key: t04 | lenTimeString: 33 | timeString: 2013-10-04 01:02:03.456 +0000 UTC 

Edit: Here's a simplified solution with the kind help of @zerkms

Play

package main

import (
    "fmt"
    "time"
)

func main() {

    p := fmt.Println

    const (
        ISO_8601           = "2006-01-02T15:04:05.000Z"
        ISO_8601_dropmilli = "2006-01-02T15:04:05.999Z"
    )

    t := time.Date(2017, time.October, 31, 15, 16, 17, 000000000, time.Local)

    p(t.Format(ISO_8601))
    p(t.Format(ISO_8601_dropmilli))
}

Output:

2017-10-31T15:16:17.000Z
2017-10-31T15:16:17Z
  • 写回答

1条回答 默认 最新

  • dongxiong5546 2017-10-31 02:51
    关注

    time.String uses the 2006-01-02 15:04:05.999999999 -0700 MST format.

    To keep the zeroes use 0 instead of 9: 2006-01-02 15:04:05.000000000 -0700 MST

    t.Format("2006-01-02 15:04:05.000 -0700 MST")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式