Skip to content

Commit dc2577d

Browse files
committed
Add support for dd-mm-yyyy (digit month) formats
Ref: araddon/pull/140
1 parent 8931277 commit dc2577d

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

parseany.go

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ const (
9393
dateAlphaPeriodWsDigit
9494
dateWeekdayComma
9595
dateWeekdayAbbrevComma
96+
dateDigitDashDigit
97+
dateDigitDashDigitDash
9698
)
9799
const (
98100
// Time state
@@ -499,6 +501,9 @@ iterRunes:
499501
if unicode.IsLetter(r) {
500502
p.stateDate = dateDigitDashAlpha
501503
p.moi = i
504+
} else if unicode.IsDigit(r) {
505+
p.stateDate = dateDigitDashDigit
506+
p.moi = i
502507
} else {
503508
return nil, unknownErr(datestr)
504509
}
@@ -513,7 +518,15 @@ iterRunes:
513518
p.yeari = i + 1
514519
p.stateDate = dateDigitDashAlphaDash
515520
}
516-
521+
case dateDigitDashDigit:
522+
// 29-06-2026
523+
switch r {
524+
case '-':
525+
p.molen = i - p.moi
526+
p.set(p.moi, "01")
527+
p.yeari = i + 1
528+
p.stateDate = dateDigitDashDigitDash
529+
}
517530
case dateDigitDashAlphaDash:
518531
// 13-Feb-03 ambiguous
519532
// 28-Feb-03 ambiguous
@@ -546,6 +559,36 @@ iterRunes:
546559
p.stateTime = timeStart
547560
break iterRunes
548561
}
562+
case dateDigitDashDigitDash:
563+
// 29-06-2026
564+
switch r {
565+
case ' ':
566+
// we need to find if this was 4 digits, aka year
567+
// or 2 digits which makes it ambiguous year/day
568+
length := i - (p.moi + p.molen + 1)
569+
if length == 4 {
570+
p.yearlen = 4
571+
p.set(p.yeari, "2006")
572+
// We now also know that part1 was the day
573+
p.dayi = 0
574+
p.daylen = p.part1Len
575+
p.setDay()
576+
} else if length == 2 {
577+
// We have no idea if this is
578+
// yy-mon-dd OR dd-mon-yy
579+
//
580+
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horible assumption
581+
p.ambiguousMD = true
582+
p.yearlen = 2
583+
p.set(p.yeari, "06")
584+
// We now also know that part1 was the day
585+
p.dayi = 0
586+
p.daylen = p.part1Len
587+
p.setDay()
588+
}
589+
p.stateTime = timeStart
590+
break iterRunes
591+
}
549592

550593
case dateDigitYearSlash:
551594
// 2014/07/10 06:55:38.156283
@@ -1860,7 +1903,33 @@ iterRunes:
18601903
}
18611904

18621905
return p, nil
1906+
case dateDigitDashDigitDash:
1907+
// 13-02-03 ambiguous
1908+
// 28-02-03 ambiguous
1909+
// 29-06-2016
1910+
length := len(datestr) - (p.moi + p.molen + 1)
1911+
if length == 4 {
1912+
p.yearlen = 4
1913+
p.set(p.yeari, "2006")
1914+
// We now also know that part1 was the day
1915+
p.dayi = 0
1916+
p.daylen = p.part1Len
1917+
p.setDay()
1918+
} else if length == 2 {
1919+
// We have no idea if this is
1920+
// yy-mon-dd OR dd-mon-yy
1921+
//
1922+
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horible assumption
1923+
p.ambiguousMD = true
1924+
p.yearlen = 2
1925+
p.set(p.yeari, "06")
1926+
// We now also know that part1 was the day
1927+
p.dayi = 0
1928+
p.daylen = p.part1Len
1929+
p.setDay()
1930+
}
18631931

1932+
return p, nil
18641933
case dateDigitDot:
18651934
// 2014.05
18661935
p.molen = i - p.moi

parseany_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,15 @@ var testInputs = []dateTest{
239239
// yyyy-mm-dd-07:00
240240
{in: "2020-07-20+08:00", out: "2020-07-19 16:00:00 +0000 UTC"},
241241
{in: "2020-07-20+0800", out: "2020-07-19 16:00:00 +0000 UTC"},
242-
// dd-mmm-yy
242+
// dd-mmm-yy (alpha month)
243243
{in: "28-Feb-02", out: "2002-02-28 00:00:00 +0000 UTC"},
244244
{in: "15-Jan-18", out: "2018-01-15 00:00:00 +0000 UTC"},
245245
{in: "15-Jan-2017", out: "2017-01-15 00:00:00 +0000 UTC"},
246+
// dd-mmm-yy (digit month)
247+
{in: "28-02-02", out: "2002-02-28 00:00:00 +0000 UTC"}, // https://github.com/araddon/dateparse/issues/139
248+
{in: "15-01-18", out: "2018-01-15 00:00:00 +0000 UTC"},
249+
{in: "15-01-2017", out: "2017-01-15 00:00:00 +0000 UTC"},
250+
246251
// yyyy-mm
247252
{in: "2014-04", out: "2014-04-01 00:00:00 +0000 UTC"},
248253
// yyyy-mm-dd hh:mm:ss AM

0 commit comments

Comments
 (0)