37 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package commands
 | ||
| 
 | ||
| import (
 | ||
| 	"fmt"
 | ||
| 	"time"
 | ||
| )
 | ||
| 
 | ||
| // SchedulePublishCommand 定时发布文章命令
 | ||
| type SchedulePublishCommand struct {
 | ||
| 	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.ParseInLocation(format, cmd.ScheduledTime, cst); err == nil {
 | ||
| 			// 确保返回的时间是东八区时区
 | ||
| 			return t.In(cst), nil
 | ||
| 		}
 | ||
| 	}
 | ||
| 
 | ||
| 	return time.Time{}, fmt.Errorf("不支持的时间格式: %s,请使用 YYYY-MM-DD HH:mm:ss 格式", cmd.ScheduledTime)
 | ||
| }
 |