周期时间计算方法《二》

package main

import (
	"fmt"
	"sort"
	"strconv"
	"strings"
	"time"
)

/*
创建会议时相关参数:
wholeType		Int 	是否全天会议
startTime		int	UTC Unix时间戳,以秒为单位。必填,若是周期会议为周期会议的第一场开始时间
length	    	int	时长(按分钟传递) , 必填, 有值
timezone		String	按照标准的时区传递:如Asia/Chongqing等, 必填
cycleRole		Json格式	周期规则(可为空)。空代表非循环会议;否则为循环会议
repeatCount		Int	循环的次数,范围2 - 100
repeatEndDate	Int	周期的结束时间,如果客户选择结束时间时,参数为当天的23:59:59,并不是会议的最后一场会议的结束时间

说明: startTime非创建会议时指定的时间戳, 而是周期会议的第一场开始时间, 由客户端计算(满足周期规则且最近的)
举例: 创建会议时开始时间为 2018/01/25 18:00(周四), 周期规则为每周一、三、五, 实际传递的startTime为1516960800(2018/01/26 18:00)
TODO: 服务器对startTime进行验证
*/

type TRecurrenceInfo struct {
	EventId           uint64 `json:"-"`
	Frequency         string `json:"frequency,omitempty"`     // daily, weekly, monthly, yearly
	Interval          int64  `json:"interval,omitempty"`      // 每几天(1-365)/周(1-52)/月(1-12)/年(1-10)
	ByDay             string `json:"byday,omitempty"`         // 星期几(SU,MO,TU,WE,TH,FR,SA), CSV格式字符串, 所以存在可能为多个
	ByMonth           string `json:"bymonth,omitempty"`       // 几月(1-12)
	ByMonthDay        string `json:"bymonthday,omitempty"`    // 几号(1-31)
	BySetPos          string `json:"bysetpos,omitempty"`      // 第几周(1-5)
	RecurrenceCount   int64  `json:"repeatcount,omitempty"`   // 循环次数(2-100)
	RecurrenceEndDate int64  `json:"repeatenddate,omitempty"` // 时间戳(秒)
}
type TRecurrenceInfoList []*TRecurrenceInfo

func (r *TRecurrenceInfo) EqualTo(rhs *TRecurrenceInfo) bool {
	return r.Frequency == rhs.Frequency &&
		r.Interval == rhs.Interval &&
		r.ByDay == rhs.ByDay &&
		r.ByMonth == rhs.ByMonth &&
		r.ByMonthDay == rhs.ByMonthDay &&
		r.BySetPos == rhs.BySetPos &&
		r.RecurrenceCount == rhs.RecurrenceCount &&
		r.RecurrenceEndDate == rhs.RecurrenceEndDate
}

const (
	DayDuration   = int64(time.Hour) * 24
	WeekDuration  = int64(time.Hour) * 24 * 7
	MonthDuration = int64(time.Hour) * 24 * 30
	YearDuration  = int64(time.Hour) * 24 * 365
)

