Skip to content

Commit

Permalink
fixed edge case in year guessing mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
jakopako committed Nov 19, 2023
1 parent 06b6220 commit 2ac7472
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,15 @@ func getDate(f *Field, s *goquery.Selection) (time.Time, error) {
}
}
}

// currently not all date parts have default values
if !combinedParts.Day || !combinedParts.Month {
return t, errors.New("date parsing error: to generate a date at least a day and a month is needed")
}

// adding default values where necessary
currentYear := time.Now().Year()
if !combinedParts.Year {
currentYear := time.Now().Year()
dateParts = append(dateParts, datePart{
stringPart: strconv.Itoa(currentYear),
layoutParts: []string{"2006"},
Expand All @@ -636,10 +642,6 @@ func getDate(f *Field, s *goquery.Selection) (time.Time, error) {
layoutParts: []string{"15:04"},
})
}
// currently not all date parts have default values
if !combinedParts.Day || !combinedParts.Month {
return t, errors.New("date parsing error: to generate a date at least a day and a month is needed")
}

var dateTimeString string
dateTimeLayouts := []string{""}
Expand All @@ -653,10 +655,25 @@ func getDate(f *Field, s *goquery.Selection) (time.Time, error) {
}
dateTimeString += dp.stringPart + " "
}

for _, dateTimeLayout := range dateTimeLayouts {
t, err = monday.ParseInLocation(dateTimeLayout, dateTimeString, loc, monday.Locale(mLocale))
if err == nil {
return t, nil
} else if !combinedParts.Year && f.GuessYear {
// edge case, parsing time "29.02. 20:00 2023 ": day out of range
// We set the year to the current year but it should actually be 2024
// We only update the year string in case guess_year is set to true
// to not confuse the user too much
if strings.HasSuffix(err.Error(), "day out of range") && strings.Contains(err.Error(), "29") {
for i := 1; i < 4; i++ {
dateTimeString = strings.Replace(dateTimeString, strconv.Itoa(currentYear), strconv.Itoa(currentYear+i), 1)
t, err = monday.ParseInLocation(dateTimeLayout, dateTimeString, loc, monday.Locale(mLocale))
if err == nil {
return t, nil
}
}
}
}
}
return t, err
Expand Down

0 comments on commit 2ac7472

Please sign in to comment.