-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sqlite3 doesn't have datetime/timestamp types #748
Comments
You are correct that SQLite doesn't really support timestamps as such. This library follows a best effort approach for dealing with them, but it is by no means perfect. When reading values out of the database, if the declared type of the column is Lines 2005 to 2022 in 4396a38
Lines 2040 to 2064 in 4396a38
When binding parameters to a query, if the parameter is a Lines 1815 to 1817 in 4396a38
|
When reading CoreData sqlite databases a lot of apps tend to write the CFAbsoluteTime which results in a TIMESTAMP field that cannot be converted into a time.Time correctly. The contents being "626839883.818818" for example. Source: https://www.epochconverter.com/coredata This has left me in a position where I don't seem to be able to get the value out of the DB correctly.
This results in mixed values for the string, and failure if you use time.Time
Do you have any suggestion on how I can get an consistant integer or float out of these databases with go-sqlite3 ? |
@tebruno99 Here is one possible solution to your problem. First, you will want to explicitly cast the column in question to coreEpoch := time.Date(2001, time.January, 1, 0, 0, 0, 0, time.UTC)
rows, err := db.Query(`SELECT EntryID, CAST(Timestamp AS FLOAT) FROM Entries`)
if err != nil {
...
}
defer rows.Close()
for rows.Next() {
var id uint32
var coreTimestamp float64
if err := rows.Scan(&id, &coreTimestamp); err != nil {
...
}
timestamp := coreEpoch.Add(time.Duration(coreTimestamp * float64(time.Second)))
fmt.Printf("%d: %s\n", id, timestamp)
}
if err := rows.Err(); err != nil {
...
} Output from your sample data:
|
@rittneje Hey Thanks for the reply! This was the first thing I thought I tried but I didn't CAST in the query when I was trying to figure it out. Thanks for the tip! |
It sure would be cool if it could try and detect what it was trying to scan into (passing in a float) to determine what type to convert instead of a mix of column type hints. |
Unfortunately, this is an artifact of Go's driver API. The driver implementation does not get to see what you are trying to scan into. Instead, the driver is supposed to read out a row of data, and the sql package itself does the conversion. This, combined with SQLite's lack of a first-class timestamp type, leads to these sorts of discrepancies. https://golang.org/pkg/database/sql/driver/#Rows |
#### Summary Related to koltyakov/cq-source-sharepoint#19 (comment). SQLite doesn't have an official `timestamp` type but the Go lib takes care of it, see mattn/go-sqlite3#748. We had this type in `v1`, see https://github.com/cloudquery/cloudquery/blob/plugins-destination-sqlite-v1.3.6/plugins/destination/sqlite/client/types.go#L58 <!--
https://github.com/mattn/go-sqlite3/blob/master/doc.go
https://www.sqlite.org/draft/datatype3.html
...
2.2. Date and Time Datatype
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
...
The text was updated successfully, but these errors were encountered: