Add support for *monthly_estimated fixes #2255

This commit is contained in:
TeoV
2020-07-02 16:29:37 +03:00
parent 95fe5bed2a
commit 0eaa9ce684
3 changed files with 21 additions and 2 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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)
}