From 4402e8c954b535de98de8258b73c42cc0ee1be04 Mon Sep 17 00:00:00 2001 From: u Date: Fri, 27 Aug 2021 13:35:54 +0800 Subject: [PATCH] fix --- core.go | 89 +++++++++++++++++++++++++++++++++++----------------- core_test.go | 100 ++++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 139 insertions(+), 50 deletions(-) diff --git a/core.go b/core.go index 432d577..b6a6261 100644 --- a/core.go +++ b/core.go @@ -112,6 +112,11 @@ func timeCompletion(a string) string { for len(sc) < 3 { sc = append(sc, "00") } + for i := range sc { + if len(sc[i]) == 1 { + sc[i] = "0" + sc[i] + } + } ret := strings.Join(sc, ":") timeCompletionCache.Store(a, ret) return ret @@ -127,16 +132,56 @@ func isInDayOfMonth(t time.Time, ta TimeArrow) bool { return false } +var inTimeOfDayCache sync.Map + func isInTimeOfDay(t time.Time, ta TimeArrow) bool { + if len(ta.TimesOnDay) == 0 { + return true + } + + tfdate := t.Format("15:04:05") + + cacheKey := fmt.Sprint(tfdate, strings.Join(ta.TimesOnDay, "|")) + ret, ok := inTimeOfDayCache.Load(cacheKey) + if ok { + return ret.(bool) + } + + for k := range ta.TimesOnDay { + tsp := strings.Split(ta.TimesOnDay[k], "-") + if len(tsp) < 2 { + log.Println("必须为时间段:", ta.TimesOnDay[k]) + continue + } + start, end := tsp[0], tsp[1] + start = timeCompletion(start) + end = timeCompletion(end) + fmt.Println("ssee", start, end, tfdate) + if start <= tfdate && tfdate < end { + inTimeOfDayCache.Store(cacheKey, true) + return true + } + } + inTimeOfDayCache.Store(cacheKey, false) + return false +} +func isInTimeOfDay2(t time.Time, ta TimeArrow) bool { if len(ta.TimesOnDay) == 0 { return true } + t, _ = time.ParseInLocation("2006-01-02 15:04:05", "2011-01-01 "+t.Format("15:04:05"), time.Local) + tfdate := t.Format("2006-01-02") - parseInLocationCache := map[string]time.Time{} - var err error + cacheKey := fmt.Sprint(t.Format("15:04:05"), strings.Join(ta.TimesOnDay, "|")) + ret, ok := inTimeOfDayCache.Load(cacheKey) + if ok { + return ret.(bool) + } + + // var err error for k := range ta.TimesOnDay { tsp := strings.Split(ta.TimesOnDay[k], "-") if len(tsp) < 2 { @@ -149,20 +194,14 @@ func isInTimeOfDay(t time.Time, ta TimeArrow) bool { start = timeCompletion(start) end = timeCompletion(end) + //tfdate为当天日期 startTimeKey := fmt.Sprintf("%s %s", tfdate, start) - startTime, ok := parseInLocationCache[startTimeKey] - if !ok { - startTime, err = time.ParseInLocation("2006-01-02 15:04:05", startTimeKey, time.Local) - parseInLocationCache[startTimeKey] = startTime - if err != nil { - log.Println("时间段开始时间格式错误", start) - continue - } - } else { - if startTime.IsZero() { - log.Println("时间段开始时间格式错误", start) - continue - } + + startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startTimeKey, time.Local) + + if startTime.IsZero() { + log.Println("时间段开始时间格式错误", start) + continue } if end == "24" || end == "24:00" || end == "24:00:00" { @@ -170,19 +209,11 @@ func isInTimeOfDay(t time.Time, ta TimeArrow) bool { } endTimeKey := fmt.Sprintf("%s %s", tfdate, end) - endTime, ok := parseInLocationCache[endTimeKey] - if !ok { - endTime, err = time.ParseInLocation("2006-01-02 15:04:05", endTimeKey, time.Local) - parseInLocationCache[endTimeKey] = endTime - if err != nil { - log.Println("时间段结束时间格式错误", end) - continue - } - } else { - if endTime.IsZero() { - log.Println("时间段结束时间格式错误", end) - continue - } + + endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endTimeKey, time.Local) + if endTime.IsZero() { + log.Println("时间段结束时间格式错误", end) + continue } if startTime.Unix() > endTime.Unix() { @@ -191,9 +222,11 @@ func isInTimeOfDay(t time.Time, ta TimeArrow) bool { } if (t.After(startTime) || t.Equal(startTime)) && t.Before(endTime) { + inTimeOfDayCache.Store(cacheKey, true) return true } } + inTimeOfDayCache.Store(cacheKey, false) return false } diff --git a/core_test.go b/core_test.go index 57fc976..b87619b 100644 --- a/core_test.go +++ b/core_test.go @@ -1,6 +1,8 @@ package time_arrow import ( + "fmt" + "math/rand" "reflect" "testing" "time" @@ -46,28 +48,82 @@ func Test_timeCompletion(t *testing.T) { } func Test_isInTimeOfDay(t *testing.T) { - ti, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-06-08 17:18:00", time.Local) - if err != nil { - panic(err) - } - ok := isInTimeOfDay(ti, TimeArrow{ - TimesOnDay: []string{ - "1:21", - "1:21-2:22", - "17:00-18:00", - }, - }) - if !ok { - t.Fatal("error") - } - ok = isInTimeOfDay(ti, TimeArrow{ - TimesOnDay: []string{ - "17:00-17:10", - "18:00-19:00", - }, - }) - if ok { - t.Fatal("error") + rand.Seed(time.Now().Unix()) + for k := 0; k < 10; k++ { + rt := rand.Int() % 24 + rm := rand.Int() % 60 + ti, err := time.ParseInLocation("2006-01-02 15:04:05", fmt.Sprintf("2020-06-08 %.2d:%.2d:00", rt, rm), time.Local) + if err != nil { + panic(err) + } + ok := isInTimeOfDay(ti, TimeArrow{ + TimesOnDay: []string{ + "1:21", + "1:21-2:22", + "17:00-18:00", + }, + }) + ok2 := isInTimeOfDay(ti, TimeArrow{ + TimesOnDay: []string{ + "1:21", + "1:21-2:22", + "17:00-18:00", + }, + }) + if ok != ok2 { + t.Fatal("error") + } + ok = isInTimeOfDay(ti, TimeArrow{ + TimesOnDay: []string{ + "17:00-17:10", + "18:00-19:00", + }, + }) + ok2 = isInTimeOfDay2(ti, TimeArrow{ + TimesOnDay: []string{ + "17:00-17:10", + "18:00-19:00", + }, + }) + if ok != ok2 { + t.Fatal("error") + } + + tss := []string{} + for i := 0; i < 24; i++ { + tss = append(tss, fmt.Sprintf("%d:00-%d:30", i, i)) + tss = append(tss, fmt.Sprintf("%d:30-%d:00", i, i+1)) + } + + ok = isInTimeOfDay(ti, TimeArrow{ + TimesOnDay: tss, + }) + + ok2 = isInTimeOfDay2(ti, TimeArrow{ + TimesOnDay: tss, + }) + if ok != ok2 { + t.Fatal("error") + } + + tss = []string{} + for i := 0; i < 24; i++ { + if i == 17 { + continue + } + tss = append(tss, fmt.Sprintf("%d:00-%d:30", i, i)) + tss = append(tss, fmt.Sprintf("%d:30-%d:00", i, i+1)) + } + + ok = isInTimeOfDay(ti, TimeArrow{ + TimesOnDay: tss, + }) + ok2 = isInTimeOfDay2(ti, TimeArrow{ + TimesOnDay: tss, + }) + if ok != ok2 { + t.Fatal("error") + } } }