1 Star 0 Fork 0

ichub / go-factroy2024

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
LocalTime.go 1.07 KB
一键复制 编辑 原始数据 按行查看 历史
leijmdas 提交于 2024-01-30 12:56 . add
package model
import (
"database/sql/driver"
"fmt"
"strings"
"time"
)
type LocalTime struct {
time.Time
}
func (t *LocalTime) MarshalJSON() ([]byte, error) {
//格式化秒seconds := t.Unix() return []byte(strconv.FormatInt(seconds, 10)), nil
tune := t.Format("20060102150405")
return []byte(tune), nil
}
func (t *LocalTime) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
//var err error
//前端接收的时间字符串
str := string(data)
//去除接收的str收尾多余的"
timeStr := strings.Trim(str, "\"")
t1, err := time.Parse("20060102150405", timeStr)
t.Time = t1
return err
}
func (t *LocalTime) Value() (driver.Value, error) {
var zeroTime time.Time
if t.Time.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return t.Time, nil
}
func (t *LocalTime) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = LocalTime{Time: value}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
func (t *LocalTime) FormatDatetime() string {
return t.Time.Format("2006-01-02 15:04:05")
}
Go
1
https://gitee.com/ichub/go-factroy2024.git
git@gitee.com:ichub/go-factroy2024.git
ichub
go-factroy2024
go-factroy2024
v1.2.0

搜索帮助