improve handling of time stamp fields
This commit is contained in:
parent
9f17a14ea9
commit
372e254619
3 changed files with 18 additions and 22 deletions
|
@ -18,8 +18,6 @@ func Template(name, src string) *template.Template {
|
|||
t, err := t.Parse(src)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
//logger.LogErr(err, info, fmt.Sprintf("%+v", t))
|
||||
//return nil
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
@ -28,7 +26,7 @@ func BuildSql(t *template.Template, data lib.Map) string {
|
|||
var out strings.Builder
|
||||
err := t.Execute(&out, data)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.Error(err).Msg("storage.template.BuildSql")
|
||||
return ""
|
||||
}
|
||||
return out.String()
|
||||
|
|
|
@ -5,7 +5,6 @@ package tracking
|
|||
import (
|
||||
sqllib "database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
|
@ -18,6 +17,7 @@ type ItemFactory func() Track
|
|||
|
||||
type Track interface {
|
||||
TrackId() lib.Ident
|
||||
TimeStamp() *time.Time
|
||||
}
|
||||
|
||||
type Container interface {
|
||||
|
@ -42,6 +42,10 @@ func (tr *track) TrackId() lib.Ident {
|
|||
return tr.trackId
|
||||
}
|
||||
|
||||
func (tr *track) TimeStamp() *time.Time {
|
||||
return tr.timeStamp
|
||||
}
|
||||
|
||||
// basic container implementation
|
||||
|
||||
type container struct {
|
||||
|
@ -87,26 +91,20 @@ func (cont *container) insert(tr *track) lib.Ident {
|
|||
"columns": columns,
|
||||
}
|
||||
sql := storage.BuildSql(SqlInsert, data)
|
||||
type rt struct {
|
||||
trid lib.Ident
|
||||
ts string
|
||||
//ts *time.Time
|
||||
}
|
||||
var res []rt
|
||||
var trid lib.Ident
|
||||
var ts *time.Time
|
||||
var tsstr string
|
||||
proc := func(r *sqllib.Rows) error {
|
||||
var rec rt
|
||||
err := r.Scan(&rec.trid, &rec.ts)
|
||||
res = append(res, rec)
|
||||
err := r.Scan(&trid, &ts)
|
||||
if err != nil {
|
||||
err = r.Scan(&trid, &tsstr)
|
||||
ts = ParseDateTime(tsstr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
if err := db.Query(proc, sql, values...); err == nil {
|
||||
row := res[0]
|
||||
trid := row.trid
|
||||
tr.trackId = trid
|
||||
if ts := ParseDateTime(row.ts); ts != nil {
|
||||
fmt.Printf("%+v\n", ts)
|
||||
tr.timeStamp = ts
|
||||
}
|
||||
tr.container = cont
|
||||
return trid
|
||||
}
|
||||
|
@ -114,11 +112,9 @@ func (cont *container) insert(tr *track) lib.Ident {
|
|||
}
|
||||
|
||||
func ParseDateTime(inp string) *time.Time {
|
||||
if ts, err := time.Parse(time.RFC3339, inp); err == nil {
|
||||
return &ts
|
||||
}
|
||||
ts, err := time.Parse("2006-01-02 15:04:05", inp)
|
||||
if err == nil {
|
||||
//fmt.Printf("%+v\n", ts)
|
||||
return &ts
|
||||
}
|
||||
log.Error(err).Msg("storage.tracking.ParseDateTime")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package scopes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
tbase "testing"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
|
@ -70,6 +71,7 @@ func TrackingTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
|||
cont.CreateTable()
|
||||
track := cont.New(lib.StrSlice{"t01", "john"}, lib.Map{})
|
||||
t.AssertEqual(track.TrackId(), lib.Ident(1))
|
||||
fmt.Printf("%+v\n", track.TimeStamp())
|
||||
}
|
||||
|
||||
func MessageTest(t *testing.T, cfg *sql.Cfg, db *sql.Storage) {
|
||||
|
|
Loading…
Add table
Reference in a new issue