public_host 4 years ago
parent
commit
391158aa6a
  1. 103
      core.go
  2. 236
      core_test.go

103
core.go

@ -12,9 +12,12 @@ import (
type TimeArrowType string
const (
TimeArrowTypeDayOfWeek TimeArrowType = "DAY_OF_WEEK"
TimeArrowTypeDayOfMonth TimeArrowType = "DAY_OF_MONTH"
TimeArrowTypeDateSlice TimeArrowType = "DATE_SLICE"
TimeArrowTypeDayOfWeek TimeArrowType = "DAY_OF_WEEK"
TimeArrowTypeDayOfMonth TimeArrowType = "DAY_OF_MONTH"
TimeArrowTypeDateSlice TimeArrowType = "DATE_SLICE"
TimeArrowTypeInHolidays TimeArrowType = "IN_HOLIDAYS" //节假日
TimeArrowTypeAtLastDayHolidays TimeArrowType = "AT_LAST_DAY_HOLIDAYS" //节假日最后一天
TimeArrowTypePriorToDayHolidays TimeArrowType = "PRIOR_TO_DAY_HOLIDAYS" //节假日前一天
)
type DateSlice struct {
@ -151,6 +154,73 @@ func isInTimeOfDay(t time.Time, ta TimeArrow) bool {
return false
}
func isInHolidays(t time.Time) bool {
holidaysData, err := GetHolidaysData()
if err != nil {
return false
}
for e := range holidaysData {
startTime := holidaysData[e].Start.Local()
endTime := holidaysData[e].End.Local()
if (t.After(startTime) || t.Equal(startTime)) && t.Before(endTime) {
return true
}
}
return false
}
func isInAtLastDayHolidays(t time.Time) bool {
t = t.Local()
holidaysData, err := GetHolidaysData()
if err != nil {
return false
}
for e := range holidaysData {
startTime, err := time.ParseInLocation("2006-01-02 15:04:05", holidaysData[e].End.Format("2006-01-02 00:00:00"), time.Local)
if err != nil {
log.Println("节假日最后一天时间段开始时间格式错误", holidaysData[e].End)
continue
}
endTime, err := time.ParseInLocation("2006-01-02 15:04:05", fmt.Sprintf("%s 23:59:59", holidaysData[e].End.Format("2006-01-02")), time.Local)
if err != nil {
log.Println("节假日最后一天时间段结束时间格式错误", holidaysData[e].End)
continue
}
if (t.After(startTime) || t.Equal(startTime)) && t.Before(endTime) {
return true
}
}
return false
}
func isInPriorToDayHolidays(t time.Time) bool {
holidaysData, err := GetHolidaysData()
if err != nil {
return false
}
for e := range holidaysData {
startTime, err := time.ParseInLocation("2006-01-02 15:04:05", holidaysData[e].Start.AddDate(0, 0, -1).Format("2006-01-02 00:00:00"), time.Local)
if err != nil {
log.Println("节假日前一天时间段开始时间格式错误", holidaysData[e].Start)
continue
}
endTime, err := time.ParseInLocation("2006-01-02 15:04:05", fmt.Sprintf("%s 23:59:59", holidaysData[e].Start.AddDate(0, 0, -1).Format("2006-01-02")), time.Local)
if err != nil {
log.Println("节假日前一天时间段结束时间格式错误", holidaysData[e].Start)
continue
}
if (t.After(startTime) || t.Equal(startTime)) && t.Before(endTime) {
return true
}
}
return false
}
func isInExpandTags(ta TimeArrow, expandTag string) bool {
if len(ta.ExpandTags) == 0 {
return true
@ -211,6 +281,14 @@ func CreateDateSliceTypePlan(group string, dateSlice []DateSlice, timesOnDay []s
}
}
type HolidaysItem struct {
Name string `json:"name"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
}
var GetHolidaysData func() ([]HolidaysItem, error)
type TimeArrowHelper struct {
GetData func(group string) (TimeArrows, error)
}
@ -247,13 +325,30 @@ func (th *TimeArrowHelper) GetHitTimeArrow(t time.Time, group string, expandTags
return &ta[e], nil
}
break
case TimeArrowTypeDateSlice:
//一月中某一天是否判定
if isInDateSlice(t, ta[e]) {
return &ta[e], nil
}
break
case TimeArrowTypeAtLastDayHolidays:
//节假日最后一天
if isInAtLastDayHolidays(t) {
return &ta[e], nil
}
break
case TimeArrowTypePriorToDayHolidays:
//节假日前一天
if isInPriorToDayHolidays(t) {
return &ta[e], nil
}
break
case TimeArrowTypeInHolidays:
//节假日
if isInHolidays(t) {
return &ta[e], nil
}
break
default:
log.Println("类型错误:", ta[e].Type)
}

236
core_test.go

@ -1,6 +1,7 @@
package time_arrow
import (
"reflect"
"testing"
"time"
)
@ -310,3 +311,238 @@ func TestGetHitTimeArrow(t *testing.T) {
}
}
func Test_isInDayOfMonth(t *testing.T) {
type args struct {
t time.Time
ta TimeArrow
}
tests := []struct {
name string
args args
want bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isInDayOfMonth(tt.args.t, tt.args.ta); got != tt.want {
t.Errorf("isInDayOfMonth() = %v, want %v", got, tt.want)
}
})
}
}
func Test_isInHolidays(t *testing.T) {
GetHolidaysData = func() (items []HolidaysItem, e error) {
start, _ := time.Parse("2006-01-02 15:04:05", "2020-10-01 00:00:00")
end, _ := time.Parse("2006-01-02 15:04:05", "2020-10-07 23:59:59")
return []HolidaysItem{
{
Name: "国庆节",
Start: start,
End: end,
},
}, nil
}
tt, _ := time.Parse("2006-01-02 15:04:05", "2020-10-03 11:00:00")
if !isInHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-08 11:00:00")
if isInHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-08 11:00:00")
if isInHolidays(tt) {
t.Fatal("error")
}
}
func Test_isInAtLastDayHolidays(t *testing.T) {
GetHolidaysData = func() (items []HolidaysItem, e error) {
start, _ := time.Parse("2006-01-02 15:04:05", "2020-10-01 00:00:00")
end, _ := time.Parse("2006-01-02 15:04:05", "2020-10-07 23:59:59")
return []HolidaysItem{
{
Name: "国庆节",
Start: start,
End: end,
},
}, nil
}
tt, _ := time.Parse("2006-01-02 15:04:05", "2020-10-03 11:00:00")
if isInAtLastDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-08 11:00:00")
if isInAtLastDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-08 11:00:00")
if isInAtLastDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-07 11:00:00")
if !isInAtLastDayHolidays(tt) {
t.Fatal("error")
}
}
func Test_isInPriorToDayHolidays(t *testing.T) {
GetHolidaysData = func() (items []HolidaysItem, e error) {
start, _ := time.Parse("2006-01-02 15:04:05", "2020-10-01 00:00:00")
end, _ := time.Parse("2006-01-02 15:04:05", "2020-10-07 23:59:59")
return []HolidaysItem{
{
Name: "国庆节",
Start: start,
End: end,
},
}, nil
}
tt, _ := time.Parse("2006-01-02 15:04:05", "2020-10-03 11:00:00")
if isInPriorToDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-10-08 11:00:00")
if isInPriorToDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-08 11:00:00")
if isInPriorToDayHolidays(tt) {
t.Fatal("error")
}
tt, _ = time.Parse("2006-01-02 15:04:05", "2020-09-30 11:00:00")
if !isInPriorToDayHolidays(tt) {
t.Fatal("error")
}
}
func Test_createUUID(t *testing.T) {
tests := []struct {
name string
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := createUUID(); got != tt.want {
t.Errorf("createUUID() = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateDayOfWeekTypePlan(t *testing.T) {
type args struct {
group string
dayOfWeek []int
timesOnDay []string
expandValue interface{}
expandTags []string
weights float64
}
tests := []struct {
name string
args args
want TimeArrow
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CreateDayOfWeekTypePlan(tt.args.group, tt.args.dayOfWeek, tt.args.timesOnDay, tt.args.expandValue, tt.args.expandTags, tt.args.weights); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateDayOfWeekTypePlan() = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateDayOfMonthTypePlan(t *testing.T) {
type args struct {
group string
dayOfMonth []int
timesOnDay []string
expandValue interface{}
expandTags []string
weights float64
}
tests := []struct {
name string
args args
want TimeArrow
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CreateDayOfMonthTypePlan(tt.args.group, tt.args.dayOfMonth, tt.args.timesOnDay, tt.args.expandValue, tt.args.expandTags, tt.args.weights); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateDayOfMonthTypePlan() = %v, want %v", got, tt.want)
}
})
}
}
func TestCreateDateSliceTypePlan(t *testing.T) {
type args struct {
group string
dateSlice []DateSlice
timesOnDay []string
expandValue interface{}
expandTags []string
weights float64
}
tests := []struct {
name string
args args
want TimeArrow
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CreateDateSliceTypePlan(tt.args.group, tt.args.dateSlice, tt.args.timesOnDay, tt.args.expandValue, tt.args.expandTags, tt.args.weights); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateDateSliceTypePlan() = %v, want %v", got, tt.want)
}
})
}
}
func TestTimeArrowHelper_GetHitTimeArrow(t *testing.T) {
type fields struct {
GetData func(group string) (TimeArrows, error)
}
type args struct {
t time.Time
group string
expandTags []string
}
tests := []struct {
name string
fields fields
args args
want *TimeArrow
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := &TimeArrowHelper{
GetData: tt.fields.GetData,
}
got, err := th.GetHitTimeArrow(tt.args.t, tt.args.group, tt.args.expandTags...)
if (err != nil) != tt.wantErr {
t.Errorf("TimeArrowHelper.GetHitTimeArrow() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TimeArrowHelper.GetHitTimeArrow() = %v, want %v", got, tt.want)
}
})
}
}
Loading…
Cancel
Save