ParseTimeDetectLayout with support for Broadsoft timestamp parsing

This commit is contained in:
DanB
2016-05-08 16:59:23 +02:00
parent 3279bb6d92
commit 38a2c0f14d
2 changed files with 11 additions and 0 deletions

View File

@@ -144,6 +144,7 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
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}$`)
broadsoftTimestampRule := regexp.MustCompile(`^\d{14}\.\d{3}`)
switch {
case rfc3339Rule.MatchString(tmStr):
return time.Parse(time.RFC3339, tmStr)
@@ -171,6 +172,8 @@ func ParseTimeDetectLayout(tmStr string, timezone string) (time.Time, error) {
return time.ParseInLocation("02.01.2006 15:04:05", tmStr, loc)
case eamonTimestampRule.MatchString(tmStr):
return time.ParseInLocation("02/01/2006 15:04:05", tmStr, loc)
case broadsoftTimestampRule.MatchString(tmStr):
return time.ParseInLocation("20060102150405.999", tmStr, loc)
case tmStr == "*now":
return time.Now(), nil
}

View File

@@ -259,6 +259,14 @@ func TestParseTimeDetectLayout(t *testing.T) {
} else if !eamonTmS.Equal(expectedTime) {
t.Errorf("Unexpected time parsed: %v, expecting: %v", eamonTmS, expectedTime)
}
broadSoftTmStr := "20160419210007.037"
broadTmS, err := ParseTimeDetectLayout(broadSoftTmStr, "")
expectedTime = time.Date(2016, 4, 19, 21, 0, 7, 37000000, time.UTC)
if err != nil {
t.Error(err)
} else if !broadTmS.Equal(expectedTime) {
t.Errorf("Expecting: %v, received: %v", expectedTime, broadTmS)
}
}
func TestParseDateUnix(t *testing.T) {