From 0eaa9ce684f39483dbc70ccd08191b337674b690 Mon Sep 17 00:00:00 2001 From: TeoV Date: Thu, 2 Jul 2020 16:29:37 +0300 Subject: [PATCH] Add support for *monthly_estimated fixes #2255 --- utils/coreutils.go | 8 ++++++++ utils/coreutils_test.go | 13 +++++++++++++ utils/dynamicdataprovider_test.go | 2 -- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/utils/coreutils.go b/utils/coreutils.go index 9f87e92d8..cdf5a972e 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -209,8 +209,16 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) { return time.Now().AddDate(0, 0, 1), nil // add one day case tmStr == "*monthly": return time.Now().AddDate(0, 1, 0), nil // add one month + case tmStr == "*monthly_estimated": + initialMnt := time.Now().Month() + tAfter := time.Now().AddDate(0, 1, 0) + for tAfter.Month()-initialMnt > 1 { + tAfter = tAfter.AddDate(0, 0, -1) + } + return tAfter, nil case tmStr == "*yearly": return time.Now().AddDate(1, 0, 0), nil // add one year + case strings.HasPrefix(tmStr, "*month_end"): expDate := GetEndOfMonth(time.Now()) extraDur, err := getAddDuration(tmStr) diff --git a/utils/coreutils_test.go b/utils/coreutils_test.go index c4733016d..2db023681 100644 --- a/utils/coreutils_test.go +++ b/utils/coreutils_test.go @@ -407,6 +407,19 @@ func TestParseTimeDetectLayout(t *testing.T) { } else if expected.Sub(date).Seconds() > 1 { t.Errorf("received: %+v", date) } + + expected = time.Now().AddDate(0, 1, 0) + if date, err := ParseTimeDetectLayout("*monthly_estimated", ""); err != nil { + t.Error(err) + } else { + for date.Month()-expected.Month() > 1 { + expected = expected.AddDate(0, 0, -1) + } + if expected.Sub(date).Seconds() > 1 { + t.Errorf("received: %+v", date) + } + } + expected = time.Now().AddDate(0, 1, 0) if date, err := ParseTimeDetectLayout("*mo", ""); err != nil { t.Error(err) diff --git a/utils/dynamicdataprovider_test.go b/utils/dynamicdataprovider_test.go index c79f50a1d..bf3129964 100644 --- a/utils/dynamicdataprovider_test.go +++ b/utils/dynamicdataprovider_test.go @@ -18,7 +18,6 @@ along with this program. If not, see package utils import ( - "fmt" "strings" "testing" ) @@ -252,5 +251,4 @@ func TestDynamicDataProviderGetFullFieldPath(t *testing.T) { if newpath == nil { t.Errorf("Expected: %v,received %q", nil, newpath) } - fmt.Println(*newpath) }