diff --git a/parseany.go b/parseany.go index 74ca494..582e58a 100644 --- a/parseany.go +++ b/parseany.go @@ -90,6 +90,9 @@ const ( dateAlphaWsAlpha dateAlphaWsAlphaYearmaybe // 34 dateAlphaPeriodWsDigit + dateAlphaSlash + dateAlphaSlashDigit + dateAlphaSlashDigitSlash dateWeekdayComma dateWeekdayAbbrevComma ) @@ -855,6 +858,14 @@ iterRunes: // // dateAlphaPeriodWsDigit // oct. 1, 1970 + // dateAlphaSlash + // dateAlphaSlashDigit + // dateAlphaSlashDigitSlash + // Oct/ 7/1970 + // Oct/07/1970 + // February/ 7/1970 + // February/07/1970 + // // dateWeekdayComma // Monday, 02 Jan 2006 15:04:05 MST // Monday, 02-Jan-06 15:04:05 MST @@ -937,6 +948,30 @@ iterRunes: } else { return p, unknownErr(datestr) } + case r == '/': + // X + // Oct/ 7/1970 + // Oct/07/1970 + // X + // February/ 7/1970 + // February/07/1970 + // Must be a valid short or long month + if i == 3 { + p.moi = 0 + p.molen = i - p.moi + p.set(p.moi, "Jan") + p.stateDate = dateAlphaSlash + } else { + possibleFullMonth := strings.ToLower(p.datestr[:i]) + if i > 3 && isMonthFull(possibleFullMonth) { + p.moi = 0 + p.molen = i - p.moi + p.fullMonth = possibleFullMonth + p.stateDate = dateAlphaSlash + } else { + return p, unknownErr(datestr) + } + } } case dateAlphaWs: @@ -1183,6 +1218,53 @@ iterRunes: default: return p, unknownErr(datestr) } + + case dateAlphaSlash: + // Oct/ 7/1970 + // February/07/1970 + switch { + case r == ' ': + // continue + case unicode.IsDigit(r): + p.stateDate = dateAlphaSlashDigit + p.dayi = i + default: + return p, unknownErr(datestr) + } + + case dateAlphaSlashDigit: + // dateAlphaSlash: + // dateAlphaSlashDigit: + // dateAlphaSlashDigitSlash: + // Oct/ 7/1970 + // Oct/07/1970 + // February/ 7/1970 + // February/07/1970 + switch { + case r == '/': + p.yeari = i + 1 + p.daylen = i - p.dayi + if !p.setDay() { + return p, unknownErr(datestr) + } + p.stateDate = dateAlphaSlashDigitSlash + case unicode.IsDigit(r): + // continue + default: + return p, unknownErr(datestr) + } + + case dateAlphaSlashDigitSlash: + switch { + case unicode.IsDigit(r): + // continue + case r == ' ': + p.stateTime = timeStart + break iterRunes + default: + return p, unknownErr(datestr) + } + case dateWeekdayComma: // Monday, 02 Jan 2006 15:04:05 MST // Monday, 02 Jan 2006 15:04:05 -0700 @@ -2044,6 +2126,11 @@ iterRunes: p.setEntireFormat([]byte("2006年01月02日 15:04:05")) return p, nil + case dateAlphaSlashDigitSlash: + // Oct/ 7/1970 + // February/07/1970 + return p, nil + case dateWeekdayComma: // Monday, 02 Jan 2006 15:04:05 -0700 // Monday, 02 Jan 2006 15:04:05 +0100 @@ -2381,6 +2468,9 @@ func isDay(alpha string) bool { return false } func isMonthFull(alpha string) bool { + if len(alpha) > len("september") { + return false + } for _, month := range months { if alpha == month { return true diff --git a/parseany_test.go b/parseany_test.go index 7940dd6..190e2c1 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -304,9 +304,19 @@ var testInputs = []dateTest{ // 112.195.209.90 - - [20/Feb/2018:12:12:14 +0800] "GET / HTTP/1.1" 200 190 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36" "-" {in: "06/May/2008:08:11:17 -0700", out: "2008-05-06 15:11:17 +0000 UTC"}, {in: "30/May/2008:08:11:17 -0700", out: "2008-05-30 15:11:17 +0000 UTC"}, - // dd/mon/yyy hh:mm:ss tz + // dd/mon/yyyy hh:mm:ss tz {in: "06/May/2008:08:11:17 -0700", out: "2008-05-06 15:11:17 +0000 UTC"}, {in: "30/May/2008:08:11:17 -0700", out: "2008-05-30 15:11:17 +0000 UTC"}, + // mon/dd/yyyy + {in: "Oct/ 7/1970", out: "1970-10-07 00:00:00 +0000 UTC"}, + {in: "Oct/31/1970", out: "1970-10-31 00:00:00 +0000 UTC"}, + {in: "Oct/03/1970", out: "1970-10-03 00:00:00 +0000 UTC"}, + {in: "Oct/03/1970 22:33:44", out: "1970-10-03 22:33:44 +0000 UTC"}, + {in: "February/ 7/1970", out: "1970-02-07 00:00:00 +0000 UTC"}, + {in: "February/27/1970", out: "1970-02-27 00:00:00 +0000 UTC"}, + {in: "February/03/1970", out: "1970-02-03 00:00:00 +0000 UTC"}, + {in: "February/03/1970 22:33:44.555", out: "1970-02-03 22:33:44.555 +0000 UTC"}, + {in: "February/03/1970 11:33:44.555 PM PST", out: "1970-02-03 23:33:44.555 +0000 UTC", zname: "PST"}, // yyyy-mm-dd {in: "2014-04-02", out: "2014-04-02 00:00:00 +0000 UTC"}, {in: "2014-03-31", out: "2014-03-31 00:00:00 +0000 UTC"},