added date series

This commit is contained in:
Radu Ioan Fericean
2012-05-30 00:44:40 +03:00
parent 2aae311008
commit 33a9ae9967
5 changed files with 214 additions and 7 deletions

Binary file not shown.

145
timespans/dateseries.go Normal file
View File

@@ -0,0 +1,145 @@
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2012 Radu Ioan Fericean
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package timespans
import (
"time"
"strings"
"strconv"
)
// Defines months series
type Months struct {
Id string
Series []time.Month
}
// Return true if the specified date is inside the series
func (m *Months) Contains(month time.Month) (result bool) {
result = false
for _, ms := range m.Series {
if ms == month {
result = true
break
}
}
return
}
/*
Serializes the month for the storage. Used for key-value storages.
*/
func (m *Months) store() (result string) {
for _, ms := range m.Series {
result += strconv.Itoa(int(ms)) + "|"
}
return
}
/*
De-serializes the month for the storage. Used for key-value storages.
*/
func (m *Months) restore(input string) {
elements := strings.Split(input, "|")
for _, ms := range elements {
if month, err := strconv.Atoi(ms); err == nil {
m.Series = append(m.Series, time.Month(month))
}
}
}
// Defines month days series
type MonthDays struct {
Id string
Series []int
}
// Return true if the specified date is inside the series
func (md *MonthDays) Contains(monthDay int) (result bool) {
result = false
for _, mds := range md.Series {
if mds == monthDay {
result = true
break
}
}
return
}
/*
Serializes the month days for the storage. Used for key-value storages.
*/
func (md *MonthDays) store() (result string) {
for _, mds := range md.Series {
result += strconv.Itoa(mds) + "|"
}
return
}
/*
De-serializes the month days for the storage. Used for key-value storages.
*/
func (md *MonthDays) restore(input string) {
elements := strings.Split(input, "|")
for _, mds := range elements {
if day, err := strconv.Atoi(mds); err == nil {
md.Series = append(md.Series, day)
}
}
}
// Defines week days series
type WeekDays struct {
Id string
Series []time.Weekday
}
// Return true if the specified date is inside the series
func (wd *WeekDays) Contains(weekDay time.Weekday) (result bool) {
result = false
for _, wds := range wd.Series {
if wds == weekDay {
result = true
break
}
}
return
}
/*
Serializes the week days for the storage. Used for key-value storages.
*/
func (wd *WeekDays) store() (result string) {
for _, wds := range wd.Series {
result += strconv.Itoa(int(wds)) + "|"
}
return
}
/*
De-serializes the week days for the storage. Used for key-value storages.
*/
func (wd *WeekDays) restore(input string) {
elements := strings.Split(input, "|")
for _, wds := range elements {
if day, err := strconv.Atoi(wds); err == nil {
wd.Series = append(wd.Series, time.Weekday(day))
}
}
}

View File

@@ -0,0 +1,64 @@
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2012 Radu Ioan Fericean
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package timespans
import (
"testing"
"reflect"
"time"
)
func TestMonthStoreRestore(t *testing.T) {
m := &Months{Id: "SUMMER", Series: []time.Month{5, 6, 7, 8}}
r := m.store()
if r != "5|6|7|8|" {
t.Errorf("Error serializing months: %v", r)
}
o := &Months{Id: "SUMMER"}
o.restore(r)
if !reflect.DeepEqual(o, m) {
t.Errorf("Expected %v was %v", m, o)
}
}
func TestMonthDayStoreRestore(t *testing.T) {
md := &MonthDays{Id: "CHRISTMAS", Series: []int{24, 25, 26}}
r := md.store()
if r != "24|25|26|" {
t.Errorf("Error serializing month days: %v", r)
}
o := &MonthDays{Id: "CHRISTMAS"}
o.restore(r)
if !reflect.DeepEqual(o, md) {
t.Errorf("Expected %v was %v", md, o)
}
}
func TestWeekDayStoreRestore(t *testing.T) {
wd := &WeekDays{Id: "WEEKEND", Series: []time.Weekday{6, 7}}
r := wd.store()
if r != "6|7|" {
t.Errorf("Error serializing week days: %v", r)
}
o := &WeekDays{Id: "WEEKEND"}
o.restore(r)
if !reflect.DeepEqual(o, wd) {
t.Errorf("Expected %v was %v", wd, o)
}
}

View File

@@ -25,8 +25,6 @@ import (
//"log"
)
type Months []time.Month
/*
Defines a time interval for which a certain set of prices will apply
*/
@@ -42,7 +40,7 @@ type Interval struct {
Returns true if the received time is inside the interval
*/
func (i *Interval) Contains(t time.Time) bool {
// chec for month
// check for month
if i.Month > 0 && t.Month() != i.Month {
return false
}

View File

@@ -27,7 +27,7 @@ func TestGetDestination(t *testing.T) {
defer getter.Close()
mb := &MinuteBucket{DestinationId: "nationale"}
d := mb.getDestination(getter)
if d.Id != "nationale" || len(d.Prefixes) != 4 {
if d == nil || d.Id != "nationale" || len(d.Prefixes) != 4 {
t.Error("Got wrong destination: ", d)
}
}
@@ -39,21 +39,21 @@ func TestMultipleGetDestination(t *testing.T) {
d := mb.getDestination(getter)
d = mb.getDestination(getter)
d = mb.getDestination(getter)
if d.Id != "nationale" || len(d.Prefixes) != 4 {
if d == nil || d.Id != "nationale" || len(d.Prefixes) != 4 {
t.Error("Got wrong destination: ", d)
}
mb = &MinuteBucket{DestinationId: "retea"}
d = mb.getDestination(getter)
d = mb.getDestination(getter)
d = mb.getDestination(getter)
if d.Id != "retea" || len(d.Prefixes) != 2 {
if d == nil || d.Id != "retea" || len(d.Prefixes) != 2 {
t.Error("Got wrong destination: ", d)
}
mb = &MinuteBucket{DestinationId: "mobil"}
d = mb.getDestination(getter)
d = mb.getDestination(getter)
d = mb.getDestination(getter)
if d.Id != "mobil" || len(d.Prefixes) != 2 {
if d == nil || d.Id != "mobil" || len(d.Prefixes) != 2 {
t.Error("Got wrong destination: ", d)
}
}