39 lines
749 B
Go
39 lines
749 B
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
// Timestamps are stored as RFC3339Nano TEXT explicitly (rather than relying
|
|
// on driver-specific time.Time marshaling) so the on-disk format is stable
|
|
// and easy to inspect with the sqlite3 CLI.
|
|
|
|
const timeLayout = time.RFC3339Nano
|
|
|
|
func timeToDB(t time.Time) string {
|
|
return t.UTC().Format(timeLayout)
|
|
}
|
|
|
|
func timePtrToDB(t *time.Time) interface{} {
|
|
if t == nil {
|
|
return nil
|
|
}
|
|
return timeToDB(*t)
|
|
}
|
|
|
|
func dbToTime(s string) (time.Time, error) {
|
|
return time.Parse(timeLayout, s)
|
|
}
|
|
|
|
func nullStringToTimePtr(ns sql.NullString) (*time.Time, error) {
|
|
if !ns.Valid || ns.String == "" {
|
|
return nil, nil
|
|
}
|
|
t, err := dbToTime(ns.String)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &t, nil
|
|
}
|