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

36 lines
674 B

3 years ago
  1. package grmi
  2. import (
  3. "fmt"
  4. json "github.com/json-iterator/go"
  5. "time"
  6. )
  7. type Date time.Time
  8. func (self Date) MarshalJSON() ([]byte, error) {
  9. return []byte(fmt.Sprintf("\"%s\"", time.Time(self).Format(URLDateFormat))), nil
  10. }
  11. func (self *Date) UnmarshalJSON(data []byte) error {
  12. var jsonString string
  13. err := json.Unmarshal(data, &jsonString)
  14. if err != nil {
  15. return err
  16. }
  17. result, err := time.Parse(URLDateFormat, jsonString)
  18. if err != nil {
  19. return err
  20. }
  21. *self = Date(result)
  22. return nil
  23. }
  24. func (self *Date) Restore() time.Time {
  25. return time.Time(*self)
  26. }
  27. func (self *Date) ToString() string {
  28. date := self.Restore()
  29. return date.Format(DateOutFormat)
  30. }