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.
31 lines
559 B
31 lines
559 B
package common
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
format = "2006-01-02"
|
|
)
|
|
|
|
type MyTime time.Time
|
|
|
|
func (t MyTime) MarshalJSON() ([]byte, error) {
|
|
b := make([]byte, 0, len(format)+2)
|
|
b = append(b, '"')
|
|
b = (time.Time(t)).AppendFormat(b, format)
|
|
b = append(b, '"')
|
|
return b, nil
|
|
}
|
|
|
|
func (t *MyTime) UnmarshalJSON(data []byte) (err error) {
|
|
if len(data) < 3 || strings.ToLower(string(data)) == "null" {
|
|
*t = MyTime(time.Time{})
|
|
return err
|
|
}
|
|
now, err := time.ParseInLocation(`"`+format+`"`, string(data), time.Local)
|
|
*t = MyTime(now)
|
|
return
|
|
}
|