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

Add support for dd-mm-yyyy (digit month) formats #1

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 71 additions & 1 deletion parseany.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ const (
dateAlphaPeriodWsDigit
dateWeekdayComma
dateWeekdayAbbrevComma
dateDigitDashDigit
dateDigitDashDigitDash
)
const (
// Time state
Expand Down Expand Up @@ -485,6 +487,9 @@ iterRunes:
if unicode.IsLetter(r) {
p.stateDate = dateDigitDashAlpha
p.moi = i
} else if unicode.IsDigit(r) {
p.stateDate = dateDigitDashDigit
p.moi = i
} else {
return nil, unknownErr(datestr)
}
Expand All @@ -499,7 +504,15 @@ iterRunes:
p.yeari = i + 1
p.stateDate = dateDigitDashAlphaDash
}

case dateDigitDashDigit:
// 29-06-2026
switch r {
case '-':
p.molen = i - p.moi
p.set(p.moi, "01")
p.yeari = i + 1
p.stateDate = dateDigitDashDigitDash
}
case dateDigitDashAlphaDash:
// 13-Feb-03 ambiguous
// 28-Feb-03 ambiguous
Expand Down Expand Up @@ -532,6 +545,36 @@ iterRunes:
p.stateTime = timeStart
break iterRunes
}
case dateDigitDashDigitDash:
// 29-06-2026
switch r {
case ' ':
// we need to find if this was 4 digits, aka year
// or 2 digits which makes it ambiguous year/day
length := i - (p.moi + p.molen + 1)
if length == 4 {
p.yearlen = 4
p.set(p.yeari, "2006")
// We now also know that part1 was the day
p.dayi = 0
p.daylen = p.part1Len
p.setDay()
} else if length == 2 {
// We have no idea if this is
// yy-mon-dd OR dd-mon-yy
//
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horible assumption
p.ambiguousMD = true
p.yearlen = 2
p.set(p.yeari, "06")
// We now also know that part1 was the day
p.dayi = 0
p.daylen = p.part1Len
p.setDay()
}
p.stateTime = timeStart
break iterRunes
}

case dateDigitYearSlash:
// 2014/07/10 06:55:38.156283
Expand Down Expand Up @@ -1844,6 +1887,33 @@ iterRunes:
p.setDay()
}

return p, nil
case dateDigitDashDigitDash:
// 13-02-03 ambiguous
// 28-02-03 ambiguous
// 29-06-2016
length := len(datestr) - (p.moi + p.molen + 1)
if length == 4 {
p.yearlen = 4
p.set(p.yeari, "2006")
// We now also know that part1 was the day
p.dayi = 0
p.daylen = p.part1Len
p.setDay()
} else if length == 2 {
// We have no idea if this is
// yy-mon-dd OR dd-mon-yy
//
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horible assumption
p.ambiguousMD = true
p.yearlen = 2
p.set(p.yeari, "06")
// We now also know that part1 was the day
p.dayi = 0
p.daylen = p.part1Len
p.setDay()
}

return p, nil

case dateDigitDot:
Expand Down
7 changes: 6 additions & 1 deletion parseany_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,15 @@ var testInputs = []dateTest{
// yyyy-mm-dd-07:00
{in: "2020-07-20+08:00", out: "2020-07-19 16:00:00 +0000 UTC"},
{in: "2020-07-20+0800", out: "2020-07-19 16:00:00 +0000 UTC"},
// dd-mmm-yy
// dd-mmm-yy (alpha month)
{in: "28-Feb-02", out: "2002-02-28 00:00:00 +0000 UTC"},
{in: "15-Jan-18", out: "2018-01-15 00:00:00 +0000 UTC"},
{in: "15-Jan-2017", out: "2017-01-15 00:00:00 +0000 UTC"},
// dd-mmm-yy (digit month)
{in: "28-02-02", out: "2002-02-28 00:00:00 +0000 UTC"}, https: //github.com/araddon/dateparse/issues/139
{in: "15-01-18", out: "2018-01-15 00:00:00 +0000 UTC"},
{in: "15-01-2017", out: "2017-01-15 00:00:00 +0000 UTC"},

// yyyy-mm
{in: "2014-04", out: "2014-04-01 00:00:00 +0000 UTC"},
// yyyy-mm-dd hh:mm:ss AM
Expand Down