Files
mayfly-go/server/pkg/utils/timex/timex.go
kanzihuang ae3d2659aa 重构数据库备份与恢复模块 (#80)
* fix: 保存 LastResult 时截断字符串过长部分,以避免数据库报错

* refactor: 新增 entity.DbTaskBase 和 persistence.dbTaskBase, 用于实现数据库备份和恢复任务处理相关部分

* fix: aeskey变更后,解密密码出现数组越界访问错误

* fix: 时间属性为零值时,保存到 mysql 数据库报错

* refactor db.infrastructure.service.scheduler

* feat: 实现立即备份功能

* refactor db.infrastructure.service.db_instance

* refactor: 从数据库中获取数据库备份目录、mysql文件路径等配置信息

* fix: 数据库备份和恢复问题

* fix: 修改 .gitignore 文件,忽略数据库备份目录和数据库程序目录
2024-01-05 08:55:34 +08:00

61 lines
1.0 KiB
Go

package timex
import (
"context"
"database/sql"
"encoding/json"
"time"
)
const DefaultDateTimeFormat = "2006-01-02 15:04:05"
func DefaultFormat(time time.Time) string {
return time.Format(DefaultDateTimeFormat)
}
func NewNullTime(t time.Time) NullTime {
return NullTime{
NullTime: sql.NullTime{
Time: t,
Valid: !t.IsZero(),
},
}
}
type NullTime struct {
sql.NullTime
}
func (nt *NullTime) UnmarshalJSON(bytes []byte) error {
if len(bytes) == 0 {
nt.NullTime = sql.NullTime{}
return nil
}
var t time.Time
if err := json.Unmarshal(bytes, &t); err != nil {
return err
}
if t.IsZero() {
nt.NullTime = sql.NullTime{}
return nil
}
nt.NullTime = sql.NullTime{
Valid: true,
Time: t,
}
return nil
}
func (nt *NullTime) MarshalJSON() ([]byte, error) {
if !nt.Valid || nt.Time.IsZero() {
return json.Marshal(nil)
}
return json.Marshal(nt.Time)
}
func SleepWithContext(ctx context.Context, d time.Duration) {
ctx, cancel := context.WithTimeout(ctx, d)
<-ctx.Done()
cancel()
}