I am trying to convert timestamps from the local Chrome sqlite db to local time using Go. I know that these timestamps are meant to be microseconds from 1601/01/01.
Checking the values I'm getting for lastVisitTime in the following program on this Chrome timestamp conversion website, I appear to be retrieving them from the database correctly.
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/local_library/comp"
)
var (
dbPath = comp.Expanduser("~/Library/Application Support/Google/Chrome/Default/History")
chromeEpochStart = time.Date(1601, 1, 1, 0, 0, 0, 0, time.UTC)
)
const (
driverName = "sqlite3"
tmpPath = "/tmp/History"
query = `
SELECT
last_visit_time
FROM
urls
ORDER BY
last_visit_time DESC
LIMIT 5
`
)
func main() {
// Copy to tmp to unlock
err := comp.Copy(dbPath, tmpPath)
comp.MustBeNil(err)
db, err := sql.Open(driverName, tmpPath)
comp.MustBeNil(err)
rows, err := db.Query(query)
comp.MustBeNil(err)
for rows.Next() {
var lastVisitTime int64
rows.Scan(&lastVisitTime)
d := time.Duration(time.Microsecond * time.Duration(lastVisitTime))
t := chromeEpochStart.Add(d)
fmt.Println(t, lastVisitTime)
}
err = rows.Close()
comp.MustBeNil(err)
err = rows.Err()
comp.MustBeNil(err)
}
But for some reason, my .Add(d) is setting the times before 1601, which I have never seen before.
1439-07-05 20:00:21.462742384 +0000 UTC 13350512095172294
1439-07-05 19:58:20.377916384 +0000 UTC 13350511974087468
1439-07-05 19:57:58.539932384 +0000 UTC 13350511952249484
1439-07-05 19:57:48.539540384 +0000 UTC 13350511942249092
1439-07-05 19:52:09.587445384 +0000 UTC 13350511603296997
What's happening here and, more importantly, how do I do this correctly?