feat: add java.sql.Time converter support - #1092
SummerC0zyR0ck wants to merge 5 commits into
Conversation
57b95ff to
d07e5be
Compare
9440e55 to
5dee8dc
Compare
|
Since the converter respects the configured For example, "HH:mm:ss a" produces "AM/PM" under |
Thanks for the suggestion! I added a locale-specific formatting test covering |
| import org.apache.poi.ss.usermodel.DateUtil; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
There was a problem hiding this comment.
Please add @Tag(Tags.UNIT).
There was a problem hiding this comment.
Thanks for the review! Added @tag(Tags.UNIT) to SqlTimeConverterTest along with the required imports.
| @Override | ||
| public Time convertToJavaData( | ||
| ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { | ||
| return Time.valueOf(DateUtils.getLocalTime( |
There was a problem hiding this comment.
An invalid argument here causes NPE: DateUtils.getLocalTime returns null for it, and Time.valueOf(null) fails. However, the other converters like LocalTimeNumberConverter return null in this case, matching POI's DateUtil.
Should we keep that behaviour?
LocalTime localTime = DateUtils.getLocalTime(
cellData.getNumberValue().doubleValue(), DateUtils.isDate1904(contentProperty, globalConfiguration));
return localTime == null ? null : Time.valueOf(localTime);There was a problem hiding this comment.
Good catch — you're right. DateUtils.getLocalTime returns null for an invalid Excel serial, and Time.valueOf(null) throws an NPE. I've updated the converter to mirror LocalTimeNumberConverter:
LocalTime localTime = DateUtils.getLocalTime(
cellData.getNumberValue().doubleValue(), DateUtils.isDate1904(contentProperty, globalConfiguration));
return localTime == null ? null : Time.valueOf(localTime);I also added a regression test (invalidNumberReturnsNull) to lock in the behavior. Thanks for pointing it out!
Purpose of the pull request
Related: #1017
Add first-class converter support for
java.sql.Time, following the existingLocalTimeconverter pattern.What's changed?
java.sql.Timerepresents a time-of-day value, but converter lookup uses the declared Java class. Therefore, the existingjava.util.DateandLocalTimeconverters do not apply to fields declared asjava.sql.Time.This PR adds a converter family under
org.apache.fesod.sheet.converters.sqltime:SqlTimeDateConverter- default write path using an ExcelDATEcell. It attachesDateUtils.EPOCH(1970-01-01) and appliesHH:mm:ssby default.SqlTimeNumberConverter- bidirectional Excel numeric serial conversion, includinguse1904windowing. Reading extracts the time component and discards any date component.SqlTimeStringConverter- bidirectionalSTRINGconversion. It defaults toHH:mm:ss, auto-detectsHH:mminput, and respects@DateTimeFormatand the configuredLocale.The converters reuse the existing
LocalTimeparsing, formatting, and Excel serial handling throughTime.toLocalTime()andTime.valueOf(LocalTime). No time-zone conversion is introduced.Registration in
DefaultConverterLoaderfollows theLocalTimepattern:initAllConverter()Tests
Tests cover converter keys, DATE/NUMBER/STRING conversion, custom date-time formats, 1904 date windowing, discarding the date component from numeric values, registry immutability, and round-trip behavior for XLSX, XLS, and CSV.
The complete
fesod-sheettest suite passes with 923 tests, and bothspotless:checkand Apache RAT checks are green.Scope is limited to
java.sql.Timeand remains JDK 8 compatible.