Skip to content

Commit

Permalink
normalizeDate: Trim zero time part
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Nov 4, 2024
1 parent 2b8b023 commit 8d1c15e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 5 additions & 3 deletions date/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,17 +708,19 @@ func normalizeAndCheckDate(str string, langHint language.Code) (Date, bool, erro
}

func normalizeDate(str string, langHint language.Code) (string, bool, error) {
trimmed := strings.ToLower(strings.TrimFunc(str, isDateTrimRune))

trimmed := strings.TrimSuffix(str, "00:00:00") // Trim zero time part
trimmed = strings.TrimFunc(trimmed, isDateTrimRune)
if len(trimmed) < MinLength {
return "", false, fmt.Errorf("too short for a date: %q", str)
}

if len(trimmed) > 10 && trimmed[10] == 't' {
if len(trimmed) > 10 && trimmed[10] == 'T' {
// Use date part of this date-time format: "2006-01-02T15:04:05"
trimmed = trimmed[:10]
}

trimmed = strings.ToLower(trimmed)

langHint, _ = langHint.Normalized()

parts := strings.FieldsFunc(trimmed, isDateSeparatorRune)
Expand Down
2 changes: 2 additions & 0 deletions date/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func Test_Normalize(t *testing.T) {

"2006-01-02T15:04:05Z07:00": "2006-01-02", // RFC3339
"2006-01-02T15:04:05.999999999Z07:00": "2006-01-02", // RFC3339Nano

"30.11.2023 00:00:00": "2023-11-30",
}

for input, expected := range dateTable {
Expand Down

0 comments on commit 8d1c15e

Please sign in to comment.