Add support for int.int unix timestamps
This commit is contained in:
parent
2e7870df5d
commit
0c0edfe2b8
@ -99,7 +99,7 @@ func InsertSets(sets Sets) {
|
||||
)
|
||||
}
|
||||
|
||||
err := db.Exec(q)
|
||||
_, err := db.Exec(q)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -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()}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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`)
|
||||
}
|
||||
|
15
shared/type_test.go
Normal file
15
shared/type_test.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user