//@index 表示的是周期会议里的第几天
//@fstStartTime 预约会议的开始的时间,不一定是第一场周期会议的时间,比如,对于以“周”为周期,每周工作日这种case,如果是周六预约会议,每个工作日的五点开始会议,那么,这个fstStartTime则表示周六五点这个时间
//@fstEndTime 预约会议的结束时间,不一定是第一场周期会议的时间
func (rule *TRecurrenceInfo) GetRecurrenceTime(index, fstStartTime, fstEndTime int64, loc *time.Location) (start, end time.Time) {
	if index < 0 {
		return
	}

	// 处理周期会议
	var deltaDuration int64 = 0
	if rule.RecurrenceCount > 0 {
		if index >= rule.RecurrenceCount {
			return
		}
	}

	start = time.Unix(fstStartTime, 0).In(loc)
	end = time.Unix(fstEndTime, 0).In(loc)

	interval := rule.Interval
	switch strings.ToUpper(rule.Frequency) {
	case DayRecurrenceFrequency:
		deltaDuration = index * interval * DayDuration
		start = start.Add(time.Duration(deltaDuration))
		end = end.Add(time.Duration(deltaDuration))
	case WeekRecurrenceFrequency:
		dayList := strings.Split(rule.ByDay, ",")
		dayLen := int64(len(dayList))
		if dayLen == 0 {
			//case:ByDay:"MO"
			dayList = append(dayList, rule.ByDay)
		}
		if len(dayList) > 0 {
			//获取周期会议的第一场时间
			start, end = rule.GetWeekRuleFstDay(start, end, loc)
			startWeekday := start.Weekday()
			firstDay := string(WeekdayString(startWeekday))
			firstDayInt := int64(startWeekday)
			//周期会议的第一场,这一天是周期规则中第几个
			//比如:会议开始时间是周二,周期规则为每周二三五,则可以算出,符合规则中的第1个规则
			offset := 0
			for i, day := range dayList {
				if day == firstDay {
					offset = i
					break
				}
			}
			indexForRule := index + int64(offset)
			i := indexForRule / dayLen // 第i周
			j := indexForRule % dayLen // 第i周第j个规则

			// 计算第i周的时间间隔
			deltaDuration = i * interval * WeekDuration
			dayInt := int64(WeekdayInt(dayList[j]))
			// 星期日为0,转换为7, 客户端顺序为星期一、二、三、四、五、六、日
			if dayInt == 0 {
				dayInt = 7
			}
			if firstDayInt == 0 {
				firstDayInt = 7
			}
			// 加上与第一场间隔的天数, 有可能为负数
			delta := (dayInt - firstDayInt) * DayDuration
			deltaDuration += delta
			//fmt.Println("start:", start, "end:", end, "dayList:", dayList, "startWeekday:", startWeekday, "offset:", offset, "indexForRule:", indexForRule, "i:", i, "j:", j, "dayInt:", dayInt, "firstDayInt", firstDayInt)
			start = start.Add(time.Duration(deltaDuration))
			end = end.Add(time.Duration(deltaDuration))
		}
	case MonthRecurrenceFrequency:
		if rule.BySetPos == "" {
			dayList := strings.Split(rule.ByMonthDay, ",") //比如:ByMonthDay:"2,3,4,5,6"
			dayLen := int64(len(dayList))
			if dayLen == 0 {
				//case:ByMonthDay:"2"
				dayList = append(dayList, rule.ByMonthDay)
			}
			if len(dayList) > 0 {
				//获取周期会议的第一场时间
				start, end = rule.GetMonthRuleFstDay(start, end, dayList, loc)
				_, _, startMonthDay := start.Date()
				offset := 0 //表示符合第几个周期规则
				//周期会议的第一场,这一天是周期规则中第几个
				//比如:会议开始时间是20号,周期规则为每月1,5,10,20,25号,则可以算出,符合规则中的第四个规则
				for i, day := range dayList {
					if ToInt64(day, 0) == int64(startMonthDay) {
						offset = i
						break
					}
				}
				//第几场周期会议相对于第一场会议,间隔几个月,第几个月的第几天
				indexForRule := index + int64(offset)
				i := indexForRule / dayLen // 第i月
				j := indexForRule % dayLen // 第i月第j个规则

				// 计算第i月的时间间隔
				nextMonthTime := start.AddDate(0, int(i*interval), 0)
				deltaDuration = int64(nextMonthTime.Sub(start))
				dayInt := ToInt64(dayList[j], 0)
				//fmt.Println("dayList:", dayList, "startMonthDay:", startMonthDay, "offset:", offset, "indexForRule:", indexForRule, "i:", i, "j:", j, "dayInt:", dayInt)
				// 加上与开始时间间隔的天数, 有可能为负数
				delta := (dayInt - int64(startMonthDay)) * DayDuration
				deltaDuration += delta
				start = start.Add(time.Duration(deltaDuration))
				end = end.Add(time.Duration(deltaDuration))
				break
			}
		} else {
			bySetPos := ToInt64(rule.BySetPos, 1)
			year, month, _ := start.Date()
			hour, minute, sec := start.Clock()
			start = Date(year, month+time.Month(index*interval), int(bySetPos),
				WeekdayInt(rule.ByDay), hour, minute, sec, start.Location())

			year, month, _ = end.Date()
			hour, minute, sec = start.Clock()
			end = Date(year, month+time.Month(index*interval), int(bySetPos),
				WeekdayInt(rule.ByDay), hour, minute, sec, end.Location())
		}

	case YearRecurrenceFrequency:
		if rule.BySetPos == "" {
			nextYearTime := start.AddDate(int(index*interval), 0, 0)
			deltaDuration = int64(nextYearTime.Sub(start))
			start = start.Add(time.Duration(deltaDuration))
			end = end.Add(time.Duration(deltaDuration))
		} else {
			bySetPos := ToInt64(rule.BySetPos, 1)
			year, month, _ := start.Date()
			hour, minute, sec := start.Clock()
			start = Date(year+int(index*interval), month, int(bySetPos),
				WeekdayInt(rule.ByDay), hour, minute, sec, start.Location())

			year, month, _ = end.Date()
			hour, minute, sec = end.Clock()
			end = Date(year+int(index*interval), month, int(bySetPos),
				WeekdayInt(rule.ByDay), hour, minute, sec, end.Location())
		}
	}

	if rule.RecurrenceEndDate == 0 {
		return
	}

	last := time.Unix(rule.RecurrenceEndDate, 0).In(start.Location())
	if start.After(last) {
		start = time.Time{}
		end = time.Time{}
	}

	return
}

