|
@ -1,6 +1,7 @@ |
|
|
package time_arrow |
|
|
package time_arrow |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"encoding/json" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"log" |
|
|
"log" |
|
|
"sort" |
|
|
"sort" |
|
@ -236,7 +237,7 @@ func isInTimeOfDay2(t time.Time, ta TimeArrow) bool { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func isInHolidays(t time.Time) bool { |
|
|
func isInHolidays(t time.Time) bool { |
|
|
holidaysData, err := GetHolidaysData() |
|
|
|
|
|
|
|
|
holidaysData, err := GetHolidaysDataWithCache() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
@ -253,7 +254,7 @@ func isInHolidays(t time.Time) bool { |
|
|
|
|
|
|
|
|
func isInAtLastDayHolidays(t time.Time) bool { |
|
|
func isInAtLastDayHolidays(t time.Time) bool { |
|
|
t = t.Local() |
|
|
t = t.Local() |
|
|
holidaysData, err := GetHolidaysData() |
|
|
|
|
|
|
|
|
holidaysData, err := GetHolidaysDataWithCache() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
@ -277,7 +278,7 @@ func isInAtLastDayHolidays(t time.Time) bool { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func isInPriorToDayHolidays(t time.Time) bool { |
|
|
func isInPriorToDayHolidays(t time.Time) bool { |
|
|
holidaysData, err := GetHolidaysData() |
|
|
|
|
|
|
|
|
holidaysData, err := GetHolidaysDataWithCache() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return false |
|
|
return false |
|
|
} |
|
|
} |
|
@ -320,7 +321,7 @@ func createUUID() string { |
|
|
return uid.String() |
|
|
return uid.String() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//CreateDayOfWeekTypePlan 创建一个每周计划
|
|
|
|
|
|
|
|
|
// CreateDayOfWeekTypePlan 创建一个每周计划
|
|
|
func CreateDayOfWeekTypePlan(group string, dayOfWeek []int, timesOnDay []string, expandValue interface{}, expandTags []string, weights float64) TimeArrow { |
|
|
func CreateDayOfWeekTypePlan(group string, dayOfWeek []int, timesOnDay []string, expandValue interface{}, expandTags []string, weights float64) TimeArrow { |
|
|
return TimeArrow{ |
|
|
return TimeArrow{ |
|
|
TimeArrowId: createUUID(), |
|
|
TimeArrowId: createUUID(), |
|
@ -334,7 +335,7 @@ func CreateDayOfWeekTypePlan(group string, dayOfWeek []int, timesOnDay []string, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//CreateDayOfMonthTypePlan 创建一个每月计划
|
|
|
|
|
|
|
|
|
// CreateDayOfMonthTypePlan 创建一个每月计划
|
|
|
func CreateDayOfMonthTypePlan(group string, dayOfMonth []int, timesOnDay []string, expandValue interface{}, expandTags []string, weights float64) TimeArrow { |
|
|
func CreateDayOfMonthTypePlan(group string, dayOfMonth []int, timesOnDay []string, expandValue interface{}, expandTags []string, weights float64) TimeArrow { |
|
|
return TimeArrow{ |
|
|
return TimeArrow{ |
|
|
TimeArrowId: createUUID(), |
|
|
TimeArrowId: createUUID(), |
|
@ -348,7 +349,7 @@ func CreateDayOfMonthTypePlan(group string, dayOfMonth []int, timesOnDay []strin |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//CreateDateSliceTypePlan 创建一个时间段计划
|
|
|
|
|
|
|
|
|
// CreateDateSliceTypePlan 创建一个时间段计划
|
|
|
func CreateDateSliceTypePlan(group string, dateSlice []DateSlice, timesOnDay []string, expandValue interface{}, expandTags []string, weights float64) TimeArrow { |
|
|
func CreateDateSliceTypePlan(group string, dateSlice []DateSlice, timesOnDay []string, expandValue interface{}, expandTags []string, weights float64) TimeArrow { |
|
|
return TimeArrow{ |
|
|
return TimeArrow{ |
|
|
TimeArrowId: createUUID(), |
|
|
TimeArrowId: createUUID(), |
|
@ -368,8 +369,32 @@ type HolidaysItem struct { |
|
|
End time.Time `json:"end"` |
|
|
End time.Time `json:"end"` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func jsonEncode(v interface{}) string { |
|
|
|
|
|
b, _ := json.Marshal(v) |
|
|
|
|
|
return string(b) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
var GetHolidaysData func() ([]HolidaysItem, error) |
|
|
var GetHolidaysData func() ([]HolidaysItem, error) |
|
|
|
|
|
|
|
|
|
|
|
var holidayCache *cache |
|
|
|
|
|
var holidayCacheLock sync.Mutex |
|
|
|
|
|
|
|
|
|
|
|
func GetHolidaysDataWithCache() ([]HolidaysItem, error) { |
|
|
|
|
|
holidayCacheLock.Lock() |
|
|
|
|
|
defer holidayCacheLock.Unlock() |
|
|
|
|
|
if holidayCache.IsExpired() { |
|
|
|
|
|
data, err := GetHolidaysData() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
holidayCache.SetData(jsonEncode(data), time.Second*60) |
|
|
|
|
|
} |
|
|
|
|
|
data := holidayCache.GetData() |
|
|
|
|
|
var result []HolidaysItem |
|
|
|
|
|
err := json.Unmarshal([]byte(data), &result) |
|
|
|
|
|
return result, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
type TimeArrowHelper struct { |
|
|
type TimeArrowHelper struct { |
|
|
GetData func(group string) (TimeArrows, error) |
|
|
GetData func(group string) (TimeArrows, error) |
|
|
} |
|
|
} |
|
|