This commit is contained in:
TeoV
2018-09-12 08:41:46 -04:00
committed by Dan Christian Bogos
parent c5d748db80
commit fb631f74b5
3 changed files with 29 additions and 13 deletions

View File

@@ -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):

View File

@@ -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) {