func (rule *TRecurrenceInfo) convertDayList() []int {
	dayList := strings.Split(rule.ByDay, ",")
	if len(dayList) == 0 {
		dayList = append(dayList, rule.ByDay)
	}
	dayWeekInts := make([]int, 0)
	for _, day := range dayList {
		dayWeekInt := int(WeekdayInt(day))
		if dayWeekInt == 0 {
			dayWeekInt = 7
		}
		dayWeekInts = append(dayWeekInts, dayWeekInt)
	}
	sort.Ints(dayWeekInts)
	return dayWeekInts
}

//获取周规则的周期会议的第一场会议
func (rule *TRecurrenceInfo) GetWeekRuleFstDay(start, end time.Time, loc *time.Location) (fstStartTime, fstEndTime time.Time) {
	startWeekday := start.Weekday()
	startWeekDayInt := int64(startWeekday)
	dayList := rule.convertDayList()
	//因为,传进来的参数,不是符合周期规则的会议开始时间,而是,当天预约会议的开始时间
	//因此,当传进来的时间,不是周期规则的时间,则计算出周期会议第一场的开始时间

	//周期规则中的第几个规则,比如:周期规则是周的一,三,五。
	//如果是周二约的会议,那么周期会议的第一场应该是周三,offset就是1即周期规则中的第二个。
	//如果是周六约的会议,那么第一场就应该是下周一,offset取0
	//如果是周五预约的会议,那么这个时间就是第一场的时间,直接返回
	offset := 0
	for i, day := range dayList {
		dayInt := ToInt64(day, 0)
		if dayInt == startWeekDayInt {
			fstStartTime = start
			fstEndTime = end
			return
		}
		if dayInt > startWeekDayInt {
			offset = i
			break
		}
	}
	fstWeekDay := int64(dayList[offset])
	var deleduration int64
	deleduration = (fstWeekDay - startWeekDayInt) * DayDuration
	if deleduration < 0 {
		deleduration = (int64(7) - startWeekDayInt + fstWeekDay) * DayDuration
	}
	fstStartTime = start.Add(time.Duration(deleduration))
	fstEndTime = end.Add(time.Duration(deleduration))
	return
}

//获取月规则的周期会议的第一场会议
func (rule *TRecurrenceInfo) GetMonthRuleFstDay(start, end time.Time, dayList []string, loc *time.Location) (fstStartTime, fstEndTime time.Time) {
	_, _, startMonthDay := start.Date()
	//因为,传进来的参数,不是符合周期规则的会议开始时间,而是,当天预约会议的开始时间
	//因此,当传进来的时间,不是周期规则的时间,则计算出周期会议第一场的开始时间

	//周期规则中的第几个规则,比如:周期规则是每个月的5,10,15,20号。
	//如果是11号约的会议,那么周期会议的第一场应该是15号,offset就是2即周期规则中的第三个。
	//如果是21号约的会议,那么第一场就应该是下个月的5号,offset取0
	//如果是20号预约的会议,那么这个时间就是第一场的时间,直接返回
	offset := 0
	for i, day := range dayList {
		dayInt := ToInt64(day, 0)
		if dayInt == int64(startMonthDay) {
			fstStartTime = start
			fstEndTime = end
			return
		}
		if dayInt > int64(startMonthDay) {
			offset = i
			break
		}
	}
	dayInt := int(ToInt64(dayList[offset], 0))
	year, month, _ := start.Date()
	hour, minute, sec := start.Clock()
	if offset == 0 {
		month = month + 1
	}
	fstStartTime = time.Date(year, month, dayInt, hour, minute, sec, 0, loc)
	year, month, _ = end.Date()
	hour, minute, sec = end.Clock()
	fstEndTime = time.Date(year, month, dayInt, hour, minute, sec, 0, loc)
	return
}

