feat: refact timezone calling
This commit is contained in:
+3
-1
@@ -6,6 +6,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/engigu/baihu-panel/internal/systime"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
@@ -46,7 +47,8 @@ func (c *customCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore
|
||||
}
|
||||
|
||||
func (c *customCore) Write(ent zapcore.Entry, fields []zapcore.Field) error {
|
||||
timestamp := ent.Time.Format("2006-01-02 15:04:05")
|
||||
// 统一使用东八区时间
|
||||
timestamp := systime.InCST(ent.Time).Format("2006-01-02 15:04:05")
|
||||
level := strings.ToUpper(ent.Level.String())
|
||||
|
||||
var levelColor string
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
internalLogger "github.com/engigu/baihu-panel/internal/logger"
|
||||
"github.com/engigu/baihu-panel/internal/systime"
|
||||
"github.com/engigu/baihu-panel/internal/utils"
|
||||
)
|
||||
|
||||
@@ -33,6 +34,8 @@ var (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 强制设置全局时区为东八区
|
||||
time.Local = systime.CST
|
||||
exePath, _ := os.Executable()
|
||||
exeDir := filepath.Dir(exePath)
|
||||
os.Chdir(exeDir)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/engigu/baihu-panel/internal/logger"
|
||||
"github.com/engigu/baihu-panel/internal/systime"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/driver/mysql"
|
||||
@@ -26,12 +27,9 @@ type Config struct {
|
||||
}
|
||||
|
||||
func Init(cfg *Config) error {
|
||||
var err error
|
||||
// 设置东八区时区
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
logger.Warnf("[Database] 加载时区失败,使用 UTC: %v", err)
|
||||
loc = time.UTC
|
||||
}
|
||||
loc := systime.CST
|
||||
time.Local = loc
|
||||
|
||||
var dialector gorm.Dialector
|
||||
|
||||
@@ -2,13 +2,14 @@ package executor
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/engigu/baihu-panel/internal/systime"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
// 东八区时区(默认)
|
||||
var defaultLocation = time.FixedZone("CST", 8*3600)
|
||||
var defaultLocation = systime.CST
|
||||
|
||||
// CronManager 统一的任务调度管理器
|
||||
type CronManager struct {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/engigu/baihu-panel/internal/systime"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
@@ -47,7 +48,8 @@ func (c *customCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore
|
||||
}
|
||||
|
||||
func (c *customCore) Write(ent zapcore.Entry, fields []zapcore.Field) error {
|
||||
timestamp := ent.Time.Format("2006-01-02 15:04:05")
|
||||
// 统一使用东八区时间
|
||||
timestamp := systime.InCST(ent.Time).Format("2006-01-02 15:04:05")
|
||||
level := strings.ToUpper(ent.Level.String())
|
||||
|
||||
var levelColor string
|
||||
@@ -82,6 +84,8 @@ func newLogger(output zapcore.WriteSyncer) *zap.Logger {
|
||||
}
|
||||
|
||||
func init() {
|
||||
// 强制设置全局时区为东八区
|
||||
time.Local = systime.CST
|
||||
atomicLevel = zap.NewAtomicLevelAt(zap.InfoLevel)
|
||||
Log = newLogger(zapcore.AddSync(os.Stdout))
|
||||
Sugar = Log.Sugar()
|
||||
@@ -93,7 +97,7 @@ func SetupFileOutput(logDir string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
logFile := filepath.Join(logDir, time.Now().Format("2006-01-02")+".log")
|
||||
logFile := filepath.Join(logDir, systime.FormatDate(time.Now())+".log")
|
||||
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/engigu/baihu-panel/internal/systime"
|
||||
)
|
||||
|
||||
const TimeFormat = "2006-01-02 15:04:05"
|
||||
@@ -16,6 +18,8 @@ func (t LocalTime) MarshalJSON() ([]byte, error) {
|
||||
if tt.IsZero() {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
// 统一输出为东八区时间
|
||||
tt = systime.InCST(tt)
|
||||
return []byte(fmt.Sprintf(`"%s"`, tt.Format(TimeFormat))), nil
|
||||
}
|
||||
|
||||
@@ -66,5 +70,5 @@ func (t LocalTime) Time() time.Time {
|
||||
}
|
||||
|
||||
func Now() LocalTime {
|
||||
return LocalTime(time.Now())
|
||||
return LocalTime(systime.Now())
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/engigu/baihu-panel/internal/constant"
|
||||
"github.com/engigu/baihu-panel/internal/database"
|
||||
"github.com/engigu/baihu-panel/internal/models"
|
||||
"github.com/engigu/baihu-panel/internal/systime"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -129,7 +130,7 @@ func (s *BackupService) CreateBackup() (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format("20060102_150405")
|
||||
timestamp := systime.FormatDatetime(time.Now())
|
||||
zipPath := filepath.Join(BackupDir, fmt.Sprintf("backup_%s.zip", timestamp))
|
||||
|
||||
zipFile, err := os.Create(zipPath)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/engigu/baihu-panel/internal/database"
|
||||
"github.com/engigu/baihu-panel/internal/models"
|
||||
"github.com/engigu/baihu-panel/internal/systime"
|
||||
)
|
||||
|
||||
type SendStatsService struct{}
|
||||
@@ -15,7 +16,7 @@ func NewSendStatsService() *SendStatsService {
|
||||
|
||||
// IncrementStats 增加任务执行统计
|
||||
func (s *SendStatsService) IncrementStats(taskID uint, status string) error {
|
||||
day := time.Now().Format("2006-01-02")
|
||||
day := systime.FormatDate(time.Now())
|
||||
|
||||
var stats models.SendStats
|
||||
result := database.DB.Where("task_id = ? AND day = ? AND status = ?", taskID, day, status).First(&stats)
|
||||
@@ -44,7 +45,7 @@ func (s *SendStatsService) GetStatsByTaskID(taskID uint) []models.SendStats {
|
||||
|
||||
// GetTodayStats 获取今日统计
|
||||
func (s *SendStatsService) GetTodayStats() []models.SendStats {
|
||||
day := time.Now().Format("2006-01-02")
|
||||
day := systime.FormatDate(time.Now())
|
||||
var stats []models.SendStats
|
||||
database.DB.Where("day = ?", day).Find(&stats)
|
||||
return stats
|
||||
@@ -52,7 +53,7 @@ func (s *SendStatsService) GetTodayStats() []models.SendStats {
|
||||
|
||||
// GetRecentStats 获取最近N天的统计
|
||||
func (s *SendStatsService) GetRecentStats(days int) []models.SendStats {
|
||||
startDay := time.Now().AddDate(0, 0, -days).Format("2006-01-02")
|
||||
startDay := systime.FormatDate(time.Now().AddDate(0, 0, -days))
|
||||
var stats []models.SendStats
|
||||
database.DB.Where("day >= ?", startDay).Order("day DESC").Find(&stats)
|
||||
return stats
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/engigu/baihu-panel/internal/database"
|
||||
"github.com/engigu/baihu-panel/internal/logger"
|
||||
"github.com/engigu/baihu-panel/internal/models"
|
||||
"github.com/engigu/baihu-panel/internal/systime"
|
||||
"github.com/engigu/baihu-panel/internal/utils"
|
||||
)
|
||||
|
||||
@@ -35,7 +36,7 @@ type CleanConfig struct {
|
||||
|
||||
// CreateEmptyLog 创建一个空的日志记录(任务开始时调用)
|
||||
func (s *TaskLogService) CreateEmptyLog(taskID uint, command string) (*models.TaskLog, error) {
|
||||
startTime := models.LocalTime(time.Now())
|
||||
startTime := models.Now()
|
||||
taskLog := &models.TaskLog{
|
||||
TaskID: taskID,
|
||||
Command: command,
|
||||
@@ -62,7 +63,7 @@ func (s *TaskLogService) SaveTaskLog(taskLog *models.TaskLog) error {
|
||||
}
|
||||
|
||||
// 更新任务的 last_run
|
||||
database.DB.Model(&models.Task{}).Where("id = ?", taskLog.TaskID).Update("last_run", time.Now())
|
||||
database.DB.Model(&models.Task{}).Where("id = ?", taskLog.TaskID).Update("last_run", models.Now())
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -109,7 +110,7 @@ func (s *TaskLogService) CleanTaskLogs(taskID uint) {
|
||||
var deleted int64
|
||||
switch config.Type {
|
||||
case "day":
|
||||
cutoff := time.Now().AddDate(0, 0, -config.Keep)
|
||||
cutoff := systime.InCST(time.Now()).AddDate(0, 0, -config.Keep)
|
||||
result := database.DB.Where("task_id = ? AND created_at < ?", taskID, cutoff).Delete(&models.TaskLog{})
|
||||
deleted = result.RowsAffected
|
||||
case "count":
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package systime
|
||||
|
||||
import "time"
|
||||
|
||||
// CST 东八区时区
|
||||
var CST = time.FixedZone("CST", 8*3600)
|
||||
|
||||
// Now 返回东八区的当前时间
|
||||
func Now() time.Time {
|
||||
return time.Now().In(CST)
|
||||
}
|
||||
|
||||
// InCST 将给定时间转换为东八区时间
|
||||
func InCST(t time.Time) time.Time {
|
||||
return t.In(CST)
|
||||
}
|
||||
|
||||
// FormatTime 将时间格式化为标准格式
|
||||
func FormatTime(t time.Time) string {
|
||||
return InCST(t).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
// FormatDate 将时间格式化为日期格式
|
||||
func FormatDate(t time.Time) string {
|
||||
return InCST(t).Format("2006-01-02")
|
||||
}
|
||||
|
||||
// FormatDatetime 用于备份等文件命名格式
|
||||
func FormatDatetime(t time.Time) string {
|
||||
return InCST(t).Format("20060102_150405")
|
||||
}
|
||||
Reference in New Issue
Block a user