1 Star 2 Fork 0

xgh2012/api-core

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sql_time.go 3.55 KB
一键复制 编辑 原始数据 按行查看 历史
xgh2012 提交于 2025-10-22 16:24 +08:00 . 添加 sql相关封装功能
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()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xgh2012/api-core.git
git@gitee.com:xgh2012/api-core.git
xgh2012
api-core
api-core
v0.0.22

搜索帮助