func Date(year int, month time.Month, monthWeek int, weekDay time.Weekday, hour, minute, sec int, loc *time.Location) time.Time {
	monthFstDayTime := time.Date(year, month, 1, hour, minute, sec, 0, loc) //一号
	week := monthFstDayTime.Weekday()

	var monthDay int = 1
	if week != time.Sunday { // 算出第一个星期天到底是几号
		monthDay = 1 + int(time.Sunday) - int(week)
	}

	monthDay = monthDay + (monthWeek * 7) + int(weekDay)

	return time.Date(year, month, monthDay, hour, minute, sec, 0, loc)
}

func ToInt64(v interface{}, defaultVal int64) int64 {
	if v == nil {
		return defaultVal
	}

	switch v.(type) {
	case bool:
		if v.(bool) {
			return 1
		}
		return 0
	case string:
		i, err := strconv.ParseInt(v.(string), 10, 64)
		if err != nil {
			return defaultVal
		}
		return i
	case uint64:
		return int64(v.(uint64))
	case int64:
		return int64(v.(int64))
	case int:
		return int64(v.(int))
	case int32:
		return int64(v.(int32))
	case uint32:
		return int64(v.(uint32))
	case float64:
		return int64(v.(float64))
	case int8:
		return int64(v.(int8))
	case uint8:
		return int64(v.(uint8))
	}
	return defaultVal
}

// the frequency an event recurs
type RecurrenceFrequency string

const (
	SecondRecurrenceFrequency RecurrenceFrequency = "SECONDLY"
	MinuteRecurrenceFrequency                     = "MINUTELY"
	HourRecurrenceFrequency                       = "HOURLY"
	DayRecurrenceFrequency                        = "DAILY"
	WeekRecurrenceFrequency                       = "WEEKLY"
	MonthRecurrenceFrequency                      = "MONTHLY"
	YearRecurrenceFrequency                       = "YEARLY"
)

// the frequency an event recurs
type RecurrenceWeekday string

const (
	MondayRecurrenceWeekday    RecurrenceWeekday = "MO"
	TuesdayRecurrenceWeekday                     = "TU"
	WednesdayRecurrenceWeekday                   = "WE"
	ThursdayRecurrenceWeekday                    = "TH"
	FridayRecurrenceWeekday                      = "FR"
	SaturdayRecurrenceWeekday                    = "SA"
	SundayRecurrenceWeekday                      = "SU"
)

func WeekdayString(weekday time.Weekday) RecurrenceWeekday {
	var r RecurrenceWeekday
	switch weekday {
	case time.Sunday:
		r = SundayRecurrenceWeekday
	case time.Monday:
		r = MondayRecurrenceWeekday
	case time.Tuesday:
		r = TuesdayRecurrenceWeekday
	case time.Wednesday:
		r = WednesdayRecurrenceWeekday
	case time.Thursday:
		r = ThursdayRecurrenceWeekday
	case time.Friday:
		r = FridayRecurrenceWeekday
	case time.Saturday:
		return SaturdayRecurrenceWeekday
	}
	return r
}

func WeekdayInt(s string) time.Weekday {
	var r time.Weekday
	switch RecurrenceWeekday(s) {
	case SundayRecurrenceWeekday:
		r = time.Sunday
	case MondayRecurrenceWeekday:
		r = time.Monday
	case TuesdayRecurrenceWeekday:
		r = time.Tuesday
	case WednesdayRecurrenceWeekday:
		r = time.Wednesday
	case ThursdayRecurrenceWeekday:
		r = time.Thursday
	case FridayRecurrenceWeekday:
		r = time.Friday
	case SaturdayRecurrenceWeekday:
		r = time.Saturday
	}
	return r
}

