From aa34dae308fe75aeee1efa0c21118872fb52b60c Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 1 Feb 2018 01:05:23 +0100 Subject: [PATCH] Move the time check function down, make it more readable --- dnscrypt-proxy/plugin_block_name.go | 39 ++++++++++++++++------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/dnscrypt-proxy/plugin_block_name.go b/dnscrypt-proxy/plugin_block_name.go index 7f38dd6d..01af7ba6 100644 --- a/dnscrypt-proxy/plugin_block_name.go +++ b/dnscrypt-proxy/plugin_block_name.go @@ -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 +}