Skip to content

Commit

Permalink
Merge pull request #2 from Clarilab/feature/handle_dashed_date_with_y…
Browse files Browse the repository at this point in the history
…ear_last

handle dashed date with year last
  • Loading branch information
Christoph-Harms authored Apr 24, 2023
2 parents 1c9c8a2 + dc40d0c commit 0be0eea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
20 changes: 17 additions & 3 deletions formatparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"strings"
)

var dashedDateRegex = regexp.MustCompile(`^\d{4}(-\d{1,2}){0,2}$`)
var dashedDateYearFirstRegex = regexp.MustCompile(`^\d{4}(-\d{1,2}){0,2}$`)
var dashedDateYearLastRegex = regexp.MustCompile(`^(\d{1,2}-){0,2}\d{4}$`)
var dottedDateRegex = regexp.MustCompile(`^(\d{1,2}\.){0,2}\d{4}$`)
var slashedDateYearLastRegex = regexp.MustCompile(`^(\d{1,2}/){0,2}\d{4}$`)
var slashedDateYearFirstRegex = regexp.MustCompile(`^\d{4}(/\d{1,2}){0,2}$`)
Expand All @@ -30,7 +31,7 @@ func DetermineDateFormat(date string) (string, error) {
return "", nil
}

if !dashedDateRegex.MatchString(date) {
if !dashedDateYearFirstRegex.MatchString(date) {
return "", ErrUnsupportedDateFormat
}

Expand Down Expand Up @@ -61,10 +62,15 @@ func TransformToDashedDate(date string) (string, error) {
return "", nil
}

if dashedDateRegex.MatchString(date) {
if dashedDateYearFirstRegex.MatchString(date) {
return date, nil
}

if dashedDateYearLastRegex.MatchString(date) {
// DD-MM-YYYY: in this case, split the date, reverse the slice and put it back together.
return strings.Join(reverse(strings.Split(date, "-")), "-"), nil
}

isDotted := dottedDateRegex.MatchString(date)
isSlashedYearLast := slashedDateYearLastRegex.MatchString(date)
isSlashedYearFirst := slashedDateYearFirstRegex.MatchString(date)
Expand Down Expand Up @@ -102,3 +108,11 @@ func TransformToDashedDate(date string) (string, error) {
return "", ErrUnsupportedDateFormat
}
}

func reverse(strSlice []string) []string {
for i, j := 0, len(strSlice)-1; i < j; i, j = i+1, j-1 {
strSlice[i], strSlice[j] = strSlice[j], strSlice[i]
}

return strSlice
}
36 changes: 19 additions & 17 deletions formatparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,25 @@ func Test_TransformToDashedDate(t *testing.T) {
input, expectedOutput string
expectedError error
}{
"DD.MM.YYYY": {"22.04.1712", "1712-04-22", nil},
"D.M.YYYY": {"2.4.1712", "1712-4-2", nil},
"D.MM.YYYY": {"2.04.1712", "1712-04-2", nil},
"D/MM/YYYY": {"2/04/1712", "1712-04-2", nil},
"YYYY-M-DD": {"1712-4-22", "1712-4-22", nil},
"DD.M.YYYY": {"22.4.1712", "1712-4-22", nil},
"MM.YYYY": {"08.1492", "1492-08", nil},
"YYYY": {"2023", "2023", nil},
"YYYY.MM.DD": {"1983.07.20", "", ErrUnsupportedDateFormat},
"date string already in dashed format": {"2012-10-03", "2012-10-03", nil},
"neither dashed nor dotted": {"20 07 1983", "", ErrUnsupportedDateFormat},
"some gibberish": {"sfv_24w4e", "", ErrUnsupportedDateFormat},
"some gibberish 2": {"_!@§hahaha", "", ErrUnsupportedDateFormat},
"some gibberish 3": {" ", "", ErrUnsupportedDateFormat},
"some gibberish 4": {\\_(ツ)_/¯", "", ErrUnsupportedDateFormat},
"can handle forward slashes": {"20/07/1983", "1983-07-20", nil},
"can handle forward slashes 2": {"1983/07/20", "1983-07-20", nil},
"DD.MM.YYYY": {"22.04.1712", "1712-04-22", nil},
"D.M.YYYY": {"2.4.1712", "1712-4-2", nil},
"D.MM.YYYY": {"2.04.1712", "1712-04-2", nil},
"D/MM/YYYY": {"2/04/1712", "1712-04-2", nil},
"YYYY-M-DD": {"1712-4-22", "1712-4-22", nil},
"DD.M.YYYY": {"22.4.1712", "1712-4-22", nil},
"MM.YYYY": {"08.1492", "1492-08", nil},
"YYYY": {"2023", "2023", nil},
"YYYY.MM.DD": {"1983.07.20", "", ErrUnsupportedDateFormat},
"date string already in dashed format": {"2012-10-03", "2012-10-03", nil},
"neither dashed nor dotted": {"20 07 1983", "", ErrUnsupportedDateFormat},
"some gibberish": {"sfv_24w4e", "", ErrUnsupportedDateFormat},
"some gibberish 2": {"_!@§hahaha", "", ErrUnsupportedDateFormat},
"some gibberish 3": {" ", "", ErrUnsupportedDateFormat},
"some gibberish 4": {\\_(ツ)_/¯", "", ErrUnsupportedDateFormat},
"can handle forward slashes": {"20/07/1983", "1983-07-20", nil},
"can handle forward slashes 2": {"1983/07/20", "1983-07-20", nil},
"can handle dashed date with year last": {"20-07-1983", "1983-07-20", nil},
"can handle dashed date with year last 2": {"20-7-1983", "1983-7-20", nil},
}

for name, tc := range testCases {
Expand Down

0 comments on commit 0be0eea

Please sign in to comment.