2024-01-05 16:26:19 -08:00
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
2024-03-01 23:01:57 -08:00
|
|
|
"errors"
|
2024-01-05 16:26:19 -08:00
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Date time.Time
|
|
|
|
|
|
|
|
|
|
func (d Date) Equal(other Date) bool {
|
2024-03-01 23:01:57 -08:00
|
|
|
// usec and nsec are lost in ISO8601 conversion
|
|
|
|
|
dr := time.Time(d).Round(time.Minute)
|
|
|
|
|
or := time.Time(other).Round(time.Minute)
|
|
|
|
|
|
|
|
|
|
return dr.Equal(or)
|
2024-01-05 16:26:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d Date) After(other Date) bool {
|
|
|
|
|
return time.Time(d).After(time.Time(other))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d Date) Before(other Date) bool {
|
|
|
|
|
return time.Time(d).Before(time.Time(other))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d Date) Format(layout string) string {
|
|
|
|
|
return time.Time(d).Format(layout)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 23:01:57 -08:00
|
|
|
func (d Date) MarshalJSON() ([]byte, error) {
|
2024-01-05 16:26:19 -08:00
|
|
|
// Must use ISO8601
|
2024-03-01 23:01:57 -08:00
|
|
|
formatted := fmt.Sprintf("\"%s\"", time.Time(d).Format("2006-01-02T15:04:05+00:00"))
|
2024-01-05 16:26:19 -08:00
|
|
|
return []byte(formatted), nil
|
|
|
|
|
}
|
2024-03-01 23:01:57 -08:00
|
|
|
|
|
|
|
|
func (d *Date) UnmarshalJSON(data []byte) error {
|
|
|
|
|
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
|
|
|
|
|
return errors.New("Time.UnmarshalJSON: input is not a JSON string")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = data[len(`"`) : len(data)-len(`"`)]
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
var t time.Time
|
|
|
|
|
t, err = time.ParseInLocation("2006-01-02T15:04:05+00:00", string(data), time.Now().Location())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*d = Date(t)
|
|
|
|
|
return nil
|
|
|
|
|
}
|