0
点赞
收藏
分享

微信扫一扫

golang自定义time格式输出

前端王祖蓝 2022-06-20 阅读 53
package jsontest

import (
"encoding/json"
"testing"
"time"
)

type Time time.Time

const (
timeFormart = "2006-01-02 15:04:05"
)

func (t *Time) UnmarshalJSON(data []byte) (err error) {
now, err := time.ParseInLocation(`"`+timeFormart+`"`, string(data), time.Local)
*t = Time(now)
return
}

func (t Time) MarshalJSON() ([]byte, error) {
b := make([]byte, 0, len(timeFormart)+2)
b = append(b, '"')
b = time.Time(t).AppendFormat(b, timeFormart)
b = append(b, '"')
return b, nil
}

func (t Time) String() string {
return time.Time(t).Format(timeFormart)
}

type Person struct {
Id int64 `json:"id"`
Name string `json:"name"`
Birthday Time `json:"birthday"`
}

func TestTimeJson(t *testing.T) {
now := Time(time.Now())
t.Log(now)
src := `{"id":5,"name":"xiaoming","birthday":"2016-06-30 16:09:51"}`
p := new(Person)
err := json.Unmarshal([]byte(src), p)
if err != nil {
t.Fatal(err)
}
t.Log(p)
t.Log(time.Time(p.Birthday))
js, _ := json.Marshal(p)
t.Log(string(js))
}


举报

相关推荐

0 条评论