You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When parsing a timestamp with which contains milliseconds, now.Parse will always display them as zeros.
Consider the following program which compares golang time library parsing to now:
package main
import (
"fmt""time""github.com/jinzhu/now"
)
funcmain(){
// Get current locationloc, err:=time.LoadLocation("America/Indianapolis")
iferr!=nil{
fmt.Println("Could not get location")
panic(err)
}
timeString:="04/20/2017 23:01:54.364"layout:="01/02/2006 15:04:05.000"// Parse using golang parse function and convert to UTCt1, err:=time.ParseInLocation(layout, timeString, loc)
iferr!=nil{
fmt.Printf("Could not parse t1, %s", err)
}
t1UTC:=t1.UTC().Format("01/02/2006 03:04:05.000-0700")
// Using Now library// Step 1: add layout to now.Timestamps as first argument to ensure that the now// library will use that onenow.TimeFormats=append([]string{layout}, now.TimeFormats...)
// Step 2: parse using now.Parset2, err:=now.Parse(timeString)
iferr!=nil{
fmt.Printf("Could not parse t2, %s", err)
}
// Step 3: conver to UTCt2UTC:=t2.UTC().Format("01/02/2006 03:04:05.000-0700")
fmt.Println("Original time (EST):\t", timeString)
fmt.Println("Parsed time using Golang (UTC):\t", t1UTC)
fmt.Println("Parsed time using Now (UTC):\t", t2UTC)
}
Output:
Original time (EST): 04/20/2017 23:01:54.364
Parsed time using Golang (UTC): 04/21/2017 03:01:54.364+0000
Parsed time using Now (UTC): 04/21/2017 03:01:54.000+0000
The last line of the output (parsed using now) shows an incorrect timestemp
The text was updated successfully, but these errors were encountered:
When parsing a timestamp with which contains milliseconds,
now.Parse
will always display them as zeros.Consider the following program which compares golang
time
library parsing tonow
:Output:
The last line of the output (parsed using
now
) shows an incorrect timestempThe text was updated successfully, but these errors were encountered: