-
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 #19 from jmillerv/14-convert-time-slot-to-a-config
14 convert time slot to a config
- Loading branch information
Showing
11 changed files
with
203 additions
and
93 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 was deleted.
Oops, something went wrong.
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,46 @@ | ||
package content | ||
|
||
import ( | ||
"github.com/araddon/dateparse" | ||
log "github.com/sirupsen/logrus" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// Times represents timeslots and are parsed in a 24hour format | ||
type Timeslot struct { | ||
Begin string | ||
End string | ||
} | ||
|
||
// IsScheduledNow checks the current time and returns a bool if the time falls within the range | ||
func (t *Timeslot) IsScheduledNow(current time.Time) bool { | ||
// get date info for string | ||
date := time.Date(current.Year(), current.Month(), current.Day(), 0, 0, 0, 0, time.Local) | ||
year, month, day := date.Date() | ||
|
||
// convert ints to dateString | ||
dateString := strconv.Itoa(year) + "-" + strconv.Itoa(int(month)) + "-" + strconv.Itoa(day) | ||
|
||
// parse the date and the config time | ||
// parsed times are returned in 2022-12-05 15:05:00 +0000 UTC format which doesn't appear to have a const in the time package | ||
parsedStartTime, _ := dateparse.ParseAny(dateString + " " + t.Begin) | ||
parsedEndTime, _ := dateparse.ParseAny(dateString + " " + t.End) | ||
|
||
// matched parse time to fixed zone time | ||
startTime := time.Date(parsedStartTime.Year(), parsedStartTime.Month(), parsedStartTime.Day(), parsedStartTime.Hour(), parsedStartTime.Minute(), parsedStartTime.Second(), parsedStartTime.Nanosecond(), time.Local) | ||
endTime := time.Date(parsedEndTime.Year(), parsedEndTime.Month(), parsedEndTime.Day(), parsedEndTime.Hour(), parsedEndTime.Minute(), parsedEndTime.Second(), parsedEndTime.Nanosecond(), time.Local) | ||
|
||
return inTimeSpan(startTime, endTime, current) | ||
} | ||
|
||
func inTimeSpan(start, end, current time.Time) bool { | ||
log.WithField("start", start).WithField("current", current).WithField("end", end).Info("timeslot::inTimeSpan: configured times") | ||
// handle scheduling that traverses days. | ||
tz, _ := time.LoadLocation("UTC") | ||
if end.Before(start) && current.After(start) { | ||
return true | ||
} | ||
|
||
return current.In(tz).After(start) && current.In(tz).Before(end) | ||
} |
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,73 @@ | ||
package content | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type TimeProvider interface { | ||
Now() time.Time | ||
} | ||
|
||
type testTime struct { | ||
TimeProvider | ||
} | ||
|
||
func (testTime *testTime) Now() time.Time { | ||
tz, _ := time.LoadLocation("EST") | ||
now := time.Date(2022, 12, 05, 23, 27, 0, 0, tz) | ||
log.Infof("testTime %v", now) | ||
return now | ||
} | ||
|
||
func TestTimes_IsScheduledNow(t *testing.T) { | ||
t.Parallel() | ||
type fields struct { | ||
Current time.Time | ||
Begin string | ||
End string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want bool | ||
}{ | ||
{ | ||
name: "Returns True", | ||
fields: fields{ | ||
Begin: "11:00 PM", | ||
End: "11:59 PM", | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "Returns False", | ||
fields: fields{ | ||
Begin: "11:28 PM", | ||
End: "10:47 PM", | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Success: evaluates true for times that traverse days", | ||
fields: fields{ | ||
Begin: "11:00 PM", | ||
End: "2:30 AM", | ||
}, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t1 *testing.T) { | ||
t1.Parallel() | ||
t := &Timeslot{ | ||
Begin: tt.fields.Begin, | ||
End: tt.fields.End, | ||
} | ||
assert.Equalf(t1, tt.want, t.IsScheduledNow((&testTime{}).Now()), "IsScheduledNow()") | ||
}) | ||
} | ||
} |
Oops, something went wrong.