From 0c0edfe2b80e6d0035f067bd2fd5366612dad30d Mon Sep 17 00:00:00 2001 From: NiseVoid Date: Fri, 21 Dec 2018 12:23:13 +0100 Subject: [PATCH] Add support for int.int unix timestamps --- data/data.go | 2 +- envitron/bind.go | 2 +- fortoprt/fetch.go | 2 ++ shared/type.go | 24 ++++++++++++++++++++++-- shared/type_test.go | 15 +++++++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 shared/type_test.go diff --git a/data/data.go b/data/data.go index 1ebbb10..dfccdba 100644 --- a/data/data.go +++ b/data/data.go @@ -99,7 +99,7 @@ func InsertSets(sets Sets) { ) } - err := db.Exec(q) + _, err := db.Exec(q) if err != nil { panic(err) } diff --git a/envitron/bind.go b/envitron/bind.go index 1752c4d..1da4f1e 100644 --- a/envitron/bind.go +++ b/envitron/bind.go @@ -43,5 +43,5 @@ func (b jsonBinder) Bind(i interface{}, c echo.Context) error { if err == io.ErrUnexpectedEOF || err == io.EOF { return jsonError{`Unexpected EOF`} } - return jsonError{`Unknown error occurred while parsing JSON`} + return jsonError{`Unknown error occurred while parsing JSON. ` + err.Error()} } diff --git a/fortoprt/fetch.go b/fortoprt/fetch.go index a25264a..a873a05 100644 --- a/fortoprt/fetch.go +++ b/fortoprt/fetch.go @@ -50,6 +50,8 @@ func readData(conn *websocket.Conn) { msg := message{} for { + conn.SetReadDeadline(time.Now().Add(time.Minute)) + nn, err := conn.Read(b[n:]) if err != nil { panic(err) diff --git a/shared/type.go b/shared/type.go index 534d7fc..2d44f60 100644 --- a/shared/type.go +++ b/shared/type.go @@ -2,6 +2,7 @@ package shared import ( "strconv" + "strings" "time" ) @@ -28,9 +29,28 @@ func (t *UnixTimestamp) UnmarshalText(b []byte) error { // UnmarshalJSON implements json.Unmarshaler func (t *UnixTimestamp) UnmarshalJSON(b []byte) error { - return t.UnmarshalText(b) + parts := strings.Split(string(b), `.`) + if len(parts) == 1 { + return t.UnmarshalText(b) + } + + i, err := strconv.ParseInt(parts[0], 10, 64) + if err != nil { + return err + } + + inano, err := strconv.ParseInt(parts[1], 10, 64) + if err != nil { + return err + } + + var tt time.Time + tt = time.Unix(i, inano*100) + + *t = UnixTimestamp(tt) + return nil } func (t UnixTimestamp) String() string { - return time.Time(t).Format(`2006-01-02 15:04`) + return time.Time(t).Format(`2006-01-02 15:04:05.999999`) } diff --git a/shared/type_test.go b/shared/type_test.go new file mode 100644 index 0000000..79e914d --- /dev/null +++ b/shared/type_test.go @@ -0,0 +1,15 @@ +package shared + +import ( + "fmt" + "testing" +) + +func TestUnixTimestampJSONNano(t *testing.T) { + var ut UnixTimestamp + ut.UnmarshalJSON([]byte(`1545197984`)) + fmt.Println(ut) + + ut.UnmarshalJSON([]byte(`1545197984.5691502`)) + fmt.Println(ut) +}