added +duration and *monthly for expiration time

This commit is contained in:
Radu Ioan Fericean
2013-07-30 22:42:51 +03:00
parent 78d2ee4e28
commit 11e7eed149
5 changed files with 40 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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

View File

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