Skip to content

Commit

Permalink
Handle MySQL Year data type with value <=1900 and >2155
Browse files Browse the repository at this point in the history
Signed-off-by: Dinu John <[email protected]>
  • Loading branch information
dinujoh committed Nov 15, 2024
1 parent fe2c1fb commit c1d495c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,15 @@ private Long parseDateTimeStrAsEpochMillis(final String dateTimeStr) {
private Long handleYear(final String yearStr) {
try {
// MySQL YEAR values are typically four-digit numbers (e.g., 2024).
return Long.parseLong(yearStr);
final long year = Long.parseLong(yearStr);

// MySQL converts values in 1- or 2-digit strings in the range '0' to '99' to YYYY format
// MySQL YEAR values in YYYY format are with a range of 1901 to 2155. Outside this range the value is 0.
if (year <= 1900 || year > 2155) {
return 0L;
}

return year;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid year format: " + yearStr, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,13 @@ void handle_withYearType_returnsCorrectEpochMillis(String input, long expected)
private static Stream<Arguments> provideYearTestCases() {
return Stream.of(
Arguments.of("2023", 2023),
Arguments.of("1900", 1900),
Arguments.of("1997", 1997)
Arguments.of("1900", 0),
Arguments.of("1997", 1997),
Arguments.of("1889", 0),
Arguments.of("1901", 1901),
Arguments.of("2155", 2155),
Arguments.of("2156", 0),
Arguments.of("3015", 0)
);
}

Expand Down

0 comments on commit c1d495c

Please sign in to comment.