From 0ac2a00a96a3719dcfc8db0d9c79d9cef0b60106 Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 12 Jul 2024 14:09:56 +0800 Subject: [PATCH] refactor: datetime --- README.md | 2 +- pkg/app/admin/model/action_log.go | 21 +++-- pkg/app/admin/model/admin.go | 32 ++++---- pkg/app/admin/model/config.go | 22 ++--- pkg/app/admin/model/file.go | 28 +++---- pkg/app/admin/model/menu.go | 34 ++++---- pkg/app/admin/model/permission.go | 16 ++-- pkg/app/admin/model/picture.go | 32 ++++---- pkg/app/admin/model/role.go | 10 +-- pkg/app/admin/service/logins/index.go | 2 +- pkg/app/miniapp/model/user.go | 36 ++++----- pkg/builder/engine.go | 2 +- pkg/utils/datetime/date.go | 6 +- pkg/utils/datetime/datetime.go | 112 ++++++++++++++++++++++++++ pkg/utils/datetime/format.go | 28 +++++-- pkg/utils/datetime/time.go | 25 ++---- 16 files changed, 263 insertions(+), 145 deletions(-) create mode 100644 pkg/utils/datetime/datetime.go diff --git a/README.md b/README.md index afe2bf7..3c0a72d 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ go run main.go 密码:```123456``` ## 特别注意 -1. **后台用户认证使用了AppKey作为JWT的加密密串,生成环境请务必更改** +1. **后台用户认证使用了AppKey作为JWT的加密密串,生产环境请务必更改** ## 相关项目 - [QuarkSmart](https://github.com/quarkcloudio/quark-smart) 单体应用 diff --git a/pkg/app/admin/model/action_log.go b/pkg/app/admin/model/action_log.go index f370750..8aa04a3 100644 --- a/pkg/app/admin/model/action_log.go +++ b/pkg/app/admin/model/action_log.go @@ -1,23 +1,22 @@ package model import ( - "github.com/quarkcloudio/quark-go/v2/pkg/dal/db" "github.com/quarkcloudio/quark-go/v2/pkg/utils/datetime" ) // 字段 type ActionLog struct { - Id int `json:"id" gorm:"autoIncrement"` - ObjectId int `json:"object_id" gorm:"size:11;not null"` - Username string `json:"username" gorm:"<-:false"` - Url string `json:"url" gorm:"size:500;not null"` - Remark string `json:"remark" gorm:"size:255;not null"` - Ip string `json:"ip" gorm:"size:100;not null"` - Type string `json:"type" gorm:"size:100;not null"` - Status int `json:"status" gorm:"size:1;not null;default:1"` - CreatedAt datetime.Time `json:"created_at"` - UpdatedAt datetime.Time `json:"updated_at"` + Id int `json:"id" gorm:"autoIncrement"` + ObjectId int `json:"object_id" gorm:"size:11;not null"` + Username string `json:"username" gorm:"<-:false"` + Url string `json:"url" gorm:"size:500;not null"` + Remark string `json:"remark" gorm:"size:255;not null"` + Ip string `json:"ip" gorm:"size:100;not null"` + Type string `json:"type" gorm:"size:100;not null"` + Status int `json:"status" gorm:"size:1;not null;default:1"` + CreatedAt datetime.Datetime `json:"created_at"` + UpdatedAt datetime.Datetime `json:"updated_at"` } // 插入数据 diff --git a/pkg/app/admin/model/admin.go b/pkg/app/admin/model/admin.go index 88b8fe7..fe16edd 100644 --- a/pkg/app/admin/model/admin.go +++ b/pkg/app/admin/model/admin.go @@ -13,20 +13,20 @@ import ( // 字段 type Admin struct { - Id int `json:"id" gorm:"autoIncrement"` - Username string `json:"username" gorm:"size:20;index:admins_username_unique,unique;not null"` - Nickname string `json:"nickname" gorm:"size:200;not null"` - Sex int `json:"sex" gorm:"size:4;not null;default:1"` - Email string `json:"email" gorm:"size:50;index:admins_email_unique,unique;not null"` - Phone string `json:"phone" gorm:"size:11;index:admins_phone_unique,unique;not null"` - Password string `json:"password" gorm:"size:255;not null"` - Avatar string `json:"avatar" gorm:"size:1000"` - LastLoginIp string `json:"last_login_ip" gorm:"size:255"` - LastLoginTime datetime.Time `json:"last_login_time"` - Status int `json:"status" gorm:"size:1;not null;default:1"` - CreatedAt datetime.Time `json:"created_at"` - UpdatedAt datetime.Time `json:"updated_at"` - DeletedAt gorm.DeletedAt `json:"deleted_at"` + Id int `json:"id" gorm:"autoIncrement"` + Username string `json:"username" gorm:"size:20;index:admins_username_unique,unique;not null"` + Nickname string `json:"nickname" gorm:"size:200;not null"` + Sex int `json:"sex" gorm:"size:4;not null;default:1"` + Email string `json:"email" gorm:"size:50;index:admins_email_unique,unique;not null"` + Phone string `json:"phone" gorm:"size:11;index:admins_phone_unique,unique;not null"` + Password string `json:"password" gorm:"size:255;not null"` + Avatar string `json:"avatar" gorm:"size:1000"` + LastLoginIp string `json:"last_login_ip" gorm:"size:255"` + LastLoginTime datetime.Datetime `json:"last_login_time"` + Status int `json:"status" gorm:"size:1;not null;default:1"` + CreatedAt datetime.Datetime `json:"created_at"` + UpdatedAt datetime.Datetime `json:"updated_at"` + DeletedAt gorm.DeletedAt `json:"deleted_at"` } // 管理员JWT结构体 @@ -45,7 +45,7 @@ type AdminClaims struct { // 管理员Seeder func (model *Admin) Seeder() { seeders := []Admin{ - {Username: "administrator", Nickname: "超级管理员", Email: "admin@yourweb.com", Phone: "10086", Password: hash.Make("123456"), Sex: 1, Status: 1, LastLoginTime: datetime.TimeNow()}, + {Username: "administrator", Nickname: "超级管理员", Email: "admin@yourweb.com", Phone: "10086", Password: hash.Make("123456"), Sex: 1, Status: 1, LastLoginTime: datetime.Now()}, } db.Client.Create(&seeders) @@ -124,7 +124,7 @@ func (model *Admin) GetMenuListById(id interface{}) (menuList interface{}, Error } // 更新最后一次登录数据 -func (model *Admin) UpdateLastLogin(uid int, lastLoginIp string, lastLoginTime datetime.Time) error { +func (model *Admin) UpdateLastLogin(uid int, lastLoginIp string, lastLoginTime datetime.Datetime) error { data := Admin{ LastLoginIp: lastLoginIp, LastLoginTime: lastLoginTime, diff --git a/pkg/app/admin/model/config.go b/pkg/app/admin/model/config.go index 06e92ac..0443314 100644 --- a/pkg/app/admin/model/config.go +++ b/pkg/app/admin/model/config.go @@ -7,17 +7,17 @@ import ( // 字段 type Config struct { - Id int `json:"id" gorm:"autoIncrement"` - Title string `json:"title" gorm:"size:255;not null"` - Type string `json:"type" gorm:"size:20;not null"` - Name string `json:"name" gorm:"size:255;not null"` - Sort int `json:"sort" gorm:"size:11;default:0"` - GroupName string `json:"group_name" gorm:"size:255;not null"` - Value string `json:"value" gorm:"size:2000"` - Remark string `json:"remark" gorm:"size:100;not null"` - Status int `json:"status" gorm:"size:1;not null;default:1"` - CreatedAt datetime.Time `json:"created_at"` - UpdatedAt datetime.Time `json:"updated_at"` + Id int `json:"id" gorm:"autoIncrement"` + Title string `json:"title" gorm:"size:255;not null"` + Type string `json:"type" gorm:"size:20;not null"` + Name string `json:"name" gorm:"size:255;not null"` + Sort int `json:"sort" gorm:"size:11;default:0"` + GroupName string `json:"group_name" gorm:"size:255;not null"` + Value string `json:"value" gorm:"size:2000"` + Remark string `json:"remark" gorm:"size:100;not null"` + Status int `json:"status" gorm:"size:1;not null;default:1"` + CreatedAt datetime.Datetime `json:"created_at"` + UpdatedAt datetime.Datetime `json:"updated_at"` } // 存储配置 diff --git a/pkg/app/admin/model/file.go b/pkg/app/admin/model/file.go index d32ec50..34f8fd0 100644 --- a/pkg/app/admin/model/file.go +++ b/pkg/app/admin/model/file.go @@ -13,20 +13,20 @@ import ( // 字段 type File struct { - Id int `json:"id" gorm:"autoIncrement"` - ObjType string `json:"obj_type" gorm:"size:255"` - ObjId int `json:"obj_id" gorm:"size:11;default:0"` - FileCategoryId int `json:"file_category_id" gorm:"size:11;default:0"` - Sort int `json:"sort" gorm:"size:11;default:0"` - Name string `json:"name" gorm:"size:255;not null"` - Size int64 `json:"size" gorm:"size:20;default:0"` - Ext string `json:"ext" gorm:"size:255"` - Path string `json:"path" gorm:"size:255;not null"` - Url string `json:"url" gorm:"size:255;not null"` - Hash string `json:"hash" gorm:"size:255;not null"` - Status int `json:"status" gorm:"size:1;not null;default:1"` - CreatedAt datetime.Time `json:"created_at"` - UpdatedAt datetime.Time `json:"updated_at"` + Id int `json:"id" gorm:"autoIncrement"` + ObjType string `json:"obj_type" gorm:"size:255"` + ObjId int `json:"obj_id" gorm:"size:11;default:0"` + FileCategoryId int `json:"file_category_id" gorm:"size:11;default:0"` + Sort int `json:"sort" gorm:"size:11;default:0"` + Name string `json:"name" gorm:"size:255;not null"` + Size int64 `json:"size" gorm:"size:20;default:0"` + Ext string `json:"ext" gorm:"size:255"` + Path string `json:"path" gorm:"size:255;not null"` + Url string `json:"url" gorm:"size:255;not null"` + Hash string `json:"hash" gorm:"size:255;not null"` + Status int `json:"status" gorm:"size:1;not null;default:1"` + CreatedAt datetime.Datetime `json:"created_at"` + UpdatedAt datetime.Datetime `json:"updated_at"` } // 插入数据并返回ID diff --git a/pkg/app/admin/model/menu.go b/pkg/app/admin/model/menu.go index 22ae02a..c5bc132 100644 --- a/pkg/app/admin/model/menu.go +++ b/pkg/app/admin/model/menu.go @@ -14,23 +14,23 @@ import ( // 字段 type Menu struct { - Id int `json:"id" gorm:"autoIncrement"` - Name string `json:"name" gorm:"size:100;not null"` - GuardName string `json:"group_name" gorm:"size:100;not null"` - Icon string `json:"icon" gorm:"size:100;"` - Type int `json:"type" gorm:"size:100;not null"` // 菜单类型:1目录,2菜单,3按钮 - Pid int `json:"pid" gorm:"size:11;default:0"` - Sort int `json:"sort" gorm:"size:11;default:0"` - Path string `json:"path" gorm:"size:255"` - Show int `json:"show" gorm:"size:1;not null;default:1"` - IsEngine int `json:"is_engine" gorm:"size:1;not null;default:0"` - IsLink int `json:"is_link" gorm:"size:1;not null;default:0"` - Status int `json:"status" gorm:"size:1;not null;default:1"` - Key string `json:"key" gorm:"<-:false"` - Locale string `json:"locale" gorm:"<-:false"` - HideInMenu bool `json:"hide_in_menu" gorm:"<-:false"` - CreatedAt datetime.Time `json:"created_at"` - UpdatedAt datetime.Time `json:"updated_at"` + Id int `json:"id" gorm:"autoIncrement"` + Name string `json:"name" gorm:"size:100;not null"` + GuardName string `json:"group_name" gorm:"size:100;not null"` + Icon string `json:"icon" gorm:"size:100;"` + Type int `json:"type" gorm:"size:100;not null"` // 菜单类型:1目录,2菜单,3按钮 + Pid int `json:"pid" gorm:"size:11;default:0"` + Sort int `json:"sort" gorm:"size:11;default:0"` + Path string `json:"path" gorm:"size:255"` + Show int `json:"show" gorm:"size:1;not null;default:1"` + IsEngine int `json:"is_engine" gorm:"size:1;not null;default:0"` + IsLink int `json:"is_link" gorm:"size:1;not null;default:0"` + Status int `json:"status" gorm:"size:1;not null;default:1"` + Key string `json:"key" gorm:"<-:false"` + Locale string `json:"locale" gorm:"<-:false"` + HideInMenu bool `json:"hide_in_menu" gorm:"<-:false"` + CreatedAt datetime.Datetime `json:"created_at"` + UpdatedAt datetime.Datetime `json:"updated_at"` } // 菜单表 diff --git a/pkg/app/admin/model/permission.go b/pkg/app/admin/model/permission.go index ae78f80..db52ef1 100644 --- a/pkg/app/admin/model/permission.go +++ b/pkg/app/admin/model/permission.go @@ -9,14 +9,14 @@ import ( // 权限 type Permission struct { - Id int `json:"id" gorm:"autoIncrement"` - Name string `json:"name" gorm:"size:500;not null"` - GuardName string `json:"group_name" gorm:"size:100;not null"` - Path string `json:"path" gorm:"size:500;not null"` - Method string `json:"method" gorm:"size:500;not null"` - Remark string `json:"remark" gorm:"size:100"` - CreatedAt datetime.Time `json:"created_at"` - UpdatedAt datetime.Time `json:"updated_at"` + Id int `json:"id" gorm:"autoIncrement"` + Name string `json:"name" gorm:"size:500;not null"` + GuardName string `json:"group_name" gorm:"size:100;not null"` + Path string `json:"path" gorm:"size:500;not null"` + Method string `json:"method" gorm:"size:500;not null"` + Remark string `json:"remark" gorm:"size:100"` + CreatedAt datetime.Datetime `json:"created_at"` + UpdatedAt datetime.Datetime `json:"updated_at"` } // 获取列表 diff --git a/pkg/app/admin/model/picture.go b/pkg/app/admin/model/picture.go index 3f0f7c9..b399852 100644 --- a/pkg/app/admin/model/picture.go +++ b/pkg/app/admin/model/picture.go @@ -12,22 +12,22 @@ import ( // 字段 type Picture struct { - Id int `json:"id" gorm:"autoIncrement"` - ObjType string `json:"obj_type" gorm:"size:255"` - ObjId int `json:"obj_id" gorm:"size:11;default:0"` - PictureCategoryId int `json:"picture_category_id" gorm:"size:11;default:0"` - Sort int `json:"sort" gorm:"size:11;default:0"` - Name string `json:"name" gorm:"size:255;not null"` - Size int64 `json:"size" gorm:"size:20;default:0"` - Width int `json:"width" gorm:"size:11;default:0"` - Height int `json:"height" gorm:"size:11;default:0"` - Ext string `json:"ext" gorm:"size:255"` - Path string `json:"path" gorm:"size:255;not null"` - Url string `json:"url" gorm:"size:255;not null"` - Hash string `json:"hash" gorm:"size:255;not null"` - Status int `json:"status" gorm:"size:1;not null;default:1"` - CreatedAt datetime.Time `json:"created_at"` - UpdatedAt datetime.Time `json:"updated_at"` + Id int `json:"id" gorm:"autoIncrement"` + ObjType string `json:"obj_type" gorm:"size:255"` + ObjId int `json:"obj_id" gorm:"size:11;default:0"` + PictureCategoryId int `json:"picture_category_id" gorm:"size:11;default:0"` + Sort int `json:"sort" gorm:"size:11;default:0"` + Name string `json:"name" gorm:"size:255;not null"` + Size int64 `json:"size" gorm:"size:20;default:0"` + Width int `json:"width" gorm:"size:11;default:0"` + Height int `json:"height" gorm:"size:11;default:0"` + Ext string `json:"ext" gorm:"size:255"` + Path string `json:"path" gorm:"size:255;not null"` + Url string `json:"url" gorm:"size:255;not null"` + Hash string `json:"hash" gorm:"size:255;not null"` + Status int `json:"status" gorm:"size:1;not null;default:1"` + CreatedAt datetime.Datetime `json:"created_at"` + UpdatedAt datetime.Datetime `json:"updated_at"` } // 获取列表 diff --git a/pkg/app/admin/model/role.go b/pkg/app/admin/model/role.go index cd1de56..bcc6de2 100644 --- a/pkg/app/admin/model/role.go +++ b/pkg/app/admin/model/role.go @@ -8,11 +8,11 @@ import ( // 角色 type Role struct { - Id int `json:"id" gorm:"autoIncrement"` - Name string `json:"name" gorm:"size:255;not null"` - GuardName string `json:"guard_name" gorm:"size:100;not null"` - CreatedAt datetime.Time `json:"created_at"` - UpdatedAt datetime.Time `json:"updated_at"` + Id int `json:"id" gorm:"autoIncrement"` + Name string `json:"name" gorm:"size:255;not null"` + GuardName string `json:"guard_name" gorm:"size:100;not null"` + CreatedAt datetime.Datetime `json:"created_at"` + UpdatedAt datetime.Datetime `json:"updated_at"` } // 获取角色列表 diff --git a/pkg/app/admin/service/logins/index.go b/pkg/app/admin/service/logins/index.go index fefb247..34cb19d 100644 --- a/pkg/app/admin/service/logins/index.go +++ b/pkg/app/admin/service/logins/index.go @@ -123,7 +123,7 @@ func (p *Index) Handle(ctx *builder.Context) error { } // 更新登录信息 - (&model.Admin{}).UpdateLastLogin(adminInfo.Id, ctx.ClientIP(), datetime.TimeNow()) + (&model.Admin{}).UpdateLastLogin(adminInfo.Id, ctx.ClientIP(), datetime.Now()) // 获取token字符串 tokenString, err := ctx.JwtToken((&model.Admin{}).GetClaims(adminInfo)) diff --git a/pkg/app/miniapp/model/user.go b/pkg/app/miniapp/model/user.go index 4e60e5f..a4b6ef6 100644 --- a/pkg/app/miniapp/model/user.go +++ b/pkg/app/miniapp/model/user.go @@ -14,22 +14,22 @@ import ( // 字段 type User struct { - Id int `json:"id" gorm:"autoIncrement"` - Username string `json:"username" gorm:"size:20;index:users_username_unique,unique;not null"` - Nickname string `json:"nickname" gorm:"size:200;not null"` - Sex int `json:"sex" gorm:"size:4;not null;default:1"` - Email string `json:"email" gorm:"size:50;index:users_email_unique,unique;not null"` - Phone string `json:"phone" gorm:"size:11;index:users_phone_unique,unique;not null"` - Password string `json:"password" gorm:"size:255;not null"` - Avatar string `json:"avatar" gorm:"size:1000"` - LastLoginIp string `json:"last_login_ip" gorm:"size:255"` - LastLoginTime datetime.Time `json:"last_login_time" gorm:"default:null"` - WxOpenid string `json:"wx_openid" gorm:"size:255"` - WxUnionid string `json:"wx_unionid" gorm:"size:255"` - Status int `json:"status" gorm:"size:1;not null;default:1"` - CreatedAt datetime.Time `json:"created_at"` - UpdatedAt datetime.Time `json:"updated_at"` - DeletedAt gorm.DeletedAt `json:"deleted_at"` + Id int `json:"id" gorm:"autoIncrement"` + Username string `json:"username" gorm:"size:20;index:users_username_unique,unique;not null"` + Nickname string `json:"nickname" gorm:"size:200;not null"` + Sex int `json:"sex" gorm:"size:4;not null;default:1"` + Email string `json:"email" gorm:"size:50;index:users_email_unique,unique;not null"` + Phone string `json:"phone" gorm:"size:11;index:users_phone_unique,unique;not null"` + Password string `json:"password" gorm:"size:255;not null"` + Avatar string `json:"avatar" gorm:"size:1000"` + LastLoginIp string `json:"last_login_ip" gorm:"size:255"` + LastLoginTime datetime.Datetime `json:"last_login_time" gorm:"default:null"` + WxOpenid string `json:"wx_openid" gorm:"size:255"` + WxUnionid string `json:"wx_unionid" gorm:"size:255"` + Status int `json:"status" gorm:"size:1;not null;default:1"` + CreatedAt datetime.Datetime `json:"created_at"` + UpdatedAt datetime.Datetime `json:"updated_at"` + DeletedAt gorm.DeletedAt `json:"deleted_at"` } // 用户JWT结构体 @@ -61,7 +61,7 @@ func (model *User) Seeder() { db.Client.Create(&menuSeeders) seeders := []User{ - {Username: "tangtanglove", Nickname: "默认用户", Email: "tangtanglove@yourweb.com", Phone: "10086", Password: hash.Make("123456"), Sex: 1, Status: 1, LastLoginTime: datetime.TimeNow()}, + {Username: "tangtanglove", Nickname: "默认用户", Email: "tangtanglove@yourweb.com", Phone: "10086", Password: hash.Make("123456"), Sex: 1, Status: 1, LastLoginTime: datetime.Now()}, } db.Client.Create(&seeders) @@ -134,7 +134,7 @@ func (model *User) GetInfoByUsername(username string) (User *User, Error error) } // 更新最后一次登录数据 -func (model *User) UpdateLastLogin(uid int, lastLoginIp string, lastLoginTime datetime.Time) error { +func (model *User) UpdateLastLogin(uid int, lastLoginIp string, lastLoginTime datetime.Datetime) error { data := User{ LastLoginIp: lastLoginIp, LastLoginTime: lastLoginTime, diff --git a/pkg/builder/engine.go b/pkg/builder/engine.go index c36e449..28f14f8 100644 --- a/pkg/builder/engine.go +++ b/pkg/builder/engine.go @@ -22,7 +22,7 @@ const ( AppName = "QuarkGo" // 版本号 - Version = "2.4.2" + Version = "2.4.3" // 包名 PkgName = "github.com/quarkcloudio/quark-go/v2" diff --git a/pkg/utils/datetime/date.go b/pkg/utils/datetime/date.go index 0f12c83..065e5d1 100644 --- a/pkg/utils/datetime/date.go +++ b/pkg/utils/datetime/date.go @@ -76,9 +76,9 @@ func (t *Date) Scan(i interface{}) error { return errors.New("无法将值转换为时间戳") } -// 将Date类型转换为Time类型 -func (t Date) ToTime() Time { - return Time{ +// 将Date类型转换为DateTime类型 +func (t Date) ToDateTime() Datetime { + return Datetime{ Time: t.Time, } } diff --git a/pkg/utils/datetime/datetime.go b/pkg/utils/datetime/datetime.go new file mode 100644 index 0000000..547501a --- /dev/null +++ b/pkg/utils/datetime/datetime.go @@ -0,0 +1,112 @@ +package datetime + +import ( + "database/sql/driver" + "errors" + "time" +) + +// 日期时间 +type Datetime struct { + time.Time +} + +// 当前日期时间 +func Now() Datetime { + return Datetime{ + Time: time.Now(), + } +} + +// 编码为自定义的Json格式 +func (t Datetime) MarshalJSON() ([]byte, error) { + + // 时间为零返回null + if t.IsZero() { + return []byte("null"), nil + } + + return []byte("\"" + t.Format("2006-01-02 15:04:05") + "\""), nil +} + +// 将Json格式解码 +func (t *Datetime) UnmarshalJSON(data []byte) error { + + var err error + + if len(data) == 2 || string(data) == "null" { + return err + } + + var now Datetime + + // 自定义格式解析 + if now, err = ParseDatetime(string(data), "2006-01-02 15:04:05"); err == nil { + *t = now + return err + } + + // 带引号的自定义格式解析 + if now, err = ParseDatetime(string(data), "\"2006-01-02 15:04:05\""); err == nil { + *t = now + return err + } + + // 默认格式解析 + if now, err = ParseDatetime(string(data), time.RFC3339); err == nil { + *t = now + return err + } + + if now, err = ParseDatetime(string(data), "\""+time.RFC3339+"\""); err == nil { + *t = now + return err + } + + return err +} + +// 转换为数据库值 +func (t Datetime) Value() (driver.Value, error) { + + if t.IsZero() { + return nil, nil + } + + return t.Time, nil +} + +// 数据库值转换为Time +func (t *Datetime) Scan(i interface{}) error { + + if value, ok := i.(time.Time); ok { + *t = Datetime{Time: value} + return nil + } + + return errors.New("无法将值转换为时间戳") +} + +// 将Datetime类型转换为Date类型 +func (t Datetime) ToDate() Date { + return Date{ + Time: t.Time, + } +} + +// 将Datetime类型转换为Time类型 +func (t Datetime) ToTime() Time { + return Time{ + Time: t.Time, + } +} + +// 格式化为字符串 +func (t Datetime) ToString() string { + return t.Format("2006-01-02 15:04:05") +} + +// 自定义格式字符串 +func (t Datetime) FormatToString(format string) string { + return t.Format(format) +} diff --git a/pkg/utils/datetime/format.go b/pkg/utils/datetime/format.go index 4604d36..32ab8d7 100644 --- a/pkg/utils/datetime/format.go +++ b/pkg/utils/datetime/format.go @@ -24,21 +24,39 @@ func ParseDateInLocation(location *time.Location, values ...string) (Date, error } // 将字符串解析为时间 -// ParseTime("2024-05-20 11:22:33") | ParseTime("2024/05/20 11:22:33", "2006/01/02 15:04:05") +// ParseTime("14:00:00") | ParseTime("14-00-00", "15-04-05") func ParseTime(values ...string) (Time, error) { - time, err := parseInLocation("2006-01-02 15:04:05", time.Local, values...) + date, err := parseInLocation("15:04:05", time.Local, values...) - return Time{Time: time}, err + return Time{Time: date}, err } // 将字符串解析为时间 -// ParseTimeInLocation(time.Local, "2024-05-20 11:22:33") | ParseTimeInLocation(time.Local, "2024/05/20 11:22:33", "2006/01/02 15:04:05") +// ParseTimeInLocation(time.Local, "14:00:00") | ParseTimeInLocation(time.Local, "14-00-00", "15-04-05") func ParseTimeInLocation(location *time.Location, values ...string) (Time, error) { + date, err := parseInLocation("15:04:05", location, values...) + + return Time{Time: date}, err +} + +// 将字符串解析为日期时间 +// ParseDatetime("2024-05-20 11:22:33") | ParseDatetime("2024/05/20 11:22:33", "2006/01/02 15:04:05") +func ParseDatetime(values ...string) (Datetime, error) { + + time, err := parseInLocation("2006-01-02 15:04:05", time.Local, values...) + + return Datetime{Time: time}, err +} + +// 将字符串解析为时间 +// ParseDatetimeInLocation(time.Local, "2024-05-20 11:22:33") | ParseDatetimeInLocation(time.Local, "2024/05/20 11:22:33", "2006/01/02 15:04:05") +func ParseDatetimeInLocation(location *time.Location, values ...string) (Datetime, error) { + time, err := parseInLocation("2006-01-02 15:04:05", location, values...) - return Time{Time: time}, err + return Datetime{Time: time}, err } // 用于解析带有可选自定义格式的日期或时间字符串 diff --git a/pkg/utils/datetime/time.go b/pkg/utils/datetime/time.go index b91d33e..f2651d3 100644 --- a/pkg/utils/datetime/time.go +++ b/pkg/utils/datetime/time.go @@ -26,7 +26,7 @@ func (t Time) MarshalJSON() ([]byte, error) { return []byte("null"), nil } - return []byte("\"" + t.Format("2006-01-02 15:04:05") + "\""), nil + return []byte("\"" + t.Format("15:04:05") + "\""), nil } // 将Json格式解码 @@ -41,24 +41,13 @@ func (t *Time) UnmarshalJSON(data []byte) error { var now Time // 自定义格式解析 - if now, err = ParseTime(string(data), "2006-01-02 15:04:05"); err == nil { + if now, err = ParseTime(string(data), "15:04:05"); err == nil { *t = now return err } // 带引号的自定义格式解析 - if now, err = ParseTime(string(data), "\"2006-01-02 15:04:05\""); err == nil { - *t = now - return err - } - - // 默认格式解析 - if now, err = ParseTime(string(data), time.RFC3339); err == nil { - *t = now - return err - } - - if now, err = ParseTime(string(data), "\""+time.RFC3339+"\""); err == nil { + if now, err = ParseTime(string(data), "\"15:04:05\""); err == nil { *t = now return err } @@ -87,16 +76,16 @@ func (t *Time) Scan(i interface{}) error { return errors.New("无法将值转换为时间戳") } -// 将Time类型转换为Date类型 -func (t Time) ToDate() Date { - return Date{ +// 将Time类型转换为Datetime类型 +func (t Time) ToDatetime() Datetime { + return Datetime{ Time: t.Time, } } // 格式化为字符串 func (t Time) ToString() string { - return t.Format("2006-01-02 15:04:05") + return t.Format("15:04:05") } // 自定义格式字符串