// SimpleDateLayout represents the "year-month-day" Go time format.
constSimpleDateLayout="2006-01-02"
// SimpleDate holds a json "year-month-day" time.
typeSimpleDatetime.Time
// ParseSimpleDate reads from "s" and returns the SimpleDate time.
funcParseSimpleDate(sstring)(SimpleDate,error){
ifs==""||s=="null"{
returnSimpleDate{},nil
}
var(
tttime.Time
errerror
)
tt,err=time.Parse(SimpleDateLayout,s)
iferr!=nil{
returnSimpleDate{},err
}
returnSimpleDate(tt.UTC()),nil
}
// UnmarshalJSON binds the json "data" to "t" with the `SimpleDateLayout`.
func(t*SimpleDate)UnmarshalJSON(data[]byte)error{
ifisNull(data){
returnnil
}
data=trimQuotes(data)
dataStr:=string(data)
iflen(dataStr)==0{
returnnil// as an excepption here, allow empty "" on simple dates, as the server would render it on a response: https://endomedical.slack.com/archives/D02BF660JA1/p1630486704048100.
}
tt,err:=time.Parse(SimpleDateLayout,dataStr)
iferr!=nil{
returnerr
}
*t=SimpleDate(tt)
returnnil
}
// MarshalJSON returns the json representation of the "t".
func(tSimpleDate)MarshalJSON()([]byte,error){
ifs:=t.String();s!=""{
s=strconv.Quote(s)
return[]byte(s),nil
}
returnemptyQuoteBytes,nil
}
// IsZero reports whether "t" is zero time.
// It completes the pg.Zeroer interface.
func(tSimpleDate)IsZero()bool{
returnt.ToTime().IsZero()
}
// ToTime returns the standard time type.
func(tSimpleDate)ToTime()time.Time{
returntime.Time(t)
}
func(tSimpleDate)Value()(driver.Value,error){
returnt.String(),nil
}
// String returns the text representation of the date
// formatted based on the `SimpleDateLayout`.
// If date is zero it returns an empty string.
func(tSimpleDate)String()string{
tt:=t.ToTime()
iftt.IsZero(){
return""
}
returntt.Format(SimpleDateLayout)
}
// Scan completes the pg and native sql driver.Scanner interface