gin将响应的时间格式化 YYYY-MM-DD HH:MM:SS

gin将响应的时间格式化 YYYY-MM-DD HH:MM:SS

  • 创建model/time_type.go
go 复制代码
package model

import (
	"database/sql/driver"
	"encoding/json"
	"fmt"
	"time"
)

// LocalTime 自定义时间类型(格式化为 YYYY-MM-DD HH:MM:SS)
type LocalTime time.Time

// 常量:时间格式化模板
const timeFormat = "2006-01-02 15:04:05"

// MarshalJSON 实现 JSON 序列化接口(返回格式化后的字符串)
func (t LocalTime) MarshalJSON() ([]byte, error) {
	// 将自定义时间类型转为 time.Time
	tt := time.Time(t)
	if tt.IsZero() {
		return []byte("null"), nil // 空时间返回 null
	}
	// 格式化时间为字符串并包裹双引号(JSON 字符串格式)
	return json.Marshal(tt.Format(timeFormat))
}

// UnmarshalJSON 实现 JSON 反序列化接口(接收格式化字符串转为时间)
func (t *LocalTime) UnmarshalJSON(data []byte) error {
	// 空值处理
	if string(data) == "null" {
		*t = LocalTime(time.Time{})
		return nil
	}
	// 解析字符串为时间
	tt, err := time.Parse(`"`+timeFormat+`"`, string(data))
	if err != nil {
		return err
	}
	*t = LocalTime(tt)
	return nil
}

// Value 实现 driver.Valuer 接口(数据库存储时的格式)
func (t LocalTime) Value() (driver.Value, error) {
	tt := time.Time(t)
	if tt.IsZero() {
		return nil, nil
	}
	return tt.Format(timeFormat), nil
}

// Scan 实现 sql.Scanner 接口(从数据库读取时解析为 LocalTime)
func (t *LocalTime) Scan(v interface{}) error {
	if v == nil {
		*t = LocalTime(time.Time{})
		return nil
	}
	switch v.(type) {
	case time.Time:
		*t = LocalTime(v.(time.Time))
	case []byte:
		tt, err := time.Parse(timeFormat, string(v.([]byte)))
		if err != nil {
			return err
		}
		*t = LocalTime(tt)
	case string:
		tt, err := time.Parse(timeFormat, v.(string))
		if err != nil {
			return err
		}
		*t = LocalTime(tt)
	default:
		return fmt.Errorf("不支持的时间类型:%T", v)
	}
	return nil
}

// String 实现 Stringer 接口(打印时返回格式化字符串)
func (t LocalTime) String() string {
	return time.Time(t).Format(timeFormat)
}
  • 修改create_atcreate_time 等时间字段的类型
go 复制代码
package model

import (
	"gorm.io/gorm"
)

type User struct {
	ID        uint64         `gorm:"primarykey;comment:用户ID" json:"id"`
	CreatedAt LocalTime      `gorm:"comment:创建时间" json:"created_at"`
	UpdatedAt LocalTime      `gorm:"comment:更新时间" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"deleted_at"` // 软删除
}
相关推荐
计算机安禾20 小时前
【数据库系统原理】第19篇:计算机存储层次结构与数据库文件的物理组织
数据库·oracle
JAVA面经实录91720 小时前
操作系统面试题
java·服务器·数据库·计算机网络·面试
摇滚侠20 小时前
mariadb-libs 被 mysql-community-libs-5.7.28-1.el7.x86_64 取代
数据库·mysql·mariadb
DIY源码阁21 小时前
JavaSwing饮品管理系统 - MySQL版
java·数据库·mysql·eclipse
专注搞钱21 小时前
GPT-4o写设备Recipe:从3小时到10分钟
数据库·人工智能·gpt·半导体
东风破1371 天前
达梦数据库实战:备份恢复与数据迁移全攻略(实例初始化、服务注册、路径迁移)
数据库·chrome
SelectDB技术团队1 天前
2026 SelectDB AI 产品发布会:Agent Native 数据基础设施能力全景发布
数据库·人工智能·agent·apache doris·selectdb
爱吃羊的老虎1 天前
【数据库】模块一:数据库基础与关系代数
数据库
dishugj1 天前
iSCSI + Multipath + ASM:Oracle RAC 共享存储技术链详解
数据库·oracle
yoothey1 天前
MySQL事务机制解析 - 面试高分知识点
数据库·mysql·面试