time.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package jsons
  2. import (
  3. //"time"
  4. )
  5. // Implementing the interrface provides
  6. // way to easily decode time and type safely
  7. // specify how to decode field.
  8. /*type TimeDecoder interface {
  9. DecodeTime([]byte) (time.Time, error)
  10. }
  11. // Implementing the interface provides type safe
  12. // way to specify how to encode and decode time.
  13. type TimeEncoder interface {
  14. EncodeTime(time.Time) ([]byte, error)
  15. }
  16. // The type provides fast way to define
  17. // parsable time variations.
  18. // Like `t jsons.Time[jsons.]`
  19. type Time[D TimeDecoder, E TimeEncoder] struct {
  20. time.Time
  21. }
  22. func (t *Time[D, E]) UnmarshalJSON(bts []byte) error {
  23. var adec [1]D
  24. dec := adec[0]
  25. err, ret := dec.DecodeTime(bts)
  26. if err != nil {
  27. return err
  28. }
  29. t.Time = ret
  30. return nil
  31. }
  32. func (t *Time[D, E]) MarshalJSON() ([]byte, error) {
  33. var aenc [1]E
  34. enc := enc[0]
  35. bts, err := enc.EncodeTime(t.Time)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return bts, err
  40. }
  41. type DatetimeLocal struct{}
  42. func (dt DatetimeLocal) DecodeTime(bts []byte) (time.Time, error) {
  43. return dt.Format("1986-01-28T11:38:00.010"), nil
  44. }*/