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 936 B

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