Loggest thy stuff
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.

46 lines
781 B

  1. package models
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. )
  7. type Date [3]int
  8. func ParseDate(s string) (Date, error) {
  9. date, err := time.ParseInLocation("2006-01-02", s, time.UTC)
  10. if err != nil {
  11. return [3]int{}, err
  12. }
  13. y, m, d := date.Date()
  14. return Date{y, int(m), d}, nil
  15. }
  16. func (d *Date) UnmarshalJSON(b []byte) error {
  17. var str string
  18. err := json.Unmarshal(b, &str)
  19. if err != nil {
  20. return err
  21. }
  22. *d, err = ParseDate(str)
  23. if err != nil {
  24. return err
  25. }
  26. return nil
  27. }
  28. func (d Date) String() string {
  29. return fmt.Sprintf("%04d-%02d-%02d", d[0], d[1], d[2])
  30. }
  31. func (d Date) MarshalJSON() ([]byte, error) {
  32. return []byte("\"" + d.String() + "\""), nil
  33. }
  34. func (d Date) ToTime() time.Time {
  35. return time.Date(d[0], time.Month(d[1]), d[2], 0, 0, 0, 0, time.UTC)
  36. }