From 3054f79adb81e0cc0f8989b3113b2735a0460149 Mon Sep 17 00:00:00 2001 From: Christoph Harms-Ensink Date: Mon, 24 Apr 2023 13:26:23 +0200 Subject: [PATCH 1/3] handle dashed date with year last --- formatparser.go | 19 ++++++++++++++++--- formatparser_test.go | 36 +++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/formatparser.go b/formatparser.go index ed5e5c3..6e8b750 100644 --- a/formatparser.go +++ b/formatparser.go @@ -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}$`) @@ -30,7 +31,7 @@ func DetermineDateFormat(date string) (string, error) { return "", nil } - if !dashedDateRegex.MatchString(date) { + if !dashedDateYearFirstRegex.MatchString(date) { return "", ErrUnsupportedDateFormat } @@ -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) @@ -102,3 +108,10 @@ func TransformToDashedDate(date string) (string, error) { return "", ErrUnsupportedDateFormat } } + +func reverse(strings []string) []string { + for i, j := 0, len(strings)-1; i < j; i, j = i+1, j-1 { + strings[i], strings[j] = strings[j], strings[i] + } + return strings +} diff --git a/formatparser_test.go b/formatparser_test.go index 74fe5fa..f102627 100644 --- a/formatparser_test.go +++ b/formatparser_test.go @@ -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 { From 1387a34f33e3043a1975efb5f500d9265b9cacab Mon Sep 17 00:00:00 2001 From: Christoph Harms-Ensink Date: Mon, 24 Apr 2023 13:31:08 +0200 Subject: [PATCH 2/3] improve code quality --- formatparser.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/formatparser.go b/formatparser.go index 6e8b750..e4b4215 100644 --- a/formatparser.go +++ b/formatparser.go @@ -109,9 +109,10 @@ func TransformToDashedDate(date string) (string, error) { } } -func reverse(strings []string) []string { - for i, j := 0, len(strings)-1; i < j; i, j = i+1, j-1 { - strings[i], strings[j] = strings[j], strings[i] +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 strings + + return strSLice } From dc40d0c3838bcdc5fc69b23b1321e9de67167b4f Mon Sep 17 00:00:00 2001 From: Christoph Harms-Ensink Date: Mon, 24 Apr 2023 13:32:05 +0200 Subject: [PATCH 3/3] fix typo --- formatparser.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/formatparser.go b/formatparser.go index e4b4215..5cd85ff 100644 --- a/formatparser.go +++ b/formatparser.go @@ -109,10 +109,10 @@ func TransformToDashedDate(date string) (string, error) { } } -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] +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 + return strSlice }