Gorm格式化time.Time时间字段
Gorm 自带的 time.Time 类型 JSON 默认输出 RFC3339Nano 格式的,但是如果想改为 yyyy-MM-dd HH
形式的时间格式,需要定制 MarshalJSON 了。ss
这个问题一般用不到特殊时间字段的根本不会发现,我这边场景里需要用到接收前端传来的会议开始时间和结束时间,一般也都是 yyyy-MM-dd HH
,所以 time.Time 自然是转换不了,接下来一起重写下 Marshal 和 UnMarshal 时间吧。ss
再插一句,如果前端没有携带这两个字段,系统会自动给你塞一个 0000-00-00 00:00:00.000,这显然也不是我们想要的,没有我们为 null 就行了。
我们先声明了一个 LocalTime 的别名,然后绑定了函数 MarshalJSON 和 UnMarshalJSON,用于在序列化和反序列化时调用。
/*
@Author : Uyynot
@Email : uyynot@qq.com
@Time : 2023/10/12 11:08
@File : base.go
@Project : nunu_project
@Desc :
*/
package model
import (
"database/sql/driver"
"fmt"
"github.com/pkg/errors"
"strings"
"time"
)
// LocalTime起一个别名
type LocalTime time.Time
// 序列化时间格式
func (t *LocalTime) MarshalJSON() ([]byte, error) {
// LocalTime 转换成 time.Time 类型
tTime := time.Time(*t)
return []byte(fmt.Sprintf("\"%v\"", tTime.Format("2006-01-02 15:04:05"))), nil
}
// 反序列化时间处理
func (t *LocalTime) UnmarshalJSON(data []byte) error {
//去除接收的str收尾多余的"
timeStr := strings.Trim(string(data), "\"")
//转为当前服务器所在时区时间
t1, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
// time.Time转换为LocalTime类型
*t = LocalTime(t1)
if err != nil {
return errors.New("时间格式有误,转换失败")
}
return nil
}
func (t LocalTime) Value() (driver.Value, error) {
var zeroTime time.Time
tlt := time.Time(t)
//判断给定时间是否和默认零时间的时间戳相同
if tlt.UnixMicro() == zeroTime.UnixMicro() {
return nil, nil
}
return tlt, nil
}
func (t *LocalTime) Scan(v any) error {
if value, ok := v.(time.Time); ok {
*t = LocalTime(value)
return nil
}
return fmt.Errorf("不能转换 %V 为时间戳", v)
}
定义 model
type Metting struct {
gorm.Model
Title string `gorm:"not null" json:"title"`
Icon string `gorm:"not null" json:"icon"`
TimeStart LocalTime `json:"time_start"`
TimeEnd LocalTime `json:"time_end"`
}
以上我们响应结果就变为了
{
"ID": 10,
"CreatedAt": "2023-10-12T12:42:55.435+08:00",
"UpdatedAt": "2023-10-12T12:42:55.435+08:00",
"DeletedAt": null,
"title": "dddd",
"icon": "dddd",
"time_start": null,
"time_end": null
},
{
"ID": 11,
"CreatedAt": "2023-10-12T13:25:24.389+08:00",
"UpdatedAt": "2023-10-12T13:25:24.389+08:00",
"DeletedAt": null,
"title": "dddd",
"icon": "",
"time_start": null,
"time_end": "2023-02-02 15:11:22"
}
``````go
{
"ID": 10,
"CreatedAt": "2023-10-12T12:42:55.435+08:00",
"UpdatedAt": "2023-10-12T12:42:55.435+08:00",
"DeletedAt": null,
"title": "dddd",
"icon": "dddd",
"time_start": null,
"time_end": null
},
{
"ID": 11,
"CreatedAt": "2023-10-12T13:25:24.389+08:00",
"UpdatedAt": "2023-10-12T13:25:24.389+08:00",
"DeletedAt": null,
"title": "dddd",
"icon": "",
"time_start": null,
"time_end": "2023-02-02 15:11:22"
}
``````go
{
"ID": 10,
"CreatedAt": "2023-10-12T12:42:55.435+08:00",
"UpdatedAt": "2023-10-12T12:42:55.435+08:00",
"DeletedAt": null,
"title": "dddd",
"icon": "dddd",
"time_start": null,
"time_end": null
},
{
"ID": 11,
"CreatedAt": "2023-10-12T13:25:24.389+08:00",
"UpdatedAt": "2023-10-12T13:25:24.389+08:00",
"DeletedAt": null,
"title": "dddd",
"icon": "",
"time_start": null,
"time_end": "2023-02-02 15:11:22"
}
我们可以看到 gorm 自带的 CreatedAt,与我们自定义时间格式的区别。
同样看到数据表中的时间也是对的了
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: