Add support for int.int unix timestamps

This commit is contained in:
Nise Void 2018-12-21 12:23:13 +01:00
parent 2e7870df5d
commit 0c0edfe2b8
Signed by: NiseVoid
GPG Key ID: FBA14AC83EA602F3
5 changed files with 41 additions and 4 deletions

View File

@ -99,7 +99,7 @@ func InsertSets(sets Sets) {
) )
} }
err := db.Exec(q) _, err := db.Exec(q)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -43,5 +43,5 @@ func (b jsonBinder) Bind(i interface{}, c echo.Context) error {
if err == io.ErrUnexpectedEOF || err == io.EOF { if err == io.ErrUnexpectedEOF || err == io.EOF {
return jsonError{`Unexpected EOF`} return jsonError{`Unexpected EOF`}
} }
return jsonError{`Unknown error occurred while parsing JSON`} return jsonError{`Unknown error occurred while parsing JSON. ` + err.Error()}
} }

View File

@ -50,6 +50,8 @@ func readData(conn *websocket.Conn) {
msg := message{} msg := message{}
for { for {
conn.SetReadDeadline(time.Now().Add(time.Minute))
nn, err := conn.Read(b[n:]) nn, err := conn.Read(b[n:])
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -2,6 +2,7 @@ package shared
import ( import (
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -28,9 +29,28 @@ func (t *UnixTimestamp) UnmarshalText(b []byte) error {
// UnmarshalJSON implements json.Unmarshaler // UnmarshalJSON implements json.Unmarshaler
func (t *UnixTimestamp) UnmarshalJSON(b []byte) error { func (t *UnixTimestamp) UnmarshalJSON(b []byte) error {
parts := strings.Split(string(b), `.`)
if len(parts) == 1 {
return t.UnmarshalText(b) 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 { 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`)
} }

15
shared/type_test.go Normal file
View File

@ -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)
}