Fixup time parser to support 0 as timestamps sent by FreeSWITCH

This commit is contained in:
DanB
2014-04-16 16:32:45 +02:00
parent 0236553c87
commit 341ead514e
3 changed files with 18 additions and 1 deletions

View File

@@ -131,7 +131,7 @@ func ParseTimeDetectLayout(tmStr string) (time.Time, error) {
} else {
return time.Unix(tmstmp, 0), nil
}
case len(tmStr) == 0: // Time probably missing from request
case tmStr == "0" || len(tmStr) == 0: // Time probably missing from request
return nilTime, nil
}
return nilTime, errors.New("Unsupported time format")

View File

@@ -40,3 +40,12 @@ func TestProcessReSearchReplace2(t *testing.T) {
t.Error("Unexpected output from SearchReplace: ", outStr)
}
}
func TestProcessReSearchReplace3(t *testing.T) { //"MatchedDestId":"CST_31800_DE080"
rsr := &ReSearchReplace{regexp.MustCompile(`"MatchedDestId":".+_(\w{5})"`), "$1"}
source := `[{"TimeStart":"2014-04-15T22:17:57+02:00","TimeEnd":"2014-04-15T22:18:01+02:00","Cost":0,"RateInterval":{"Timing":{"Years":[],"Months":[],"MonthDays":[],"WeekDays":[],"StartTime":"00:00:00","EndTime":""},"Rating":{"ConnectFee":0,"Rates":[{"GroupIntervalStart":0,"Value":0,"RateIncrement":1000000000,"RateUnit":60000000000}],"RoundingMethod":"*middle","RoundingDecimals":4},"Weight":10},"CallDuration":4000000000,"Increments":null,"MatchedSubject":"*out:sip.test.cgrates.org:call:*any","MatchedPrefix":"+49800","MatchedDestId":"CST_31800_DE080"}]`
expectOut := "DE080"
if outStr := rsr.Process(source); outStr != expectOut {
t.Error("Unexpected output from SearchReplace: ", outStr)
}
}

View File

@@ -194,6 +194,14 @@ func TestParseTimeDetectLayout(t *testing.T) {
} else if !fsTm.Equal(expectedTime) {
t.Errorf("Unexpected time parsed: %v, expecting: %v", fsTm, expectedTime)
}
fsTmstampStr = "0"
fsTm, err = ParseTimeDetectLayout(fsTmstampStr)
expectedTime = time.Time{}
if err != nil {
t.Error(err)
} else if !fsTm.Equal(expectedTime) {
t.Errorf("Unexpected time parsed: %v, expecting: %v", fsTm, expectedTime)
}
}
func TestParseDateUnix(t *testing.T) {