forked from kubereboot/kured
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jjjordanmsft/master
Merge upstream patch to add schedulability
- Loading branch information
Showing
8 changed files
with
322 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package timewindow | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var EveryDay = []string{"su", "mo", "tu", "we", "th", "fr", "sa"} | ||
|
||
// dayStrings maps day strings to time.Weekdays | ||
var dayStrings = map[string]time.Weekday{ | ||
"su": time.Sunday, | ||
"sun": time.Sunday, | ||
"sunday": time.Sunday, | ||
"mo": time.Monday, | ||
"mon": time.Monday, | ||
"monday": time.Monday, | ||
"tu": time.Tuesday, | ||
"tue": time.Tuesday, | ||
"tuesday": time.Tuesday, | ||
"we": time.Wednesday, | ||
"wed": time.Wednesday, | ||
"wednesday": time.Wednesday, | ||
"th": time.Thursday, | ||
"thu": time.Thursday, | ||
"thursday": time.Thursday, | ||
"fr": time.Friday, | ||
"fri": time.Friday, | ||
"friday": time.Friday, | ||
"sa": time.Saturday, | ||
"sat": time.Saturday, | ||
"saturday": time.Saturday, | ||
} | ||
|
||
type weekdays uint32 | ||
|
||
// parseWeekdays creates a set of weekdays from a string slice | ||
func parseWeekdays(days []string) (weekdays, error) { | ||
var result uint32 | ||
for _, day := range days { | ||
if len(day) == 0 { | ||
continue | ||
} | ||
|
||
weekday, err := parseWeekday(day) | ||
if err != nil { | ||
return weekdays(0), err | ||
} | ||
|
||
result |= 1 << uint32(weekday) | ||
} | ||
|
||
return weekdays(result), nil | ||
} | ||
|
||
// Contains returns true if the specified weekday is a member of this set. | ||
func (w weekdays) Contains(day time.Weekday) bool { | ||
return uint32(w)&(1<<uint32(day)) != 0 | ||
} | ||
|
||
// String returns a string representation of the set of weekdays. | ||
func (w weekdays) String() string { | ||
var b strings.Builder | ||
for i := uint32(0); i < 7; i++ { | ||
if uint32(w)&(1<<i) != 0 { | ||
b.WriteString(time.Weekday(i).String()[0:3]) | ||
} else { | ||
b.WriteString("---") | ||
} | ||
} | ||
|
||
return b.String() | ||
} | ||
|
||
func parseWeekday(day string) (time.Weekday, error) { | ||
if n, err := strconv.Atoi(day); err == nil { | ||
if n >= 0 && n < 7 { | ||
return time.Weekday(n), nil | ||
} else { | ||
return time.Sunday, fmt.Errorf("Invalid weekday, number out of range: %s", day) | ||
} | ||
} | ||
|
||
if weekday, ok := dayStrings[strings.ToLower(day)]; ok { | ||
return weekday, nil | ||
} else { | ||
return time.Sunday, fmt.Errorf("Invalid weekday: %s", day) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package timewindow | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestParseWeekdays(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
result string | ||
}{ | ||
{"0,4", "Sun---------Thu------"}, | ||
{"su,mo,tu", "SunMonTue------------"}, | ||
{"sunday,tu,thu", "Sun---Tue---Thu------"}, | ||
{"THURSDAY", "------------Thu------"}, | ||
{"we,WED,WeDnEsDaY", "---------Wed---------"}, | ||
{"", "---------------------"}, | ||
{",,,", "---------------------"}, | ||
} | ||
|
||
for _, tst := range tests { | ||
res, err := parseWeekdays(strings.Split(tst.input, ",")) | ||
if err != nil { | ||
t.Errorf("Received error for input %s: %v", tst.input, err) | ||
} else if res.String() != tst.result { | ||
t.Errorf("Test %s: Expected %s got %s", tst.input, tst.result, res.String()) | ||
} | ||
} | ||
} | ||
|
||
func TestParseWeekdaysErrors(t *testing.T) { | ||
tests := []string{ | ||
"15", | ||
"-8", | ||
"8", | ||
"mon,tue,wed,fridayyyy", | ||
} | ||
|
||
for _, tst := range tests { | ||
_, err := parseWeekdays(strings.Split(tst, ",")) | ||
if err == nil { | ||
t.Errorf("Expected to receive error for input %s", tst) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package timewindow | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// TimeWindow specifies a schedule of days and times. | ||
type TimeWindow struct { | ||
days weekdays | ||
location *time.Location | ||
startTime time.Time | ||
endTime time.Time | ||
} | ||
|
||
// New creates a TimeWindow instance based on string inputs specifying a schedule. | ||
func New(days []string, startTime, endTime, location string) (*TimeWindow, error) { | ||
tw := &TimeWindow{} | ||
|
||
var err error | ||
if tw.days, err = parseWeekdays(days); err != nil { | ||
return nil, err | ||
} | ||
|
||
if tw.location, err = time.LoadLocation(location); err != nil { | ||
return nil, err | ||
} | ||
|
||
if tw.startTime, err = parseTime(startTime, tw.location); err != nil { | ||
return nil, err | ||
} | ||
|
||
if tw.endTime, err = parseTime(endTime, tw.location); err != nil { | ||
return nil, err | ||
} | ||
|
||
return tw, nil | ||
} | ||
|
||
// Contains determines whether the specified time is within this time window. | ||
func (tw *TimeWindow) Contains(t time.Time) bool { | ||
loctime := t.In(tw.location) | ||
if !tw.days.Contains(loctime.Weekday()) { | ||
return false | ||
} | ||
|
||
start := time.Date(loctime.Year(), loctime.Month(), loctime.Day(), tw.startTime.Hour(), tw.startTime.Minute(), tw.startTime.Second(), 0, tw.location) | ||
end := time.Date(loctime.Year(), loctime.Month(), loctime.Day(), tw.endTime.Hour(), tw.endTime.Minute(), tw.endTime.Second(), 1e9-1, tw.location) | ||
|
||
return (loctime.After(start) || loctime.Equal(start)) && (loctime.Before(end) || loctime.Equal(end)) | ||
} | ||
|
||
// String returns a string representation of this time window. | ||
func (tw *TimeWindow) String() string { | ||
return fmt.Sprintf("%s between %02d:%02d and %02d:%02d %s", tw.days.String(), tw.startTime.Hour(), tw.startTime.Minute(), tw.endTime.Hour(), tw.endTime.Minute(), tw.location.String()) | ||
} | ||
|
||
// parseTime tries to parse a time with several formats. | ||
func parseTime(s string, loc *time.Location) (time.Time, error) { | ||
fmts := []string{"15:04", "15:04:05", "03:04pm", "15", "03pm", "3pm"} | ||
for _, f := range fmts { | ||
if t, err := time.ParseInLocation(f, s, loc); err == nil { | ||
return t, nil | ||
} | ||
} | ||
|
||
return time.Now(), fmt.Errorf("Invalid time format: %s", s) | ||
} |
Oops, something went wrong.