Files
easygoadmin/app/model/localtime.go
yaoyilin 1b36bd8fbe feat: 初始化项目
初始化项目
2022-10-31 22:29:16 +08:00

125 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"database/sql/driver"
"fmt"
"time"
)
type LocalTime time.Time
func (l LocalTime) MarshalJSON() ([]byte, error) {
b := make([]byte, 0, len(localDateTimeFormat)+2)
b = append(b, '"')
b = time.Time(l).AppendFormat(b, localDateTimeFormat)
b = append(b, '"')
return b, nil
}
func (l *LocalTime) UnmarshalJSON(b []byte) error {
now, err := time.ParseInLocation(`"`+localDateTimeFormat+`"`, string(b), time.Local)
*l = LocalTime(now)
return err
}
func (l LocalTime) String() string {
return time.Time(l).Format(localDateTimeFormat)
}
func (l LocalTime) Now() LocalTime {
return LocalTime(time.Now())
}
func (l LocalTime) ParseTime(t time.Time) LocalTime {
return LocalTime(t)
}
func (j LocalTime) format() string {
return time.Time(j).Format(localDateTimeFormat)
}
func (j LocalTime) MarshalText() ([]byte, error) {
return []byte(j.format()), nil
}
func (l *LocalTime) FromDB(b []byte) error {
if nil == b || len(b) == 0 {
l = nil
return nil
}
var now time.Time
var err error
now, err = time.ParseInLocation(localDateTimeFormat, string(b), time.Local)
if nil == err {
*l = LocalTime(now)
return nil
}
now, err = time.ParseInLocation("2006-01-02T15:04:05Z", string(b), time.Local)
if nil == err {
*l = LocalTime(now)
return nil
}
return err
}
//func (t *LocalTime) Scan(v interface{}) error {
// // Should be more strictly to check this type.
// vt, err := time.Parse("2006-01-02 15:04:05", string(v.([]byte)))
// if err != nil {
// return err
// }
// *t = LocalTime(vt)
// return nil
//}
func (l *LocalTime) ToDB() ([]byte, error) {
if nil == l {
return nil, nil
}
return []byte(time.Time(*l).Format(localDateTimeFormat)), nil
}
func (l *LocalTime) Value() (driver.Value, error) {
if nil == l {
return nil, nil
}
return time.Time(*l).Format(localDateTimeFormat), nil
}
// XTime 1. 创建 time.Time 类型的副本 XTime
type XTime struct {
time.Time
}
// MarshalJSON 2. 为 Xtime 重写 MarshaJSON 方法,在此方法中实现自定义格式的转换;
func (t XTime) MarshalJSON() ([]byte, error) {
output := fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05"))
return []byte(output), nil
}
// Value 3. 为 Xtime 实现 Value 方法,写入数据库时会调用该方法将自定义时间类型转换并写入数据库;
func (t XTime) Value() (driver.Value, error) {
var zeroTime time.Time
if t.Time.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return t.Time, nil
}
// Scan 4. 为 Xtime 实现 Scan 方法,读取数据库时会调用该方法将时间数据转换成自定义时间类型;
func (t *XTime) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = XTime{Time: value}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
func TimeToXTime(expires string) *XTime {
var t, _ = time.ParseInLocation(localDateTimeFormat, expires, time.Local)
return &XTime{Time: t}
}