func main() {
	GetWeekRuleRecurrenceTime()
	GetWeekRuleRecurrenceTime2()
	GetWeekRuleRecurrenceTime3()
	GetWeekRuleRecurrenceTime4()
	GetWeekRuleRecurrenceTime11()
	GetWeekRuleRecurrenceTime22()
	GetWeekRuleRecurrenceTime33()
	GetWeekRuleRecurrenceTime44()
	GetMonthRuleRecurrenceTime()
	GetMonthRuleRecurrenceTime2()
	GetMonthRuleRecurrenceTime3()
	GetMonthRuleRecurrenceTime4()
	GetMonthRuleRecurrenceTime11()
	GetMonthRuleRecurrenceTime22()
	GetMonthRuleRecurrenceTime33()
	GetMonthRuleRecurrenceTime44()
}

func GetWeekRuleRecurrenceTime() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeWeek...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00  //周三
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "weekly"
	recurr.Interval = 1
	recurr.ByDay = "TU,SU" // 星期二、日

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetWeekRuleRecurrenceTime2() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeWeek2...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00  //周三
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "weekly"
	recurr.Interval = 1
	recurr.ByDay = "TU,WE,SU" // 星期二、三、日

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetWeekRuleRecurrenceTime3() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeWeek3...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00  //周三
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "weekly"
	recurr.Interval = 1
	recurr.ByDay = "MO,TU" // 星期一、二

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetWeekRuleRecurrenceTime4() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeWeek4...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00  //周三
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "weekly"
	recurr.Interval = 1
	recurr.ByDay = "WE" // 星期三

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetWeekRuleRecurrenceTime11() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeWeek11 cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeWeek11...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00  //周三
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "weekly"
	recurr.Interval = 2
	recurr.ByDay = "TU,SU" // 星期二、日

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetWeekRuleRecurrenceTime22() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeWeek22...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00  //周三
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "weekly"
	recurr.Interval = 2
	recurr.ByDay = "TU,WE,SU" // 星期二、三、日

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetWeekRuleRecurrenceTime33() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeWeek33...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00  //周三
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "weekly"
	recurr.Interval = 2
	recurr.ByDay = "MO,TU" // 星期一、二

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetWeekRuleRecurrenceTime44() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeWeek44...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00  //周三
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "weekly"
	recurr.Interval = 2
	recurr.ByDay = "WE" // 星期三

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetMonthRuleRecurrenceTime() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeMonth cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeMonth1...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "monthly"
	recurr.Interval = 1
	recurr.ByMonthDay = "2,3,4,5,6"

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetMonthRuleRecurrenceTime2() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeMonth2 cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeMonth2...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "monthly"
	recurr.Interval = 1
	recurr.ByMonthDay = "2,3,4,5,6,25,26,28"

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetMonthRuleRecurrenceTime3() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeMonth3 cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeMonth3...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "monthly"
	recurr.Interval = 1
	recurr.ByMonthDay = "2,3,4,5,6,24,25,26,28"

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetMonthRuleRecurrenceTime4() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeMonth4 cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeMonth4...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "monthly"
	recurr.Interval = 1
	recurr.ByMonthDay = "24"

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetMonthRuleRecurrenceTime11() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeMonth cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeMonth11...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "monthly"
	recurr.Interval = 2
	recurr.ByMonthDay = "2,3,4,5,6"

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetMonthRuleRecurrenceTime22() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeMonth2 cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeMonth22...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "monthly"
	recurr.Interval = 2
	recurr.ByMonthDay = "2,3,4,5,6,25,26,28"

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetMonthRuleRecurrenceTime33() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeMonth3 cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeMonth3...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "monthly"
	recurr.Interval = 2
	recurr.ByMonthDay = "2,3,4,5,6,24,25,26,28"

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

func GetMonthRuleRecurrenceTime44() {
	start := time.Now()
	defer func() {
		cost := time.Since(start)
		fmt.Println("TestGetRecurrenceTimeMonth4 cost=", cost)
	}()
	fmt.Println("####################TestGetRecurrenceTimeMonth44...")
	loc, _ := time.LoadLocation("Asia/Shanghai")
	fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
	fstEndTime := int64(1661335200)   // 2022-08-24 18:00:00
	recurr := TRecurrenceInfo{}
	recurr.Frequency = "monthly"
	recurr.Interval = 2
	recurr.ByMonthDay = "24"

	for i := 0; i <= 10; i++ {
		start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
		fmt.Println("start:", start, "end:", end)
	}
}

你可能感兴趣的:(Go,golang)