changeset 5296:b3b990811f2c

Moved Dusk and Dawn functions to common time functions.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 29 Jul 2020 11:49:26 +0200
parents 3872f3bd1a08
children 71f73df1356d
files pkg/common/time.go pkg/controllers/fwa.go
diffstat 2 files changed, 25 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/common/time.go	Thu Jul 09 17:26:53 2020 +0200
+++ b/pkg/common/time.go	Wed Jul 29 11:49:26 2020 +0200
@@ -98,6 +98,7 @@
 	}
 }
 
+// MinTime returns the earlier time of a and b.
 func MinTime(a, b time.Time) time.Time {
 	if a.Before(b) {
 		return a
@@ -105,6 +106,7 @@
 	return b
 }
 
+// MaxTime returns the later time of a and b.
 func MaxTime(a, b time.Time) time.Time {
 	if a.After(b) {
 		return a
@@ -112,9 +114,30 @@
 	return b
 }
 
+// OrderTime orders the times a and b ascendingly.
 func OrderTime(a, b time.Time) (time.Time, time.Time) {
 	if a.Before(b) {
 		return a, b
 	}
 	return b, a
 }
+
+// Dusk returns the beginning of the day the time t is in.
+func Dusk(t time.Time) time.Time {
+	return time.Date(
+		t.Year(),
+		t.Month(),
+		t.Day(),
+		0, 0, 0, 0,
+		t.Location())
+}
+
+// Dawn returns the last time of the day the time t is in.
+func Dawn(t time.Time) time.Time {
+	return time.Date(
+		t.Year(),
+		t.Month(),
+		t.Day(),
+		23, 59, 59, 999999999,
+		t.Location())
+}
--- a/pkg/controllers/fwa.go	Thu Jul 09 17:26:53 2020 +0200
+++ b/pkg/controllers/fwa.go	Wed Jul 29 11:49:26 2020 +0200
@@ -342,7 +342,7 @@
 	}
 
 	// Stop yesterday
-	end := common.MinTime(dusk(time.Now()).Add(-time.Nanosecond), to)
+	end := common.MinTime(common.Dusk(time.Now()).Add(-time.Nanosecond), to)
 
 	// We step through the time in steps of one day.
 	for current = from; current.Before(end); {
@@ -456,24 +456,6 @@
 	return len(classes) - 1
 }
 
-func dusk(t time.Time) time.Time {
-	return time.Date(
-		t.Year(),
-		t.Month(),
-		t.Day(),
-		0, 0, 0, 0,
-		t.Location())
-}
-
-func dawn(t time.Time) time.Time {
-	return time.Date(
-		t.Year(),
-		t.Month(),
-		t.Day(),
-		23, 59, 59, 999999999,
-		t.Location())
-}
-
 func parseFromTo(
 	rw http.ResponseWriter,
 	req *http.Request,
@@ -490,7 +472,7 @@
 
 	from, to = common.OrderTime(from, to)
 	// Operate on daily basis so go to full days.
-	return dusk(from), dawn(to), true
+	return common.Dusk(from), common.Dawn(to), true
 }
 
 func parseFWAMode(mode string) fwaMode {