Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse a time in Go with no period separating seconds and milliseconds?

Tags:

go

I'm having to parse times that are in the format:

20060102150405000

E.g. "20170116111621978" represents 16 January 2017, 11:16:21.978. It looks to me like the Go time package requires an embedded period in the date format (05.000) (and hence the input). I can't think of any way short of rewriting the source string to include a period to parse these with time.Parse. Anyone have a suggestion?

like image 235
Scott Deerwester Avatar asked Oct 20 '25 13:10

Scott Deerwester


1 Answers

Unfortunately the time package requires a period prefix for the fraction seconds part (milliseconds) in the layout, so you have 2 choices:

  • either modify the input to insert a period, so you can use the layout 20060102150405.000
  • or cut off the last 3 digits, parse the prefix using the layout 20060102150405, parse the milliseconds (e.g. using strconv.Atoi()) and add it to the time using Time.Add().
like image 134
icza Avatar answered Oct 23 '25 08:10

icza