• 周期时间计算方法《二》


    1. package main
    2. import (
    3. "fmt"
    4. "sort"
    5. "strconv"
    6. "strings"
    7. "time"
    8. )
    9. /*
    10. 创建会议时相关参数:
    11. wholeType Int 是否全天会议
    12. startTime int UTC Unix时间戳,以秒为单位。必填,若是周期会议为周期会议的第一场开始时间
    13. length int 时长(按分钟传递) , 必填, 有值
    14. timezone String 按照标准的时区传递:如Asia/Chongqing等, 必填
    15. cycleRole Json格式 周期规则(可为空)。空代表非循环会议;否则为循环会议
    16. repeatCount Int 循环的次数,范围2 - 100
    17. repeatEndDate Int 周期的结束时间,如果客户选择结束时间时,参数为当天的23:59:59,并不是会议的最后一场会议的结束时间
    18. 说明: startTime非创建会议时指定的时间戳, 而是周期会议的第一场开始时间, 由客户端计算(满足周期规则且最近的)
    19. 举例: 创建会议时开始时间为 2018/01/25 18:00(周四), 周期规则为每周一、三、五, 实际传递的startTime为1516960800(2018/01/26 18:00)
    20. TODO: 服务器对startTime进行验证
    21. */
    22. type TRecurrenceInfo struct {
    23. EventId uint64 `json:"-"`
    24. Frequency string `json:"frequency,omitempty"` // daily, weekly, monthly, yearly
    25. Interval int64 `json:"interval,omitempty"` // 每几天(1-365)/周(1-52)/月(1-12)/年(1-10)
    26. ByDay string `json:"byday,omitempty"` // 星期几(SU,MO,TU,WE,TH,FR,SA), CSV格式字符串, 所以存在可能为多个
    27. ByMonth string `json:"bymonth,omitempty"` // 几月(1-12)
    28. ByMonthDay string `json:"bymonthday,omitempty"` // 几号(1-31)
    29. BySetPos string `json:"bysetpos,omitempty"` // 第几周(1-5)
    30. RecurrenceCount int64 `json:"repeatcount,omitempty"` // 循环次数(2-100)
    31. RecurrenceEndDate int64 `json:"repeatenddate,omitempty"` // 时间戳(秒)
    32. }
    33. type TRecurrenceInfoList []*TRecurrenceInfo
    34. func (r *TRecurrenceInfo) EqualTo(rhs *TRecurrenceInfo) bool {
    35. return r.Frequency == rhs.Frequency &&
    36. r.Interval == rhs.Interval &&
    37. r.ByDay == rhs.ByDay &&
    38. r.ByMonth == rhs.ByMonth &&
    39. r.ByMonthDay == rhs.ByMonthDay &&
    40. r.BySetPos == rhs.BySetPos &&
    41. r.RecurrenceCount == rhs.RecurrenceCount &&
    42. r.RecurrenceEndDate == rhs.RecurrenceEndDate
    43. }
    44. const (
    45. DayDuration = int64(time.Hour) * 24
    46. WeekDuration = int64(time.Hour) * 24 * 7
    47. MonthDuration = int64(time.Hour) * 24 * 30
    48. YearDuration = int64(time.Hour) * 24 * 365
    49. )
    50. //@index 表示的是周期会议里的第几天
    51. //@fstStartTime 预约会议的开始的时间,不一定是第一场周期会议的时间,比如,对于以“周”为周期,每周工作日这种case,如果是周六预约会议,每个工作日的五点开始会议,那么,这个fstStartTime则表示周六五点这个时间
    52. //@fstEndTime 预约会议的结束时间,不一定是第一场周期会议的时间
    53. func (rule *TRecurrenceInfo) GetRecurrenceTime(index, fstStartTime, fstEndTime int64, loc *time.Location) (start, end time.Time) {
    54. if index < 0 {
    55. return
    56. }
    57. // 处理周期会议
    58. var deltaDuration int64 = 0
    59. if rule.RecurrenceCount > 0 {
    60. if index >= rule.RecurrenceCount {
    61. return
    62. }
    63. }
    64. start = time.Unix(fstStartTime, 0).In(loc)
    65. end = time.Unix(fstEndTime, 0).In(loc)
    66. interval := rule.Interval
    67. switch strings.ToUpper(rule.Frequency) {
    68. case DayRecurrenceFrequency:
    69. deltaDuration = index * interval * DayDuration
    70. start = start.Add(time.Duration(deltaDuration))
    71. end = end.Add(time.Duration(deltaDuration))
    72. case WeekRecurrenceFrequency:
    73. dayList := strings.Split(rule.ByDay, ",")
    74. dayLen := int64(len(dayList))
    75. if dayLen == 0 {
    76. //case:ByDay:"MO"
    77. dayList = append(dayList, rule.ByDay)
    78. }
    79. if len(dayList) > 0 {
    80. //获取周期会议的第一场时间
    81. start, end = rule.GetWeekRuleFstDay(start, end, loc)
    82. startWeekday := start.Weekday()
    83. firstDay := string(WeekdayString(startWeekday))
    84. firstDayInt := int64(startWeekday)
    85. //周期会议的第一场,这一天是周期规则中第几个
    86. //比如:会议开始时间是周二,周期规则为每周二三五,则可以算出,符合规则中的第1个规则
    87. offset := 0
    88. for i, day := range dayList {
    89. if day == firstDay {
    90. offset = i
    91. break
    92. }
    93. }
    94. indexForRule := index + int64(offset)
    95. i := indexForRule / dayLen // 第i周
    96. j := indexForRule % dayLen // 第i周第j个规则
    97. // 计算第i周的时间间隔
    98. deltaDuration = i * interval * WeekDuration
    99. dayInt := int64(WeekdayInt(dayList[j]))
    100. // 星期日为0,转换为7, 客户端顺序为星期一、二、三、四、五、六、日
    101. if dayInt == 0 {
    102. dayInt = 7
    103. }
    104. if firstDayInt == 0 {
    105. firstDayInt = 7
    106. }
    107. // 加上与第一场间隔的天数, 有可能为负数
    108. delta := (dayInt - firstDayInt) * DayDuration
    109. deltaDuration += delta
    110. //fmt.Println("start:", start, "end:", end, "dayList:", dayList, "startWeekday:", startWeekday, "offset:", offset, "indexForRule:", indexForRule, "i:", i, "j:", j, "dayInt:", dayInt, "firstDayInt", firstDayInt)
    111. start = start.Add(time.Duration(deltaDuration))
    112. end = end.Add(time.Duration(deltaDuration))
    113. }
    114. case MonthRecurrenceFrequency:
    115. if rule.BySetPos == "" {
    116. dayList := strings.Split(rule.ByMonthDay, ",") //比如:ByMonthDay:"2,3,4,5,6"
    117. dayLen := int64(len(dayList))
    118. if dayLen == 0 {
    119. //case:ByMonthDay:"2"
    120. dayList = append(dayList, rule.ByMonthDay)
    121. }
    122. if len(dayList) > 0 {
    123. //获取周期会议的第一场时间
    124. start, end = rule.GetMonthRuleFstDay(start, end, dayList, loc)
    125. _, _, startMonthDay := start.Date()
    126. offset := 0 //表示符合第几个周期规则
    127. //周期会议的第一场,这一天是周期规则中第几个
    128. //比如:会议开始时间是20号,周期规则为每月1,5,10,20,25号,则可以算出,符合规则中的第四个规则
    129. for i, day := range dayList {
    130. if ToInt64(day, 0) == int64(startMonthDay) {
    131. offset = i
    132. break
    133. }
    134. }
    135. //第几场周期会议相对于第一场会议,间隔几个月,第几个月的第几天
    136. indexForRule := index + int64(offset)
    137. i := indexForRule / dayLen // 第i月
    138. j := indexForRule % dayLen // 第i月第j个规则
    139. // 计算第i月的时间间隔
    140. nextMonthTime := start.AddDate(0, int(i*interval), 0)
    141. deltaDuration = int64(nextMonthTime.Sub(start))
    142. dayInt := ToInt64(dayList[j], 0)
    143. //fmt.Println("dayList:", dayList, "startMonthDay:", startMonthDay, "offset:", offset, "indexForRule:", indexForRule, "i:", i, "j:", j, "dayInt:", dayInt)
    144. // 加上与开始时间间隔的天数, 有可能为负数
    145. delta := (dayInt - int64(startMonthDay)) * DayDuration
    146. deltaDuration += delta
    147. start = start.Add(time.Duration(deltaDuration))
    148. end = end.Add(time.Duration(deltaDuration))
    149. break
    150. }
    151. } else {
    152. bySetPos := ToInt64(rule.BySetPos, 1)
    153. year, month, _ := start.Date()
    154. hour, minute, sec := start.Clock()
    155. start = Date(year, month+time.Month(index*interval), int(bySetPos),
    156. WeekdayInt(rule.ByDay), hour, minute, sec, start.Location())
    157. year, month, _ = end.Date()
    158. hour, minute, sec = start.Clock()
    159. end = Date(year, month+time.Month(index*interval), int(bySetPos),
    160. WeekdayInt(rule.ByDay), hour, minute, sec, end.Location())
    161. }
    162. case YearRecurrenceFrequency:
    163. if rule.BySetPos == "" {
    164. nextYearTime := start.AddDate(int(index*interval), 0, 0)
    165. deltaDuration = int64(nextYearTime.Sub(start))
    166. start = start.Add(time.Duration(deltaDuration))
    167. end = end.Add(time.Duration(deltaDuration))
    168. } else {
    169. bySetPos := ToInt64(rule.BySetPos, 1)
    170. year, month, _ := start.Date()
    171. hour, minute, sec := start.Clock()
    172. start = Date(year+int(index*interval), month, int(bySetPos),
    173. WeekdayInt(rule.ByDay), hour, minute, sec, start.Location())
    174. year, month, _ = end.Date()
    175. hour, minute, sec = end.Clock()
    176. end = Date(year+int(index*interval), month, int(bySetPos),
    177. WeekdayInt(rule.ByDay), hour, minute, sec, end.Location())
    178. }
    179. }
    180. if rule.RecurrenceEndDate == 0 {
    181. return
    182. }
    183. last := time.Unix(rule.RecurrenceEndDate, 0).In(start.Location())
    184. if start.After(last) {
    185. start = time.Time{}
    186. end = time.Time{}
    187. }
    188. return
    189. }
    190. func (rule *TRecurrenceInfo) convertDayList() []int {
    191. dayList := strings.Split(rule.ByDay, ",")
    192. if len(dayList) == 0 {
    193. dayList = append(dayList, rule.ByDay)
    194. }
    195. dayWeekInts := make([]int, 0)
    196. for _, day := range dayList {
    197. dayWeekInt := int(WeekdayInt(day))
    198. if dayWeekInt == 0 {
    199. dayWeekInt = 7
    200. }
    201. dayWeekInts = append(dayWeekInts, dayWeekInt)
    202. }
    203. sort.Ints(dayWeekInts)
    204. return dayWeekInts
    205. }
    206. //获取周规则的周期会议的第一场会议
    207. func (rule *TRecurrenceInfo) GetWeekRuleFstDay(start, end time.Time, loc *time.Location) (fstStartTime, fstEndTime time.Time) {
    208. startWeekday := start.Weekday()
    209. startWeekDayInt := int64(startWeekday)
    210. dayList := rule.convertDayList()
    211. //因为,传进来的参数,不是符合周期规则的会议开始时间,而是,当天预约会议的开始时间
    212. //因此,当传进来的时间,不是周期规则的时间,则计算出周期会议第一场的开始时间
    213. //周期规则中的第几个规则,比如:周期规则是周的一,三,五。
    214. //如果是周二约的会议,那么周期会议的第一场应该是周三,offset就是1即周期规则中的第二个。
    215. //如果是周六约的会议,那么第一场就应该是下周一,offset取0
    216. //如果是周五预约的会议,那么这个时间就是第一场的时间,直接返回
    217. offset := 0
    218. for i, day := range dayList {
    219. dayInt := ToInt64(day, 0)
    220. if dayInt == startWeekDayInt {
    221. fstStartTime = start
    222. fstEndTime = end
    223. return
    224. }
    225. if dayInt > startWeekDayInt {
    226. offset = i
    227. break
    228. }
    229. }
    230. fstWeekDay := int64(dayList[offset])
    231. var deleduration int64
    232. deleduration = (fstWeekDay - startWeekDayInt) * DayDuration
    233. if deleduration < 0 {
    234. deleduration = (int64(7) - startWeekDayInt + fstWeekDay) * DayDuration
    235. }
    236. fstStartTime = start.Add(time.Duration(deleduration))
    237. fstEndTime = end.Add(time.Duration(deleduration))
    238. return
    239. }
    240. //获取月规则的周期会议的第一场会议
    241. func (rule *TRecurrenceInfo) GetMonthRuleFstDay(start, end time.Time, dayList []string, loc *time.Location) (fstStartTime, fstEndTime time.Time) {
    242. _, _, startMonthDay := start.Date()
    243. //因为,传进来的参数,不是符合周期规则的会议开始时间,而是,当天预约会议的开始时间
    244. //因此,当传进来的时间,不是周期规则的时间,则计算出周期会议第一场的开始时间
    245. //周期规则中的第几个规则,比如:周期规则是每个月的5,10,15,20号。
    246. //如果是11号约的会议,那么周期会议的第一场应该是15号,offset就是2即周期规则中的第三个。
    247. //如果是21号约的会议,那么第一场就应该是下个月的5号,offset取0
    248. //如果是20号预约的会议,那么这个时间就是第一场的时间,直接返回
    249. offset := 0
    250. for i, day := range dayList {
    251. dayInt := ToInt64(day, 0)
    252. if dayInt == int64(startMonthDay) {
    253. fstStartTime = start
    254. fstEndTime = end
    255. return
    256. }
    257. if dayInt > int64(startMonthDay) {
    258. offset = i
    259. break
    260. }
    261. }
    262. dayInt := int(ToInt64(dayList[offset], 0))
    263. year, month, _ := start.Date()
    264. hour, minute, sec := start.Clock()
    265. if offset == 0 {
    266. month = month + 1
    267. }
    268. fstStartTime = time.Date(year, month, dayInt, hour, minute, sec, 0, loc)
    269. year, month, _ = end.Date()
    270. hour, minute, sec = end.Clock()
    271. fstEndTime = time.Date(year, month, dayInt, hour, minute, sec, 0, loc)
    272. return
    273. }
    274. func Date(year int, month time.Month, monthWeek int, weekDay time.Weekday, hour, minute, sec int, loc *time.Location) time.Time {
    275. monthFstDayTime := time.Date(year, month, 1, hour, minute, sec, 0, loc) //一号
    276. week := monthFstDayTime.Weekday()
    277. var monthDay int = 1
    278. if week != time.Sunday { // 算出第一个星期天到底是几号
    279. monthDay = 1 + int(time.Sunday) - int(week)
    280. }
    281. monthDay = monthDay + (monthWeek * 7) + int(weekDay)
    282. return time.Date(year, month, monthDay, hour, minute, sec, 0, loc)
    283. }
    284. func ToInt64(v interface{}, defaultVal int64) int64 {
    285. if v == nil {
    286. return defaultVal
    287. }
    288. switch v.(type) {
    289. case bool:
    290. if v.(bool) {
    291. return 1
    292. }
    293. return 0
    294. case string:
    295. i, err := strconv.ParseInt(v.(string), 10, 64)
    296. if err != nil {
    297. return defaultVal
    298. }
    299. return i
    300. case uint64:
    301. return int64(v.(uint64))
    302. case int64:
    303. return int64(v.(int64))
    304. case int:
    305. return int64(v.(int))
    306. case int32:
    307. return int64(v.(int32))
    308. case uint32:
    309. return int64(v.(uint32))
    310. case float64:
    311. return int64(v.(float64))
    312. case int8:
    313. return int64(v.(int8))
    314. case uint8:
    315. return int64(v.(uint8))
    316. }
    317. return defaultVal
    318. }
    319. // the frequency an event recurs
    320. type RecurrenceFrequency string
    321. const (
    322. SecondRecurrenceFrequency RecurrenceFrequency = "SECONDLY"
    323. MinuteRecurrenceFrequency = "MINUTELY"
    324. HourRecurrenceFrequency = "HOURLY"
    325. DayRecurrenceFrequency = "DAILY"
    326. WeekRecurrenceFrequency = "WEEKLY"
    327. MonthRecurrenceFrequency = "MONTHLY"
    328. YearRecurrenceFrequency = "YEARLY"
    329. )
    330. // the frequency an event recurs
    331. type RecurrenceWeekday string
    332. const (
    333. MondayRecurrenceWeekday RecurrenceWeekday = "MO"
    334. TuesdayRecurrenceWeekday = "TU"
    335. WednesdayRecurrenceWeekday = "WE"
    336. ThursdayRecurrenceWeekday = "TH"
    337. FridayRecurrenceWeekday = "FR"
    338. SaturdayRecurrenceWeekday = "SA"
    339. SundayRecurrenceWeekday = "SU"
    340. )
    341. func WeekdayString(weekday time.Weekday) RecurrenceWeekday {
    342. var r RecurrenceWeekday
    343. switch weekday {
    344. case time.Sunday:
    345. r = SundayRecurrenceWeekday
    346. case time.Monday:
    347. r = MondayRecurrenceWeekday
    348. case time.Tuesday:
    349. r = TuesdayRecurrenceWeekday
    350. case time.Wednesday:
    351. r = WednesdayRecurrenceWeekday
    352. case time.Thursday:
    353. r = ThursdayRecurrenceWeekday
    354. case time.Friday:
    355. r = FridayRecurrenceWeekday
    356. case time.Saturday:
    357. return SaturdayRecurrenceWeekday
    358. }
    359. return r
    360. }
    361. func WeekdayInt(s string) time.Weekday {
    362. var r time.Weekday
    363. switch RecurrenceWeekday(s) {
    364. case SundayRecurrenceWeekday:
    365. r = time.Sunday
    366. case MondayRecurrenceWeekday:
    367. r = time.Monday
    368. case TuesdayRecurrenceWeekday:
    369. r = time.Tuesday
    370. case WednesdayRecurrenceWeekday:
    371. r = time.Wednesday
    372. case ThursdayRecurrenceWeekday:
    373. r = time.Thursday
    374. case FridayRecurrenceWeekday:
    375. r = time.Friday
    376. case SaturdayRecurrenceWeekday:
    377. r = time.Saturday
    378. }
    379. return r
    380. }
    381. func main() {
    382. GetWeekRuleRecurrenceTime()
    383. GetWeekRuleRecurrenceTime2()
    384. GetWeekRuleRecurrenceTime3()
    385. GetWeekRuleRecurrenceTime4()
    386. GetWeekRuleRecurrenceTime11()
    387. GetWeekRuleRecurrenceTime22()
    388. GetWeekRuleRecurrenceTime33()
    389. GetWeekRuleRecurrenceTime44()
    390. GetMonthRuleRecurrenceTime()
    391. GetMonthRuleRecurrenceTime2()
    392. GetMonthRuleRecurrenceTime3()
    393. GetMonthRuleRecurrenceTime4()
    394. GetMonthRuleRecurrenceTime11()
    395. GetMonthRuleRecurrenceTime22()
    396. GetMonthRuleRecurrenceTime33()
    397. GetMonthRuleRecurrenceTime44()
    398. }
    399. func GetWeekRuleRecurrenceTime() {
    400. start := time.Now()
    401. defer func() {
    402. cost := time.Since(start)
    403. fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
    404. }()
    405. fmt.Println("####################TestGetRecurrenceTimeWeek...")
    406. loc, _ := time.LoadLocation("Asia/Shanghai")
    407. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00 //周三
    408. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    409. recurr := TRecurrenceInfo{}
    410. recurr.Frequency = "weekly"
    411. recurr.Interval = 1
    412. recurr.ByDay = "TU,SU" // 星期二、日
    413. for i := 0; i <= 10; i++ {
    414. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    415. fmt.Println("start:", start, "end:", end)
    416. }
    417. }
    418. func GetWeekRuleRecurrenceTime2() {
    419. start := time.Now()
    420. defer func() {
    421. cost := time.Since(start)
    422. fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
    423. }()
    424. fmt.Println("####################TestGetRecurrenceTimeWeek2...")
    425. loc, _ := time.LoadLocation("Asia/Shanghai")
    426. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00 //周三
    427. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    428. recurr := TRecurrenceInfo{}
    429. recurr.Frequency = "weekly"
    430. recurr.Interval = 1
    431. recurr.ByDay = "TU,WE,SU" // 星期二、三、日
    432. for i := 0; i <= 10; i++ {
    433. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    434. fmt.Println("start:", start, "end:", end)
    435. }
    436. }
    437. func GetWeekRuleRecurrenceTime3() {
    438. start := time.Now()
    439. defer func() {
    440. cost := time.Since(start)
    441. fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
    442. }()
    443. fmt.Println("####################TestGetRecurrenceTimeWeek3...")
    444. loc, _ := time.LoadLocation("Asia/Shanghai")
    445. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00 //周三
    446. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    447. recurr := TRecurrenceInfo{}
    448. recurr.Frequency = "weekly"
    449. recurr.Interval = 1
    450. recurr.ByDay = "MO,TU" // 星期一、二
    451. for i := 0; i <= 10; i++ {
    452. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    453. fmt.Println("start:", start, "end:", end)
    454. }
    455. }
    456. func GetWeekRuleRecurrenceTime4() {
    457. start := time.Now()
    458. defer func() {
    459. cost := time.Since(start)
    460. fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
    461. }()
    462. fmt.Println("####################TestGetRecurrenceTimeWeek4...")
    463. loc, _ := time.LoadLocation("Asia/Shanghai")
    464. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00 //周三
    465. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    466. recurr := TRecurrenceInfo{}
    467. recurr.Frequency = "weekly"
    468. recurr.Interval = 1
    469. recurr.ByDay = "WE" // 星期三
    470. for i := 0; i <= 10; i++ {
    471. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    472. fmt.Println("start:", start, "end:", end)
    473. }
    474. }
    475. func GetWeekRuleRecurrenceTime11() {
    476. start := time.Now()
    477. defer func() {
    478. cost := time.Since(start)
    479. fmt.Println("TestGetRecurrenceTimeWeek11 cost=", cost)
    480. }()
    481. fmt.Println("####################TestGetRecurrenceTimeWeek11...")
    482. loc, _ := time.LoadLocation("Asia/Shanghai")
    483. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00 //周三
    484. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    485. recurr := TRecurrenceInfo{}
    486. recurr.Frequency = "weekly"
    487. recurr.Interval = 2
    488. recurr.ByDay = "TU,SU" // 星期二、日
    489. for i := 0; i <= 10; i++ {
    490. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    491. fmt.Println("start:", start, "end:", end)
    492. }
    493. }
    494. func GetWeekRuleRecurrenceTime22() {
    495. start := time.Now()
    496. defer func() {
    497. cost := time.Since(start)
    498. fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
    499. }()
    500. fmt.Println("####################TestGetRecurrenceTimeWeek22...")
    501. loc, _ := time.LoadLocation("Asia/Shanghai")
    502. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00 //周三
    503. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    504. recurr := TRecurrenceInfo{}
    505. recurr.Frequency = "weekly"
    506. recurr.Interval = 2
    507. recurr.ByDay = "TU,WE,SU" // 星期二、三、日
    508. for i := 0; i <= 10; i++ {
    509. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    510. fmt.Println("start:", start, "end:", end)
    511. }
    512. }
    513. func GetWeekRuleRecurrenceTime33() {
    514. start := time.Now()
    515. defer func() {
    516. cost := time.Since(start)
    517. fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
    518. }()
    519. fmt.Println("####################TestGetRecurrenceTimeWeek33...")
    520. loc, _ := time.LoadLocation("Asia/Shanghai")
    521. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00 //周三
    522. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    523. recurr := TRecurrenceInfo{}
    524. recurr.Frequency = "weekly"
    525. recurr.Interval = 2
    526. recurr.ByDay = "MO,TU" // 星期一、二
    527. for i := 0; i <= 10; i++ {
    528. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    529. fmt.Println("start:", start, "end:", end)
    530. }
    531. }
    532. func GetWeekRuleRecurrenceTime44() {
    533. start := time.Now()
    534. defer func() {
    535. cost := time.Since(start)
    536. fmt.Println("TestGetRecurrenceTimeWeek cost=", cost)
    537. }()
    538. fmt.Println("####################TestGetRecurrenceTimeWeek44...")
    539. loc, _ := time.LoadLocation("Asia/Shanghai")
    540. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00 //周三
    541. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    542. recurr := TRecurrenceInfo{}
    543. recurr.Frequency = "weekly"
    544. recurr.Interval = 2
    545. recurr.ByDay = "WE" // 星期三
    546. for i := 0; i <= 10; i++ {
    547. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    548. fmt.Println("start:", start, "end:", end)
    549. }
    550. }
    551. func GetMonthRuleRecurrenceTime() {
    552. start := time.Now()
    553. defer func() {
    554. cost := time.Since(start)
    555. fmt.Println("TestGetRecurrenceTimeMonth cost=", cost)
    556. }()
    557. fmt.Println("####################TestGetRecurrenceTimeMonth1...")
    558. loc, _ := time.LoadLocation("Asia/Shanghai")
    559. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
    560. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    561. recurr := TRecurrenceInfo{}
    562. recurr.Frequency = "monthly"
    563. recurr.Interval = 1
    564. recurr.ByMonthDay = "2,3,4,5,6"
    565. for i := 0; i <= 10; i++ {
    566. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    567. fmt.Println("start:", start, "end:", end)
    568. }
    569. }
    570. func GetMonthRuleRecurrenceTime2() {
    571. start := time.Now()
    572. defer func() {
    573. cost := time.Since(start)
    574. fmt.Println("TestGetRecurrenceTimeMonth2 cost=", cost)
    575. }()
    576. fmt.Println("####################TestGetRecurrenceTimeMonth2...")
    577. loc, _ := time.LoadLocation("Asia/Shanghai")
    578. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
    579. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    580. recurr := TRecurrenceInfo{}
    581. recurr.Frequency = "monthly"
    582. recurr.Interval = 1
    583. recurr.ByMonthDay = "2,3,4,5,6,25,26,28"
    584. for i := 0; i <= 10; i++ {
    585. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    586. fmt.Println("start:", start, "end:", end)
    587. }
    588. }
    589. func GetMonthRuleRecurrenceTime3() {
    590. start := time.Now()
    591. defer func() {
    592. cost := time.Since(start)
    593. fmt.Println("TestGetRecurrenceTimeMonth3 cost=", cost)
    594. }()
    595. fmt.Println("####################TestGetRecurrenceTimeMonth3...")
    596. loc, _ := time.LoadLocation("Asia/Shanghai")
    597. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
    598. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    599. recurr := TRecurrenceInfo{}
    600. recurr.Frequency = "monthly"
    601. recurr.Interval = 1
    602. recurr.ByMonthDay = "2,3,4,5,6,24,25,26,28"
    603. for i := 0; i <= 10; i++ {
    604. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    605. fmt.Println("start:", start, "end:", end)
    606. }
    607. }
    608. func GetMonthRuleRecurrenceTime4() {
    609. start := time.Now()
    610. defer func() {
    611. cost := time.Since(start)
    612. fmt.Println("TestGetRecurrenceTimeMonth4 cost=", cost)
    613. }()
    614. fmt.Println("####################TestGetRecurrenceTimeMonth4...")
    615. loc, _ := time.LoadLocation("Asia/Shanghai")
    616. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
    617. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    618. recurr := TRecurrenceInfo{}
    619. recurr.Frequency = "monthly"
    620. recurr.Interval = 1
    621. recurr.ByMonthDay = "24"
    622. for i := 0; i <= 10; i++ {
    623. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    624. fmt.Println("start:", start, "end:", end)
    625. }
    626. }
    627. func GetMonthRuleRecurrenceTime11() {
    628. start := time.Now()
    629. defer func() {
    630. cost := time.Since(start)
    631. fmt.Println("TestGetRecurrenceTimeMonth cost=", cost)
    632. }()
    633. fmt.Println("####################TestGetRecurrenceTimeMonth11...")
    634. loc, _ := time.LoadLocation("Asia/Shanghai")
    635. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
    636. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    637. recurr := TRecurrenceInfo{}
    638. recurr.Frequency = "monthly"
    639. recurr.Interval = 2
    640. recurr.ByMonthDay = "2,3,4,5,6"
    641. for i := 0; i <= 10; i++ {
    642. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    643. fmt.Println("start:", start, "end:", end)
    644. }
    645. }
    646. func GetMonthRuleRecurrenceTime22() {
    647. start := time.Now()
    648. defer func() {
    649. cost := time.Since(start)
    650. fmt.Println("TestGetRecurrenceTimeMonth2 cost=", cost)
    651. }()
    652. fmt.Println("####################TestGetRecurrenceTimeMonth22...")
    653. loc, _ := time.LoadLocation("Asia/Shanghai")
    654. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
    655. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    656. recurr := TRecurrenceInfo{}
    657. recurr.Frequency = "monthly"
    658. recurr.Interval = 2
    659. recurr.ByMonthDay = "2,3,4,5,6,25,26,28"
    660. for i := 0; i <= 10; i++ {
    661. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    662. fmt.Println("start:", start, "end:", end)
    663. }
    664. }
    665. func GetMonthRuleRecurrenceTime33() {
    666. start := time.Now()
    667. defer func() {
    668. cost := time.Since(start)
    669. fmt.Println("TestGetRecurrenceTimeMonth3 cost=", cost)
    670. }()
    671. fmt.Println("####################TestGetRecurrenceTimeMonth3...")
    672. loc, _ := time.LoadLocation("Asia/Shanghai")
    673. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
    674. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    675. recurr := TRecurrenceInfo{}
    676. recurr.Frequency = "monthly"
    677. recurr.Interval = 2
    678. recurr.ByMonthDay = "2,3,4,5,6,24,25,26,28"
    679. for i := 0; i <= 10; i++ {
    680. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    681. fmt.Println("start:", start, "end:", end)
    682. }
    683. }
    684. func GetMonthRuleRecurrenceTime44() {
    685. start := time.Now()
    686. defer func() {
    687. cost := time.Since(start)
    688. fmt.Println("TestGetRecurrenceTimeMonth4 cost=", cost)
    689. }()
    690. fmt.Println("####################TestGetRecurrenceTimeMonth44...")
    691. loc, _ := time.LoadLocation("Asia/Shanghai")
    692. fstStartTime := int64(1661331600) // 2022-08-24 17:00:00
    693. fstEndTime := int64(1661335200) // 2022-08-24 18:00:00
    694. recurr := TRecurrenceInfo{}
    695. recurr.Frequency = "monthly"
    696. recurr.Interval = 2
    697. recurr.ByMonthDay = "24"
    698. for i := 0; i <= 10; i++ {
    699. start, end := recurr.GetRecurrenceTime(int64(i), fstStartTime, fstEndTime, loc)
    700. fmt.Println("start:", start, "end:", end)
    701. }
    702. }

  • 相关阅读:
    STM32笔记
    arima模型python代码
    RNN/LSTM学习记录
    中国人民大学与加拿大女王大学金融硕士——人生总要逼自己一把
    axios和SpringMVC数据交互(一维二维数组,JSON/form形式,@RequestBody/@RequestParam)
    PDF头部报错:Evaluation Warning : The document was created with Spire.PDF for Java.
    九章云极DataCanvas公司完成D1轮融资
    矩阵论复习提纲
    Spring——Bean管理XML方式
    什么样的企业最需要MES?
  • 原文地址:https://blog.csdn.net/ezreal_pan/article/details/126508646