Files
tyapi-server/internal/application/article/dto/commands/schedule_publish_command.go

40 lines
1.2 KiB
Go
Raw Normal View History

2025-09-01 18:29:59 +08:00
package commands
2025-09-02 16:37:28 +08:00
import (
"fmt"
"time"
)
2025-09-01 18:29:59 +08:00
// SchedulePublishCommand 定时发布文章命令
type SchedulePublishCommand struct {
2025-09-02 16:37:28 +08:00
ID string `json:"-" uri:"id" binding:"required" comment:"文章ID"`
ScheduledTime string `json:"scheduled_time" binding:"required" comment:"定时发布时间"`
}
// GetScheduledTime 获取解析后的定时发布时间
func (cmd *SchedulePublishCommand) GetScheduledTime() (time.Time, error) {
// 定义中国东八区时区
cst := time.FixedZone("CST", 8*3600)
// 支持多种时间格式
formats := []string{
"2006-01-02 15:04:05", // "2025-09-02 14:12:01"
"2006-01-02T15:04:05", // "2025-09-02T14:12:01"
"2006-01-02T15:04:05Z", // "2025-09-02T14:12:01Z"
"2006-01-02 15:04", // "2025-09-02 14:12"
time.RFC3339, // "2025-09-02T14:12:01+08:00"
}
for _, format := range formats {
if t, err := time.Parse(format, cmd.ScheduledTime); err == nil {
// 如果解析的时间没有时区信息,则设置为中国东八区
if t.Location() == time.UTC {
t = t.In(cst)
}
return t, nil
}
}
return time.Time{}, fmt.Errorf("不支持的时间格式: %s请使用 YYYY-MM-DD HH:mm:ss 格式", cmd.ScheduledTime)
2025-09-01 18:29:59 +08:00
}