Support git log format (Thu Apr 7 15:13:13 2005 -0700)

Adapt commit 99d9682a1c from https://github.com/araddon/dateparse/pull/92 by https://github.com/jiangxin (merge timeWsYearOffset case and validate format)
This commit is contained in:
Klondike Dragon 2023-12-15 17:42:07 -07:00
parent 18ec8c69f6
commit 249dd7368c
2 changed files with 30 additions and 5 deletions

View File

@ -122,6 +122,7 @@ const (
timeWsOffsetColonAlpha timeWsOffsetColonAlpha
timeWsOffsetColon timeWsOffsetColon
timeWsYear // 16 timeWsYear // 16
timeWsYearOffset
timeOffset timeOffset
timeOffsetColon timeOffsetColon
timeOffsetColonAlpha timeOffsetColonAlpha
@ -997,13 +998,14 @@ iterRunes:
} }
case dateAlpha: case dateAlpha:
// dateAlphaWS // dateAlphaWs
// Mon Jan _2 15:04:05 2006 // Mon Jan _2 15:04:05 2006
// Mon Jan _2 15:04:05 MST 2006 // Mon Jan _2 15:04:05 MST 2006
// Mon Jan 02 15:04:05 -0700 2006 // Mon Jan 02 15:04:05 -0700 2006
// Mon Jan 02 15:04:05 2006 -0700
// Mon Aug 10 15:44:11 UTC+0100 2015 // Mon Aug 10 15:44:11 UTC+0100 2015
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) // Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
// dateAlphaWSDigit // dateAlphaWsDigit
// May 8, 2009 5:57:51 PM // May 8, 2009 5:57:51 PM
// oct 1, 1970 // oct 1, 1970
// dateAlphaWsMonth // dateAlphaWsMonth
@ -1135,6 +1137,7 @@ iterRunes:
// Mon Jan _2 15:04:05 2006 // Mon Jan _2 15:04:05 2006
// Mon Jan _2 15:04:05 MST 2006 // Mon Jan _2 15:04:05 MST 2006
// Mon Jan 02 15:04:05 -0700 2006 // Mon Jan 02 15:04:05 -0700 2006
// Mon Jan 02 15:04:05 2006 -0700
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) // Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
// Mon Aug 10 15:44:11 UTC+0100 2015 // Mon Aug 10 15:44:11 UTC+0100 2015
// dateAlphaWsDigit // dateAlphaWsDigit
@ -1664,6 +1667,8 @@ iterRunes:
// 00:12:00 +00:00 UTC // 00:12:00 +00:00 UTC
// timeWsYear // timeWsYear
// 00:12:00 2008 // 00:12:00 2008
// timeWsYearOffset
// 00:12:00 2008 -0700
// timeZ // timeZ
// 15:04:05.99Z // 15:04:05.99Z
switch r { switch r {
@ -1688,6 +1693,23 @@ iterRunes:
p.yeari = i p.yeari = i
} }
} }
case timeWsYear:
// timeWsYearOffset
// 00:12:00 2008 -0700
switch r {
case ' ':
p.yearlen = i - p.yeari
if !p.setYear() {
return p, unknownErr(datestr)
}
case '+', '-':
p.offseti = i
p.stateTime = timeWsYearOffset
default:
if !unicode.IsDigit(r) {
return p, unknownErr(datestr)
}
}
case timeWsAlpha: case timeWsAlpha:
// 06:20:00 UTC // 06:20:00 UTC
// 06:20:00 UTC-05 // 06:20:00 UTC-05
@ -2049,7 +2071,7 @@ iterRunes:
if p.mslen >= 10 { if p.mslen >= 10 {
return p, fmt.Errorf("fractional seconds in %q too long near %q", datestr, string(p.datestr[p.msi:p.mslen])) return p, fmt.Errorf("fractional seconds in %q too long near %q", datestr, string(p.datestr[p.msi:p.mslen]))
} }
case timeOffset, timeWsOffset: case timeOffset, timeWsOffset, timeWsYearOffset:
switch len(p.datestr) - p.offseti { switch len(p.datestr) - p.offseti {
case 3: case 3:
// 19:55:00+01 (or 19:55:00 +01) // 19:55:00+01 (or 19:55:00 +01)

View File

@ -443,6 +443,9 @@ var testInputs = []dateTest{
{in: "4/2/2014, 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"}, {in: "4/2/2014, 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "04/02/2014, 04:08 AM", out: "2014-04-02 04:08:00 +0000 UTC"}, {in: "04/02/2014, 04:08 AM", out: "2014-04-02 04:08:00 +0000 UTC"},
{in: "04/02/2014, 04:08 PM", out: "2014-04-02 16:08:00 +0000 UTC"}, {in: "04/02/2014, 04:08 PM", out: "2014-04-02 16:08:00 +0000 UTC"},
// Git log default date format - https://github.com/araddon/dateparse/pull/92
{in: "Thu Apr 7 15:13:13 2005 -0700", out: "2005-04-07 22:13:13 +0000 UTC"},
{in: "Tue Dec 12 23:07:11 2023 -0700", out: "2023-12-13 06:07:11 +0000 UTC"},
// yyyy-mm-dd hh:mm:ss,000 // yyyy-mm-dd hh:mm:ss,000
{in: "2014-05-11 08:20:13,787", out: "2014-05-11 08:20:13.787 +0000 UTC"}, {in: "2014-05-11 08:20:13,787", out: "2014-05-11 08:20:13.787 +0000 UTC"},
// yyyy-mm-dd hh:mm:ss +0000 // yyyy-mm-dd hh:mm:ss +0000
@ -1090,5 +1093,5 @@ func TestRetryAmbiguousDateWithSwap(t *testing.T) {
// Convenience function for debugging a particular broken test case // Convenience function for debugging a particular broken test case
func TestDebug(t *testing.T) { func TestDebug(t *testing.T) {
MustParse("Jul 9, 2012 at 5:02am (EST)") MustParse("Tue Dec 12 23:07:11 2023 -0700")
} }