Skip to content

Commit

Permalink
Added *mo time support
Browse files Browse the repository at this point in the history
  • Loading branch information
Trial97 authored and danbogos committed Jun 17, 2020
1 parent affc022 commit 747c0cf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/debian/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <[email protected]> Wed, 19 Feb 2020 13:25:52 +0200

Expand Down
27 changes: 20 additions & 7 deletions utils/coreutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions utils/coreutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 747c0cf

Please sign in to comment.