代码拉取完成,页面将自动刷新
package common
import (
"encoding/json"
"errors"
"time"
)
// Layout to be used in the timestamp marshaling/unmarshaling everywhere.
// The timezone must always be UTC.
const TsLayout = "2006-01-02T15:04:05.000Z"
type Time time.Time
// MarshalJSON implements json.Marshaler interface.
// The time is a quoted string in the JsTsLayout format.
func (t Time) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Time(t).UTC().Format(TsLayout))
}
// UnmarshalJSON implements js.Unmarshaler interface.
// The time is expected to be a quoted string in TsLayout
// format.
func (t *Time) UnmarshalJSON(data []byte) (err error) {
if data[0] != []byte(`"`)[0] || data[len(data)-1] != []byte(`"`)[0] {
return errors.New("Not quoted")
}
*t, err = ParseTime(string(data[1 : len(data)-1]))
return
}
// ParseTime parses a time in the TsLayout format.
func ParseTime(timespec string) (Time, error) {
t, err := time.Parse(TsLayout, timespec)
return Time(t), err
}
// MustParseTime is a convenience equivalent of the ParseTime function
// that panics in case of errors.
func MustParseTime(timespec string) Time {
ts, err := ParseTime(timespec)
if err != nil {
panic(err)
}
return ts
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。