Added *mo time support

This commit is contained in:
Trial97
2020-06-17 10:52:51 +03:00
committed by Dan Christian Bogos
parent affc022841
commit 747c0cf86f
3 changed files with 40 additions and 8 deletions

View File

@@ -14,7 +14,7 @@ cgrates (0.11.0~dev) UNRELEASED; urgency=medium
* [Templates] Added new dataconvertor: *ip2hex
* [AgentS] Added support for *group type and correctly overwrite
the values in case of *variable
* [EventReader] Correctly populate ConcurrentRequest from config
* [ERs] Correctly populate ConcurrentRequest from config
* [FilterS] Updated *exists to dynamically compute the path if the
path
* [AgentS] Added support for *tmp path
@@ -67,6 +67,7 @@ cgrates (0.11.0~dev) UNRELEASED; urgency=medium
* [ERs] Added support to reference CSV fields by the column name
* [ERs] Renamed *default reader folders
* [FilterS] Updated Filter indexes
* [General] Added *mo+extraDuration time support (e.g. *mo+1h will be time.Now() + 1 month + 1 hour)
-- DanB <danb@cgrates.org> Wed, 19 Feb 2020 13:25:52 +0200

View File

@@ -183,6 +183,15 @@ func Round(x float64, prec int, method string) float64 {
return rounder / pow
}
func getAddDuration(tmStr string) (addDur time.Duration, err error) {
eDurIdx := strings.Index(tmStr, "+")
if eDurIdx == -1 {
return
}
return time.ParseDuration(tmStr[eDurIdx+1:])
}
// ParseTimeDetectLayout returns the time from string
func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
tmStr = strings.TrimSpace(tmStr)
var nilTime time.Time
@@ -204,14 +213,18 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
return time.Now().AddDate(1, 0, 0), nil // add one year
case strings.HasPrefix(tmStr, "*month_end"):
expDate := GetEndOfMonth(time.Now())
if eDurIdx := strings.Index(tmStr, "+"); eDurIdx != -1 {
var extraDur time.Duration
if extraDur, err = time.ParseDuration(tmStr[eDurIdx+1:]); err != nil {
return nilTime, err
}
expDate = expDate.Add(extraDur)
extraDur, err := getAddDuration(tmStr)
if err != nil {
return nilTime, err
}
expDate = expDate.Add(extraDur)
return expDate, nil
case strings.HasPrefix(tmStr, "*mo"): // add one month and extra duration
extraDur, err := getAddDuration(tmStr)
if err != nil {
return nilTime, err
}
return time.Now().AddDate(0, 1, 0).Add(extraDur), nil
case astTimestamp.MatchString(tmStr):
return time.Parse("2006-01-02T15:04:05.999999999-0700", tmStr)
case rfc3339Rule.MatchString(tmStr):
@@ -272,7 +285,7 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
return nilTime, errors.New("Unsupported time format")
}
// returns a number equal or larger than the amount that exactly
// RoundDuration returns a number equal or larger than the amount that exactly
// is divisible to whole
func RoundDuration(whole, amount time.Duration) time.Duration {
a, w := float64(amount), float64(whole)

View File

@@ -407,6 +407,18 @@ 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("*mo", ""); err != nil {
t.Error(err)
} else if expected.Sub(date).Seconds() > 1 {
t.Errorf("received: %+v", date)
}
expected = time.Now().AddDate(0, 1, 0).Add(time.Hour + 2*time.Minute)
if date, err := ParseTimeDetectLayout("*mo+1h2m", ""); err != nil {
t.Error(err)
} else if expected.Sub(date).Seconds() > 1 {
t.Errorf("received: %+v", date)
}
expected = time.Now().AddDate(1, 0, 0)
if date, err := ParseTimeDetectLayout("*yearly", ""); err != nil {
t.Error(err)
@@ -432,6 +444,12 @@ func TestParseTimeDetectLayout(t *testing.T) {
t.Errorf("Expecting nilTime, received: %+v", date)
}
if date, err := ParseTimeDetectLayout("*mo+xyz", ""); err == nil {
t.Error("Expecting error 'time: invalid time duration', received: nil")
} else if date != nilTime {
t.Errorf("Expecting nilTime, received: %+v", date)
}
date, err = ParseTimeDetectLayout("2013-07-30T19:33:10Z", "")
expected = time.Date(2013, 7, 30, 19, 33, 10, 0, time.UTC)
if err != nil || !date.Equal(expected) {