mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
added +duration and *monthly for expiration time
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user