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