简体   繁体   English

time.Parse in Go 具有不同的小数秒长度

[英]time.Parse in Go with varying fractional second length

I am parsing a timestamp from a DB.我正在解析来自数据库的时间戳。 My layout is the following:我的布局如下:

layout = "2006-01-02 15:04:05.000000000 -0700 MST"

pipelineTS, err := time.Parse(layout, rawPipelineTS)

The issue is that sometimes the fractional seconds are not 9 digits, for example:问题是有时小数秒不是 9 位数字,例如:

2018-12-18 15:25:08.73728596 +0000 UTC  

When it finds a value like this, it errors out.当它找到这样的值时,它会出错。 Any ideas on how to fix this?有想法该怎么解决这个吗? The timestamps come from a DB table.时间戳来自数据库表。 I need it to take any number of fractional second digits.我需要它取任意数量的小数秒数。

Package time包车时间

A decimal point followed by one or more zeros represents a fractional second, printed to the given number of decimal places.小数点后跟一个或多个零表示小数秒,打印到给定的小数位数。 A decimal point followed by one or more nines represents a fractional second, printed to the given number of decimal places, with trailing zeros removed.小数点后跟一个或多个九表示小数秒,打印到给定的小数位数,并删除尾随零。


Use nines, not zeros, for the layout fractional seconds.为布局小数秒使用 nines,而不是 zeros。

For example,例如,

package main

import (
    "fmt"
    "time"
)

func main() {
    layout := "2006-01-02 15:04:05.999999999 -0700 MST"

    input := "2018-12-18 15:25:08.73728596 +0000 UTC" // 8 digits
    t, err := time.Parse(layout, input)
    fmt.Println(t, err)

    input = "2018-12-18 15:25:08.7372 +0000 UTC" // 4 digits
    t, err = time.Parse(layout, input)
    fmt.Println(t, err)

    input = "2018-12-18 15:25:08 +0000 UTC" // 0 digits
    t, err = time.Parse(layout, input)
    fmt.Println(t, err)
}

Playground: https://play.golang.org/p/j4WBmz3ENke游乐场:https: //play.golang.org/p/j4WBmz3ENke

Output:输出:

2018-12-18 15:25:08.73728596 +0000 UTC <nil>
2018-12-18 15:25:08.7372 +0000 UTC <nil>
2018-12-18 15:25:08 +0000 UTC <nil>

From the time docs:time文档:

A decimal point followed by one or more zeros represents a fractional second, printed to the given number of decimal places.小数点后跟一个或多个零表示小数秒,打印到给定的小数位数。 A decimal point followed by one or more nines represents a fractional second, printed to the given number of decimal places, with trailing zeros removed.小数点后跟一个或多个九表示小数秒,打印到给定的小数位数,并删除尾随零。 When parsing (only), the input may contain a fractional second field immediately after the seconds field, even if the layout does not signify its presence.解析时(仅),输入可能包含紧跟在秒字段之后的小数秒字段,即使布局并不表示它的存在。 In that case a decimal point followed by a maximal series of digits is parsed as a fractional second.在这种情况下,小数点后跟最大数字系列被解析为小数秒。

For time parsing:对于时间解析:

  1. If you don't specify fractional seconds in the layout, time.Parse will still accept it in the input, with full resolution (the "maximal series of digits").如果您未在布局中指定小数秒, time.Parse仍将在输入中接受它,并具有完整分辨率(“最大数字系列”)。 ( source ) 来源
  2. If you specify fractional seconds with one or more zeros, time.Parse requires the input to have that exact number of fractional digits.如果您指定带有一个或多个零的小数秒, time.Parse要求输入具有精确的小数位数。 ( source ) 来源
  3. If you specify fractional seconds with one or more nines, time.Parse actually accepts any number of fractional digits (up to 9).如果您用一个或多个 9 指定小数秒, time.Parse实际上接受任意数量的小数位(最多 9)。 That is, the behavior is the same as if you didn't specify fractional seconds at all (case #1).也就是说,行为与您根本没有指定小数秒相同(案例 #1)。 I think the docs are not clear on this point at all, but for parsing (not formatting), .9 , .99 , .999 , .999999 , etc. are all exactly equivalent.我认为文档在这一点上根本不清楚,但对于解析(不是格式化), .9.99.999.999999等都是完全等价的。 ( source ) 来源

Playground samples: https://play.golang.org/p/aXYiaMsCVuz游乐场示例:https: //play.golang.org/p/aXYiaMsCVuz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM