go-sqlite icon indicating copy to clipboard operation
go-sqlite copied to clipboard

Helper functions for time.Time

Open delaneyj opened this issue 4 years ago • 4 comments

I'd be nice to be able to have Get/Set of time.Time directly. Versions for rfc3339, int64 unix sec && int64 unix milliseconds it what i tend to use most. Are you open to PRs?

delaneyj avatar Sep 16 '21 20:09 delaneyj

While I agree that storing times is a pretty common case (I use it a lot myself!), I don't want to introduce the surface area in this package. Reason being: SQLite notably doesn't have a time type, so the representation that applications use vary based on need. I would rather just expose the column access primitives, and then have applications transform the data into time.Time values.

I'm open to hearing concrete API proposals, but I don't want to raise hopes that I'm necessarily going to merge anything. After all, "no is temporary, yes is forever."

Notes:

zombiezen avatar Sep 18 '21 04:09 zombiezen

I want to argue but damn, you have thought it through. Maybe it could/should live at a sqlitex level, if at all.

delaneyj avatar Sep 18 '21 14:09 delaneyj

I agree that sqlitex would be the right place for it, if such a function should be added. The strawman I've been thinking of is:

package sqlitex

// ColumnTime returns a query result as a time in UTC. The time must be
// in one of the formats 1-7 from https://sqlite.org/lang_datefunc.html#time_values.
// That is, one of:
//
//   YYYY-MM-DD
//   YYYY-MM-DD HH:MM
//   YYYY-MM-DD HH:MM:SS
//   YYYY-MM-DD HH:MM:SS.SSS
//   YYYY-MM-DDTHH:MM
//   YYYY-MM-DDTHH:MM:SS
//   YYYY-MM-DDTHH:MM:SS.SSS
//
// Column indices start at 0.
func ColumnTime(stmt *sqlite.Stmt, col int) time.Time

// GetTime returns a query result value for colName as a time.
func GetTime(stmt *sqlite.Stmt, colName string) time.Time

The part where I'm stuck is that this implies for symmetry that there should be a BindTime, but since there is no completely ubiquitous format, I don't think providing a function is as clear as callers writing:

stmt.BindText(1, timeValue.Format("2006-01-02 15:04:05"))

zombiezen avatar Sep 18 '21 16:09 zombiezen

I was thinking of it being a little more complicated than that. So for example if you're using the Julian for float64 you can actually get down to the microsecond level if I recall correctly. The string version is easier give that saucing and loading as time.3339 works well already. I can totally add in my own version no problem it just seems like something that's very common at this level

delaneyj avatar Sep 20 '21 00:09 delaneyj