🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> ### 实现固定周期维护 ~~~ package main import ( "fmt" "time" ) type CloseTrade struct { StartWeek int StartHour int StartMinute int EndWeek int EndHour int EndMinute int Msg string } func main() { cTrade := CloseTrade{ StartWeek: 6, StartHour: 18, StartMinute: 30, EndWeek: 7, EndHour: 8, EndMinute: 30, Msg: "每周六18:30~周日8:30, 系统维护", } sTime := WeekByDate(cTrade.StartWeek, cTrade.StartHour, cTrade.StartMinute) eTime := WeekByDate(cTrade.EndWeek, cTrade.EndHour, cTrade.EndMinute) if sTime < time.Now().Unix() && time.Now().Unix() < eTime { fmt.Println(cTrade.Msg) } } func WeekByDate(week, hour, minute int) int64 { var ( offset int offsetDay int ) now := time.Now() switch week { case 7: offset = int(time.Sunday - now.Weekday()) offsetDay = 7 case 1: offset = int(time.Monday - now.Weekday()) case 2: offset = int(time.Tuesday - now.Weekday()) case 3: offset = int(time.Wednesday - now.Weekday()) case 4: offset = int(time.Thursday - now.Weekday()) case 5: offset = int(time.Friday - now.Weekday()) case 6: offset = int(time.Saturday - now.Weekday()) } return time.Date(now.Year(), now.Month(), now.Day()+offsetDay, hour, minute, 0, 0, time.Local).AddDate(0, 0, offset).Unix() } ~~~