feat: added jsons.Timestamp.

This commit is contained in:
Andrey Parhomenko 2024-01-18 21:35:58 +03:00
parent a9c2c88106
commit 6fa4ef4cf4
3 changed files with 34 additions and 2 deletions

View file

@ -81,6 +81,10 @@ Def(
c.SetStatus(statuses.OK)
}),
//),
).Def(
"auth",
bond.Func(func(c *bond.Context){
})
))
func main() {

View file

@ -3,10 +3,10 @@ package jsons
import (
"strconv"
"encoding/json"
//"errors"
"time"
)
// The package implements basic types
// to be used in structures that can be parsed
// from the poorly structured JSON. (Like ones that make IDs strings)
@ -86,3 +86,17 @@ func (jam *ArrayMap[V]) UnmarshalJSON(bts []byte) error {
return nil
}
type Timestamp int64
func (ts *Timestamp) UnmarshalJSON(bts []byte) error {
i, err := parseInt(string(bts), 64)
if err != nil {
return err
}
*ts = Timestamp(i)
return nil
}
func (ts Timestamp) Time() time.Time {
return time.Unix(int64(ts), 0)
}

14
jsons/timestamp_test.go Normal file
View file

@ -0,0 +1,14 @@
package jsons
//import "time"
import "testing"
func TestTimestampJson(t *testing.T) {
//wantStr := "Thu Jan 18 2024 17:33:01 GMT+0300"
gotStr := "1705588381"
got := Timestamp(0)
err := got.UnmarshalJSON([]byte(gotStr))
if err != nil {
t.Errorf("JSON unmarshalling error: %s", err)
}
}