Private
Public Access
1
0
Files
Kordophone/mock/model/date.go

54 lines
1.1 KiB
Go
Raw Normal View History

2024-01-05 16:26:19 -08:00
package model
import (
"errors"
2024-01-05 16:26:19 -08:00
"fmt"
"time"
)
type Date time.Time
func (d Date) Equal(other Date) bool {
// 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)
}
func (d Date) MarshalJSON() ([]byte, error) {
2024-01-05 16:26:19 -08:00
// Must use ISO8601
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
}
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
}