feat: refact timezone calling

This commit is contained in:
engigu
2026-02-09 18:39:41 +08:00
parent eb05eafb72
commit f693c32554
10 changed files with 64 additions and 18 deletions
+2 -1
View File
@@ -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)
+4 -3
View File
@@ -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
+4 -3
View File
@@ -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":