You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

types.go 912 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package serder
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. )
  7. type TimestampSecond time.Time
  8. func (t *TimestampSecond) MarshalJSON() ([]byte, error) {
  9. raw := time.Time(*t)
  10. return []byte(fmt.Sprintf("%d", raw.Unix())), nil
  11. }
  12. func (t *TimestampSecond) UnmarshalJSON(data []byte) error {
  13. var timestamp int64
  14. var err error
  15. if timestamp, err = strconv.ParseInt(string(data), 10, 64); err != nil {
  16. return err
  17. }
  18. *t = TimestampSecond(time.Unix(timestamp, 0))
  19. return nil
  20. }
  21. type TimestampMilliSecond time.Time
  22. func (t *TimestampMilliSecond) MarshalJSON() ([]byte, error) {
  23. raw := time.Time(*t)
  24. return []byte(fmt.Sprintf("%d", raw.UnixMilli())), nil
  25. }
  26. func (t *TimestampMilliSecond) UnmarshalJSON(data []byte) error {
  27. var timestamp int64
  28. var err error
  29. if timestamp, err = strconv.ParseInt(string(data), 10, 64); err != nil {
  30. return err
  31. }
  32. *t = TimestampMilliSecond(time.UnixMilli(timestamp))
  33. return nil
  34. }