From fb631f74b5666e9bb4091fb534e2080907d7a4cd Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 12 Sep 2018 08:41:46 -0400 Subject: [PATCH] Fixes #1195 --- cdrc/xml_test.go | 26 +++++++++++++------------- utils/coreutils.go | 7 +++++++ utils/coreutils_test.go | 9 +++++++++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/cdrc/xml_test.go b/cdrc/xml_test.go index f6a5b50b8..44c87a470 100644 --- a/cdrc/xml_test.go +++ b/cdrc/xml_test.go @@ -407,10 +407,10 @@ var xmlContent = ` 1510225513055 1510225513304 - 20160419210005.247 - 20160419210006.813 + 1510225514836 + 1510225516981 - 20160419210020.296 + 1510225516981 1510225516981 @@ -455,10 +455,10 @@ var xmlContent = ` 1510225531933 1510225532183 - 20160419210005.247 - 20160419210006.813 + 1510225534973 + 1510225539364 1510225593101 - 20160419210020.296 + 1510225593101 1510225593101 @@ -503,10 +503,10 @@ var xmlContent = ` 1510225865894 1510225866144 - 20160419210005.247 - 20160419210006.813 + 1510225866756 + 1510225876243 1510225916144 - 20160419210020.296 + 1510225916144 1510225916144 @@ -605,13 +605,13 @@ func TestXMLRPProcessWithNewFilters2(t *testing.T) { t.Error(err) } expectedCDRs := []*engine.CDR{ - &engine.CDR{CGRID: "7c6170203bd3d5b34e9fce366b5ee7d621217824", + &engine.CDR{CGRID: "0ad7f9554ff8fc5b3a7cebbe7431bbf809bc5144", OriginHost: "0.0.0.0", Source: "zw_cfs1", OriginID: "46d7974398c2671016afccc3f2c428c7", ToR: "*voice", RequestType: "*rated", Tenant: "XX.liquid.tel", Category: "call", Account: "+27110493421", Destination: "+270843073451", - SetupTime: time.Date(2016, 4, 19, 21, 0, 5, 247000000, time.UTC), - AnswerTime: time.Date(2016, 4, 19, 21, 0, 6, 813000000, time.UTC), - Usage: time.Duration(13483000000), + SetupTime: time.Date(2017, 11, 9, 11, 5, 34, 973000000, time.UTC), + AnswerTime: time.Date(2017, 11, 9, 11, 5, 39, 364000000, time.UTC), + Usage: time.Duration(53737000000), ExtraFields: map[string]string{}, Cost: -1}, } if !reflect.DeepEqual(expectedCDRs, cdrs) { diff --git a/utils/coreutils.go b/utils/coreutils.go index e13b6c880..18eeaff0d 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -170,6 +170,7 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) { fsTimestamp := regexp.MustCompile(`^\d{16}$`) astTimestamp := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d*[+,-]\d+$`) unixTimestampRule := regexp.MustCompile(`^\d{10}$`) + unixTimestampMilisecondsRule := regexp.MustCompile(`^\d{13}$`) oneLineTimestampRule := regexp.MustCompile(`^\d{14}$`) oneSpaceTimestampRule := regexp.MustCompile(`^\d{2}\.\d{2}.\d{4}\s{1}\d{2}:\d{2}:\d{2}$`) eamonTimestampRule := regexp.MustCompile(`^\d{2}/\d{2}/\d{4}\s{1}\d{2}:\d{2}:\d{2}$`) @@ -197,6 +198,12 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) { } else { return time.Unix(tmstmp, 0).In(loc), nil } + case unixTimestampMilisecondsRule.MatchString(tmStr): + if tmstmp, err := strconv.ParseInt(tmStr, 10, 64); err != nil { + return nilTime, err + } else { + return time.Unix(0, tmstmp*int64(time.Millisecond)).In(loc), nil + } case tmStr == "0" || len(tmStr) == 0: // Time probably missing from request return nilTime, nil case oneLineTimestampRule.MatchString(tmStr): diff --git a/utils/coreutils_test.go b/utils/coreutils_test.go index 49d464276..c5e6b9023 100644 --- a/utils/coreutils_test.go +++ b/utils/coreutils_test.go @@ -239,6 +239,15 @@ func TestParseTimeDetectLayout(t *testing.T) { } else if parseNowTimeStr.After(start) && parseNowTimeStr.Before(end) { t.Errorf("Unexpected time parsed: %v", parseNowTimeStr) } + + unixTmMilisecStr := "1534176053410" + expectedTime = time.Date(2018, 8, 13, 16, 00, 53, 410000000, time.UTC) + unixTm, err = ParseTimeDetectLayout(unixTmMilisecStr, "") + if err != nil { + t.Error(err) + } else if !unixTm.Equal(expectedTime) { + t.Errorf("Unexpected time parsed: %v, expecting: %v", unixTm, expectedTime) + } } func TestParseDateUnix(t *testing.T) {