Loggest thine 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.
 
 
 
 
 
 

74 lines
1.4 KiB

package models
import (
"encoding/json"
"fmt"
"time"
)
type Date [3]int
func (d Date) Before(other Date) bool {
if d[0] == other[0] {
if d[1] == other[1] {
return d[2] < other[2]
} else {
return d[1] < other[1]
}
} else {
return d[0] < other[0]
}
}
func (d Date) IsZero() bool {
return d == [3]int{0, 0, 0}
}
func (d *Date) AsTime() time.Time {
return time.Date(d[0], time.Month(d[1]), d[2], 0, 0, 0, 0, time.UTC)
}
func ParseDate(s string) (Date, error) {
date, err := time.ParseInLocation("2006-01-02", s, time.UTC)
if err != nil {
return [3]int{}, err
}
y, m, d := date.Date()
return Date{y, int(m), d}, nil
}
func (d Date) String() string {
return fmt.Sprintf("%04d-%02d-%02d", d[0], d[1], d[2])
}
func (d Date) ToTime() time.Time {
return time.Date(d[0], time.Month(d[1]), d[2], 0, 0, 0, 0, time.UTC)
}
func (d Date) WithTime(hh, mm, ss int) time.Time {
return d.WithTimeAndLocation(hh, mm, ss, time.UTC)
}
func (d Date) WithTimeAndLocation(hh, mm, ss int, loc *time.Location) time.Time {
return time.Date(d[0], time.Month(d[1]), d[2], hh, mm, ss, 0, loc)
}
func (d *Date) UnmarshalJSON(b []byte) error {
var str string
err := json.Unmarshal(b, &str)
if err != nil {
return err
}
*d, err = ParseDate(str)
if err != nil {
return err
}
return nil
}
func (d Date) MarshalJSON() ([]byte, error) {
return []byte("\"" + d.String() + "\""), nil
}