57 lines
1 KiB
Go
57 lines
1 KiB
Go
|
package jsons
|
||
|
|
||
|
import (
|
||
|
//"time"
|
||
|
)
|
||
|
|
||
|
// Implementing the interrface provides
|
||
|
// way to easily decode time and type safely
|
||
|
// specify how to decode field.
|
||
|
/*type TimeDecoder interface {
|
||
|
DecodeTime([]byte) (time.Time, error)
|
||
|
}
|
||
|
|
||
|
// Implementing the interface provides type safe
|
||
|
// way to specify how to encode and decode time.
|
||
|
type TimeEncoder interface {
|
||
|
EncodeTime(time.Time) ([]byte, error)
|
||
|
}
|
||
|
|
||
|
// The type provides fast way to define
|
||
|
// parsable time variations.
|
||
|
// Like `t jsons.Time[jsons.]`
|
||
|
type Time[D TimeDecoder, E TimeEncoder] struct {
|
||
|
time.Time
|
||
|
}
|
||
|
|
||
|
func (t *Time[D, E]) UnmarshalJSON(bts []byte) error {
|
||
|
var adec [1]D
|
||
|
dec := adec[0]
|
||
|
|
||
|
err, ret := dec.DecodeTime(bts)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
t.Time = ret
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (t *Time[D, E]) MarshalJSON() ([]byte, error) {
|
||
|
var aenc [1]E
|
||
|
enc := enc[0]
|
||
|
|
||
|
bts, err := enc.EncodeTime(t.Time)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return bts, err
|
||
|
}
|
||
|
|
||
|
type DatetimeLocal struct{}
|
||
|
func (dt DatetimeLocal) DecodeTime(bts []byte) (time.Time, error) {
|
||
|
return dt.Format("1986-01-28T11:38:00.010"), nil
|
||
|
}*/
|
||
|
|