From 11e7eed1494aa4e60ffb701adda9d87592591336 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 30 Jul 2013 22:42:51 +0300 Subject: [PATCH] added +duration and *monthly for expiration time --- engine/action_timing.go | 2 ++ engine/action_trigger.go | 3 ++- engine/minute_buckets.go | 3 ++- utils/coreutils.go | 7 +++---- utils/utils_test.go | 31 +++++++++++++++++++++++++++++++ 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/engine/action_timing.go b/engine/action_timing.go index e8829074d..89868ce86 100644 --- a/engine/action_timing.go +++ b/engine/action_timing.go @@ -20,6 +20,7 @@ package engine import ( "fmt" + "github.com/cgrates/cgrates/utils" "sort" "strconv" "strings" @@ -206,6 +207,7 @@ func (at *ActionTiming) Execute() (err error) { return } for _, a := range aac { + a.ExpirationDate, _ = utils.ParseDate(a.ExpirationString) actionFunction, exists := getActionFunc(a.ActionType) if !exists { Logger.Crit(fmt.Sprintf("Function type %v not available, aborting execution!", a.ActionType)) diff --git a/engine/action_trigger.go b/engine/action_trigger.go index d88b40ad2..82bba4849 100644 --- a/engine/action_trigger.go +++ b/engine/action_trigger.go @@ -20,7 +20,7 @@ package engine import ( "fmt" - //"log" + "github.com/cgrates/cgrates/utils" "sort" ) @@ -46,6 +46,7 @@ func (at *ActionTrigger) Execute(ub *UserBalance) (err error) { return } for _, a := range aac { + a.ExpirationDate, _ = utils.ParseDate(a.ExpirationString) actionFunction, exists := getActionFunc(a.ActionType) if !exists { Logger.Warning(fmt.Sprintf("Function type %v not available, aborting execution!", a.ActionType)) diff --git a/engine/minute_buckets.go b/engine/minute_buckets.go index 65893e219..50bdd0070 100644 --- a/engine/minute_buckets.go +++ b/engine/minute_buckets.go @@ -65,7 +65,8 @@ func (mb *MinuteBucket) Equal(o *MinuteBucket) bool { return mb.DestinationId == o.DestinationId && mb.Weight == o.Weight && mb.Price == o.Price && - mb.PriceType == o.PriceType + mb.PriceType == o.PriceType && + mb.ExpirationDate.Equal(o.ExpirationDate) } func (mb *MinuteBucket) IsExpired() bool { diff --git a/utils/coreutils.go b/utils/coreutils.go index 106fb38a4..df1fab17a 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -96,12 +96,9 @@ func Round(x float64, prec int, method string) float64 { } func ParseDate(date string) (expDate time.Time, err error) { - if date == "" { - return // zero values are fine - } expirationTime := []byte(date) switch { - case string(expirationTime) == "*unlimited": + case string(expirationTime) == "*unlimited" || string(expirationTime) == "": // leave it at zero case string(expirationTime[0]) == "+": d, err := time.ParseDuration(string(expirationTime[1:])) @@ -109,6 +106,8 @@ func ParseDate(date string) (expDate time.Time, err error) { return expDate, err } expDate = time.Now().Add(d) + case string(expirationTime) == "*monthly": + expDate = time.Now().AddDate(0, 1, 0) // add one month default: unix, err := strconv.ParseInt(string(expirationTime), 10, 64) if err != nil { diff --git a/utils/utils_test.go b/utils/utils_test.go index e55775f10..25dea5bc6 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -20,6 +20,7 @@ package utils import ( "testing" + "time" ) func TestFirstNonEmpty(t *testing.T) { @@ -119,3 +120,33 @@ func TestRoundByMethodDown2(t *testing.T) { t.Errorf("Error rounding up: sould be %v was %v", expected, result) } } + +func TestParseDateUnix(t *testing.T) { + date, err := ParseDate("1375212790") + expected := time.Date(2013, 7, 30, 19, 33, 10, 0, time.UTC) + if err != nil || !date.Equal(expected) { + t.Error("error parsing date: ", expected.Sub(date)) + } +} + +func TestParseDateUnlimited(t *testing.T) { + date, err := ParseDate("*unlimited") + if err != nil || !date.IsZero() { + t.Error("error parsing unlimited date!: ") + } +} + +func TestParseDateEmpty(t *testing.T) { + date, err := ParseDate("") + if err != nil || !date.IsZero() { + t.Error("error parsing unlimited date!: ") + } +} + +func TestParseDateMonthly(t *testing.T) { + date, err := ParseDate("*monthly") + expected := time.Now().AddDate(0, 1, 0) + if err != nil || expected.Sub(date).Seconds() > 1 { + t.Error("error parsing date: ", expected.Sub(date).Seconds()) + } +}