💻 Tech
When gofake generates date time, it has nanosecond precision because it’s go’s time.Time precision. The problem arises when a row is inserted into mongodb where it loses the precision because mongodb’s date type can only store up to millisecond precision. During tests, when dates are compared, they will fail. To solve this, use the Truncate function in Go.
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t)
fmt.Println(t.Truncate(time.Millisecond))
}
2009-11-10 23:00:00 +0000 UTC m=+0.000000001
2009-11-10 23:00:00 +0000 UTC