1 Star 0 Fork 0

bughou / go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
time.go 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
bughou 提交于 2022-03-20 18:50 . save
package time2
import (
"database/sql/driver"
"fmt"
"strings"
"time"
)
type Time struct {
time.Time
}
const (
timeFormat = "2006-01-02 15:04:05"
)
func Now() Time {
return Time{time.Now()}
}
func New(year, month, day, hour, minute, second int) Time {
return Time{time.Date(year, time.Month(month), day, hour, minute, second, 0, time.Local)}
}
func Parse(str string) (Time, error) {
str = strings.Trim(str, "\"")
if str == "" || str == "null" {
return Time{}, nil
}
t, err := time.Parse(timeFormat, str)
if err != nil {
return Time{}, err
}
return Time{t}, nil
}
func (t Time) String() string {
if t.Time.IsZero() {
return ""
}
return t.Format(timeFormat)
}
func (t Time) MarshalJSON() ([]byte, error) {
if t.Time.IsZero() {
return []byte("null"), nil
}
return []byte(`"` + t.Format(timeFormat) + `"`), nil
}
func (t *Time) UnmarshalJSON(b []byte) error {
d, err := Parse(string(b))
if err != nil {
return err
}
*t = d
return nil
}
func (t Time) Value() (driver.Value, error) {
if t.Time.IsZero() {
return []byte("NULL"), nil
}
return []byte(t.Format("'2006-01-02T15:04:05.999999Z07:00'")), nil
}
func (t *Time) Scan(value interface{}) error {
if value == nil {
*t = Time{}
return nil
}
v, ok := value.(time.Time)
if ok {
*t = Time{v}
return nil
}
return fmt.Errorf("can not convert %v to t.Time", value)
}
Go
1
https://gitee.com/bughou/go.git
git@gitee.com:bughou/go.git
bughou
go
go
d31700df43a9

搜索帮助