Copilot commented on code in PR #1033: URL: https://github.com/apache/fesod/pull/1033#discussion_r3838270093
########## fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeDateConverter.java: ########## @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * This file is part of the Apache Fesod (Incubating) project, which was derived from Alibaba EasyExcel. + * + * Copyright (C) 2018-2024 Alibaba Group Holding Ltd. + */ + +package org.apache.fesod.sheet.converters.offsetdatetime; + +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import org.apache.fesod.sheet.converters.Converter; +import org.apache.fesod.sheet.metadata.GlobalConfiguration; +import org.apache.fesod.sheet.metadata.data.WriteCellData; +import org.apache.fesod.sheet.metadata.property.ExcelContentProperty; +import org.apache.fesod.sheet.util.DateUtils; +import org.apache.fesod.sheet.util.WorkBookUtil; + +/** OffsetDateTime and date converter. */ +public class OffsetDateTimeDateConverter implements Converter<OffsetDateTime> { + @Override + public Class<?> supportJavaTypeKey() { + return OffsetDateTime.class; + } + + @Override + public WriteCellData<?> convertToExcelData( + OffsetDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) + throws Exception { + LocalDateTime localDateTime = value.toLocalDateTime(); + WriteCellData<?> cellData = new WriteCellData<>(localDateTime); + String format = null; + if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) { + format = contentProperty.getDateTimeFormatProperty().getFormat(); + } + WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat); Review Comment: When a field has `@DateTimeFormat` without a value, `DateTimeFormatProperty.format` is the empty string. `WorkBookUtil.fillDataFormat` only uses its default when the format is `null` (see `WorkBookUtil.java:158-163`), so this writes the native date with an empty/General number format instead of the advertised `yyyy-MM-dd HH:mm:ss` default. Normalize an empty format to `null` before filling the data format and add a regression test for an empty annotation format. ########## fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeNumberConverter.java: ########## @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * This file is part of the Apache Fesod (Incubating) project, which was derived from Alibaba EasyExcel. + * + * Copyright (C) 2018-2024 Alibaba Group Holding Ltd. + */ + +package org.apache.fesod.sheet.converters.offsetdatetime; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import org.apache.fesod.sheet.converters.Converter; +import org.apache.fesod.sheet.enums.CellDataTypeEnum; +import org.apache.fesod.sheet.metadata.GlobalConfiguration; +import org.apache.fesod.sheet.metadata.data.ReadCellData; +import org.apache.fesod.sheet.metadata.data.WriteCellData; +import org.apache.fesod.sheet.metadata.property.ExcelContentProperty; +import org.apache.fesod.sheet.util.DateUtils; +import org.apache.poi.ss.usermodel.DateUtil; + +/** OffsetDateTime and number converter. */ +public class OffsetDateTimeNumberConverter implements Converter<OffsetDateTime> { + @Override + public Class<?> supportJavaTypeKey() { + return OffsetDateTime.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.NUMBER; + } + + @Override + public OffsetDateTime convertToJavaData( + ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { + LocalDateTime localDateTime = DateUtils.getLocalDateTime( + cellData.getNumberValue().doubleValue(), resolveUse1904windowing(contentProperty, globalConfiguration)); + if (localDateTime == null) { + return null; + } + return localDateTime.atZone(ZoneId.systemDefault()).toOffsetDateTime(); + } + + @Override + public WriteCellData<?> convertToExcelData( + OffsetDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { + return new WriteCellData<>(BigDecimal.valueOf(DateUtil.getExcelDate( + value.toLocalDateTime(), resolveUse1904windowing(contentProperty, globalConfiguration)))); + } + + private boolean resolveUse1904windowing( + ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { + if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) { + Boolean propertyUse1904windowing = + contentProperty.getDateTimeFormatProperty().getUse1904windowing(); + if (propertyUse1904windowing != null) { + return propertyUse1904windowing; Review Comment: The global fallback here is bypassed for normal annotated fields: `DateTimeFormatProperty.build` converts `BooleanEnum.DEFAULT`'s `null` to `false` (`DateTimeFormatProperty.java:55-57`). Consequently, reading a 1904-windowed workbook into an `@DateTimeFormat` `OffsetDateTime` field uses 1900 serials and shifts the value by 1462 days. Preserve `DEFAULT` as `null` (and update the existing date-number converters that unbox this property) so only an explicitly configured property value overrides the workbook/global setting. ########## fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeStringConverter.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * This file is part of the Apache Fesod (Incubating) project, which was derived from Alibaba EasyExcel. + * + * Copyright (C) 2018-2024 Alibaba Group Holding Ltd. + */ + +package org.apache.fesod.sheet.converters.offsetdatetime; + +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.Locale; +import org.apache.fesod.common.util.StringUtils; +import org.apache.fesod.sheet.converters.Converter; +import org.apache.fesod.sheet.enums.CellDataTypeEnum; +import org.apache.fesod.sheet.metadata.GlobalConfiguration; +import org.apache.fesod.sheet.metadata.data.ReadCellData; +import org.apache.fesod.sheet.metadata.data.WriteCellData; +import org.apache.fesod.sheet.metadata.property.ExcelContentProperty; +import org.apache.fesod.sheet.util.DateUtils; + +/** OffsetDateTime and string converter. */ +public class OffsetDateTimeStringConverter implements Converter<OffsetDateTime> { + @Override + public Class<?> supportJavaTypeKey() { + return OffsetDateTime.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public OffsetDateTime convertToJavaData( + ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { + String stringValue = cellData.getStringValue(); + String format = format(contentProperty); + DateTimeFormatter formatter = formatter(contentProperty, globalConfiguration.getLocale()); + try { + return OffsetDateTime.parse(stringValue, formatter); + } catch (DateTimeParseException e) { + try { + return DateUtils.parseLocalDateTime(stringValue, format, globalConfiguration.getLocale()) + .atZone(ZoneId.systemDefault()) + .toOffsetDateTime(); + } catch (RuntimeException inner) { + if (StringUtils.isEmpty(format)) { + return LocalDateTime.parse(stringValue, DateTimeFormatter.ISO_LOCAL_DATE_TIME) + .atZone(ZoneId.systemDefault()) + .toOffsetDateTime(); + } + throw inner; + } + } + } + + @Override + public WriteCellData<?> convertToExcelData( + OffsetDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { + return new WriteCellData<>(value.format(formatter(contentProperty, globalConfiguration.getLocale()))); + } + + private String format(ExcelContentProperty contentProperty) { + if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { + return null; + } + return contentProperty.getDateTimeFormatProperty().getFormat(); + } + + private DateTimeFormatter formatter(ExcelContentProperty contentProperty, Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + String format = format(contentProperty); + if (StringUtils.isEmpty(format)) { + return DateTimeFormatter.ISO_OFFSET_DATE_TIME; + } + return DateTimeFormatter.ofPattern(format, locale); Review Comment: For every non-empty pattern, this creates a new `DateTimeFormatter` during each cell conversion. `LocalDateTimeStringConverter` uses `DateUtils`' thread-local formatter cache instead; formatted `OffsetDateTime` columns can therefore repeatedly parse the same pattern in the per-cell hot path. Cache formatters by pattern and locale (or reuse the existing cache) here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
