代码拉取完成,页面将自动刷新
package coreModel
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
)
// CustomDateTime 日期+时间
type CustomDateTime time.Time
var locTime *time.Location
func init() {
// 加载东八区时区
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
panic(err)
}
locTime = loc
}
func TimeNowTime() CustomDateTime {
return CustomDateTime(time.Now())
}
func TimeNowDate() CustomDate {
return CustomDate(time.Now())
}
func (c *CustomDateTime) Scan(value interface{}) error {
switch v := value.(type) {
case time.Time:
*c = CustomDateTime(v)
case []uint8:
parseTime, err := time.ParseInLocation("2006-01-02 15:04:05", string(v), locTime)
if err != nil {
return err
}
*c = CustomDateTime(parseTime)
case nil:
*c = CustomDateTime(time.Time{})
default:
return errors.New("database time wrong")
}
return nil
}
func (c CustomDateTime) Local() time.Time {
//判断时间是否为空
if c == (CustomDateTime{}) {
return time.Time{}
}
return time.Time(c).In(locTime).Local()
}
// IsEmpty 判断时间是否为空
func (c CustomDateTime) IsEmpty() bool {
return time.Time(c).In(locTime).IsZero()
}
func (c CustomDateTime) MarshalJSON() ([]byte, error) {
//判断时间是否为空
if c == (CustomDateTime{}) {
return []byte("null"), nil
}
formattedTime := time.Time(c).In(locTime).Format("2006-01-02 15:04:05")
return []byte(fmt.Sprintf(`"%s"`, formattedTime)), nil
}
func (c *CustomDateTime) UnmarshalJSON(data []byte) error {
var timeStr string
if err := json.Unmarshal(data, &timeStr); err != nil {
return err
}
if strings.TrimSpace(timeStr) != "" {
t, err := time.ParseInLocation(time.DateTime, timeStr, locTime)
if err != nil {
return fmt.Errorf("failed to parse CustomDateTime from JSON: %v", err)
}
*c = CustomDateTime(t)
} else {
*c = CustomDateTime{}
}
return nil
}
// Value 实现 Valuer 接口,将自定义类型转换为数据库值
func (c CustomDateTime) Value() (driver.Value, error) {
t := time.Time(c).In(locTime)
if t.IsZero() {
return nil, nil
}
return t, nil
}
// CustomDate 日期
type CustomDate time.Time
func (c *CustomDate) Scan(value interface{}) error {
switch v := value.(type) {
case time.Time:
*c = CustomDate(v)
case []uint8:
parseTime, err := time.ParseInLocation("2006-01-02", string(v), locTime)
if err != nil {
return err
}
*c = CustomDate(parseTime)
case nil:
*c = CustomDate(time.Time{})
default:
return errors.New("database time wrong")
}
return nil
}
func (c CustomDate) Local() time.Time {
return time.Time(c).In(locTime).Local()
}
func (c CustomDate) MarshalJSON() ([]byte, error) {
//判断时间是否为空
if c == (CustomDate{}) {
return []byte("null"), nil
}
formattedTime := time.Time(c).In(locTime).Format("2006-01-02")
return []byte(fmt.Sprintf(`"%s"`, formattedTime)), nil
}
func (c *CustomDate) UnmarshalJSON(data []byte) error {
var timeStr string
if err := json.Unmarshal(data, &timeStr); err != nil {
return err
}
if strings.TrimSpace(timeStr) != "" {
t, err := time.ParseInLocation(time.DateOnly, timeStr, locTime)
if err != nil {
return fmt.Errorf("failed to parse CustomDateTime from JSON: %v", err)
}
*c = CustomDate(t)
} else {
*c = CustomDate{}
}
return nil
}
// Value 实现 Valuer 接口,将自定义类型转换为数据库值
func (c CustomDate) Value() (driver.Value, error) {
t := time.Time(c).In(locTime)
if t.IsZero() {
return nil, nil
}
return t, nil
}
// IsEmpty 判断时间是否为空
func (c CustomDate) IsEmpty() bool {
return time.Time(c).In(locTime).IsZero()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。