game/common/utils/marshal.go

30 lines
506 B
Go

package utils
import (
"encoding/json"
"fmt"
"time"
)
const (
timeFormat = "2006-01-02 15:04:05"
)
func Marshal(a any) string {
s, _ := json.Marshal(a)
return string(s)
}
type Time time.Time
func (t Time) MarshalJSON() ([]byte, error) {
var stamp = fmt.Sprintf("\"%s\"", time.Time(t).Format(timeFormat))
return []byte(stamp), nil
}
func (t *Time) UnmarshalJSON(data []byte) error {
now, err := time.ParseInLocation(`"`+timeFormat+`"`, string(data), time.Local)
*t = Time(now)
return err
}