Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parsing dashes #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 73 additions & 3 deletions parseany.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,38 @@ iterRunes:
p.moi = i + 1
p.set(0, "2006")
} else {
// Either Ambiguous dd/mm vs mm/dd OR dd/month/yy
// 08-May-2005
// 03-31-2005
// 31-03-2005
if i+2 < len(p.datestr) && unicode.IsLetter(rune(datestr[i+1])) {
// 08-May-2005
p.stateDate = dateDigitDashAlpha
p.moi = i + 1
p.daylen = 2
p.dayi = 0
p.setDay()
continue
}
p.stateDate = dateDigitDash
// Ambiguous dd-mm vs mm-dd the bane of date-parsing
// 03-31-2005
// 31-03-2005
p.ambiguousMD = true
if p.preferMonthFirst {
if p.molen == 0 {
// 03-31-2005
p.molen = i
p.setMonth()
p.dayi = i + 1
}
} else {
if p.daylen == 0 {
p.daylen = i
p.setDay()
p.moi = i + 1
}
}
}
case '/':
// 08/May/2005
Expand Down Expand Up @@ -456,7 +487,6 @@ iterRunes:
p.setDay()
break iterRunes
}

case dateYearDashDashT:
// dateYearDashDashT
// 2006-01-02T15:04:05Z07:00
Expand All @@ -479,15 +509,51 @@ iterRunes:
p.set(p.moi, "Jan")
p.dayi = i + 1
}

case dateDigitDash:
// 13-Feb-03
// 29-Jun-2016
if unicode.IsLetter(r) {
p.stateDate = dateDigitDashAlpha
p.moi = i
} else {
return nil, unknownErr(datestr)
// 03-19-2012 10:11:59
// 04-2-2014 03:00:37
// 3-1-2012 10:11:59
// 4-8-2014 22:05
// 3-1-2014
// 10-13-2014
// 01-02-2006
// 1-2-06
switch r {
case '-':
// This is the 2nd / so now we should know start pts of all of the dd, mm, yy
if p.preferMonthFirst {
if p.daylen == 0 {
p.daylen = i - p.dayi
p.setDay()
p.yeari = i + 1
}
} else {
if p.molen == 0 {
p.molen = i - p.moi
p.setMonth()
p.yeari = i + 1
}
}
// Note no break, we are going to pass by and re-enter this dateDigitSlash
// and look for ending (space) or not (just date)
case ' ':
p.stateTime = timeStart
if p.yearlen == 0 {
p.yearlen = i - p.yeari
p.setYear()
}
break iterRunes
}
//return nil, unknownErr(datestr)
}

case dateDigitDashAlpha:
// 13-Feb-03
// 28-Feb-03
Expand Down Expand Up @@ -1845,7 +1911,6 @@ iterRunes:
}

return p, nil

case dateDigitDot:
// 2014.05
p.molen = i - p.moi
Expand Down Expand Up @@ -1907,6 +1972,11 @@ iterRunes:
case dateAlphaWsDigitYearmaybe:
return p, nil

case dateDigitDash:
// 3/1/2014
// 10/13/2014
// 01/02/2006
return p, nil
case dateDigitSlash:
// 3/1/2014
// 10/13/2014
Expand Down
6 changes: 5 additions & 1 deletion parseany_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ var testInputs = []dateTest{
// dd/mon/yyy 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"},
// mm-dd-yyyy
{in: "04-02-2014", out: "2014-04-02 00:00:00 +0000 UTC"},
{in: "03-31-2014", out: "2014-03-31 00:00:00 +0000 UTC"},
{in: "4-2-2014", out: "2014-04-02 00:00:00 +0000 UTC"},
// 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"},
Expand Down Expand Up @@ -511,7 +515,7 @@ var testParseErrors = []dateTest{
{in: "oct.-7-1970", err: true},
{in: "septe. 7, 1970", err: true},
{in: "SeptemberRR 7th, 1970", err: true},
{in: "29-06-2016", err: true},
// {in: "29-06-2016", err: true},
// this is just testing the empty space up front
{in: " 2018-01-02 17:08:09 -07:00", err: true},
}
Expand Down