Skip to content

Commit

Permalink
feat(decode): Add checks for impossible date or time
Browse files Browse the repository at this point in the history
Discovered missing check while addressing failing
`TestTOMLTest_Invalid_Datetime_ImpossibleDate` test case.

Related to pelletier#613
  • Loading branch information
jidicula committed Oct 10, 2021
1 parent e86a442 commit 9d45398
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func parseLocalDate(b []byte) (LocalDate, error) {

date.Day = parseDecimalDigits(b[8:10])

dateString := fmt.Sprintf("%04d-%02d-%02d",
date.Year, date.Month, date.Day)
_, err := time.Parse("2006-01-02", dateString)
if err != nil {
return LocalDate{}, newDecodeError(b, "invalid local date")
}

return date, nil
}

Expand Down Expand Up @@ -144,6 +151,13 @@ func parseLocalDateTime(b []byte) (LocalDateTime, []byte, error) {
}
dt.LocalTime = t

localDateTimeString := fmt.Sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",
dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second)
_, err = time.Parse(time.RFC3339, localDateTimeString)
if err != nil {
return LocalDateTime{}, rest, newDecodeError(b, "invalid local date-time")
}

return dt, rest, nil
}

Expand Down Expand Up @@ -202,6 +216,13 @@ func parseLocalTime(b []byte) (LocalTime, []byte, error) {
return t, b[9+digits:], nil
}

localTimeString := fmt.Sprintf("%02d:%02d:%02d",
t.Hour, t.Minute, t.Second)
_, err := time.Parse("15:04:05", localTimeString)
if err != nil {
return LocalTime{}, b, newDecodeError(b, "invalid local time")
}

return t, b[8:], nil
}

Expand Down

0 comments on commit 9d45398

Please sign in to comment.