mirror of
https://github.com/quarkcloudio/quark-go.git
synced 2025-09-26 20:11:11 +08:00
refactor: datetime
This commit is contained in:
@@ -85,7 +85,7 @@ go run main.go
|
||||
密码:```123456```
|
||||
|
||||
## 特别注意
|
||||
1. **后台用户认证使用了AppKey作为JWT的加密密串,生成环境请务必更改**
|
||||
1. **后台用户认证使用了AppKey作为JWT的加密密串,生产环境请务必更改**
|
||||
|
||||
## 相关项目
|
||||
- [QuarkSmart](https://github.com/quarkcloudio/quark-smart) 单体应用
|
||||
|
@@ -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"`
|
||||
}
|
||||
|
||||
// 插入数据
|
||||
|
@@ -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,
|
||||
|
@@ -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"`
|
||||
}
|
||||
|
||||
// 存储配置
|
||||
|
@@ -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
|
||||
|
@@ -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"`
|
||||
}
|
||||
|
||||
// 菜单表
|
||||
|
@@ -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"`
|
||||
}
|
||||
|
||||
// 获取列表
|
||||
|
@@ -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"`
|
||||
}
|
||||
|
||||
// 获取列表
|
||||
|
@@ -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"`
|
||||
}
|
||||
|
||||
// 获取角色列表
|
||||
|
@@ -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))
|
||||
|
@@ -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,
|
||||
|
@@ -22,7 +22,7 @@ const (
|
||||
AppName = "QuarkGo"
|
||||
|
||||
// 版本号
|
||||
Version = "2.4.2"
|
||||
Version = "2.4.3"
|
||||
|
||||
// 包名
|
||||
PkgName = "github.com/quarkcloudio/quark-go/v2"
|
||||
|
@@ -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,
|
||||
}
|
||||
}
|
||||
|
112
pkg/utils/datetime/datetime.go
Normal file
112
pkg/utils/datetime/datetime.go
Normal file
@@ -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)
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
||||
// 用于解析带有可选自定义格式的日期或时间字符串
|
||||
|
@@ -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")
|
||||
}
|
||||
|
||||
// 自定义格式字符串
|
||||
|
Reference in New Issue
Block a user