Move the time check function down, make it more readable

This commit is contained in:
Frank Denis 2018-02-01 01:05:23 +01:00
parent 61592776e2
commit aa34dae308
1 changed files with 21 additions and 18 deletions

View File

@ -48,24 +48,6 @@ type WeeklyRanges struct {
ranges [7][]TimeRange
}
func (weeklyRanges *WeeklyRanges) Match() bool {
now := time.Now().Local()
day := now.Weekday()
weeklyRange := weeklyRanges.ranges[day]
if len(weeklyRange) == 0 {
return false
}
hour, min, _ := now.Clock()
nowX := (hour*60 + min) * 60
for _, timeRange := range weeklyRange {
if (timeRange.after > timeRange.before && (nowX >= timeRange.after || nowX <= timeRange.before)) ||
(nowX >= timeRange.after && nowX <= timeRange.before) {
return true
}
}
return false
}
type TimeRangeStr struct {
After string
Before string
@ -344,3 +326,24 @@ func ParseAllWeeklyRanges(allWeeklyRangesStr map[string]WeeklyRangesStr) (*map[s
}
return &allWeeklyRanges, nil
}
func (weeklyRanges *WeeklyRanges) Match() bool {
now := time.Now().Local()
day := now.Weekday()
weeklyRange := weeklyRanges.ranges[day]
if len(weeklyRange) == 0 {
return false
}
hour, min, _ := now.Clock()
nowX := (hour*60 + min) * 60
for _, timeRange := range weeklyRange {
if timeRange.after > timeRange.before {
if nowX >= timeRange.after || nowX <= timeRange.before {
return true
}
} else if nowX >= timeRange.after && nowX <= timeRange.before {
return true
}
}
return false
}