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

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