diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java index f17d9b62c..7d1c765d0 100644 --- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java @@ -70,6 +70,9 @@ import org.apache.fesod.sheet.converters.longconverter.LongBooleanConverter; import org.apache.fesod.sheet.converters.longconverter.LongNumberConverter; import org.apache.fesod.sheet.converters.longconverter.LongStringConverter; +import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeDateConverter; +import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeNumberConverter; +import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeStringConverter; import org.apache.fesod.sheet.converters.shortconverter.ShortBooleanConverter; import org.apache.fesod.sheet.converters.shortconverter.ShortNumberConverter; import org.apache.fesod.sheet.converters.shortconverter.ShortStringConverter; @@ -122,6 +125,8 @@ private static void initAllConverter() { putAllConverter(new LocalTimeNumberConverter()); putAllConverter(new LocalTimeStringConverter()); + putAllConverter(new OffsetDateTimeNumberConverter()); + putAllConverter(new OffsetDateTimeStringConverter()); putAllConverter(new DoubleBooleanConverter()); putAllConverter(new DoubleNumberConverter()); @@ -160,6 +165,7 @@ private static void initDefaultWriteConverter() { putWriteConverter(new LocalDateTimeDateConverter()); putWriteConverter(new LocalDateDateConverter()); putWriteConverter(new LocalTimeDateConverter()); + putWriteConverter(new OffsetDateTimeDateConverter()); putWriteConverter(new DoubleNumberConverter()); putWriteConverter(new FloatNumberConverter()); putWriteConverter(new IntegerNumberConverter()); @@ -181,6 +187,7 @@ private static void initDefaultWriteConverter() { putWriteStringConverter(new LocalDateStringConverter()); putWriteStringConverter(new LocalDateTimeStringConverter()); putWriteStringConverter(new LocalTimeStringConverter()); + putWriteStringConverter(new OffsetDateTimeStringConverter()); putWriteStringConverter(new DoubleStringConverter()); putWriteStringConverter(new FloatStringConverter()); putWriteStringConverter(new IntegerStringConverter()); diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeDateConverter.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeDateConverter.java new file mode 100644 index 000000000..8a032cfb1 --- /dev/null +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeDateConverter.java @@ -0,0 +1,61 @@ +/* + * 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.common.util.StringUtils; +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 { + @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(); + if (StringUtils.isEmpty(format)) { + format = null; + } + } + WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat); + return cellData; + } +} diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeNumberConverter.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeNumberConverter.java new file mode 100644 index 000000000..697f46640 --- /dev/null +++ b/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 { + @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; + } + } + Boolean globalUse1904windowing = globalConfiguration.getUse1904windowing(); + return globalUse1904windowing != null && globalUse1904windowing; + } +} diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeStringConverter.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeStringConverter.java new file mode 100644 index 000000000..6bf891425 --- /dev/null +++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeStringConverter.java @@ -0,0 +1,118 @@ +/* + * 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 java.util.Map; +import org.apache.fesod.common.util.MapUtils; +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 { + /** + * Thread-local cache of {@link DateTimeFormatter} instances, keyed by pattern and locale, so + * the per-cell hot path does not rebuild formatters for every conversion. + */ + private static final ThreadLocal> FORMATTER_CACHE = new ThreadLocal<>(); + + @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; + } + final String pattern = format; + final Locale actualLocale = locale; + Map cache = FORMATTER_CACHE.get(); + if (cache == null) { + cache = MapUtils.newHashMap(); + FORMATTER_CACHE.set(cache); + } + return cache.computeIfAbsent( + pattern + '\0' + actualLocale, key -> DateTimeFormatter.ofPattern(pattern, actualLocale)); + } +} diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/OffsetDateTimeConverterTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/OffsetDateTimeConverterTest.java new file mode 100644 index 000000000..ce3fd52e3 --- /dev/null +++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converter/OffsetDateTimeConverterTest.java @@ -0,0 +1,209 @@ +/* + * 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.converter; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import java.math.BigDecimal; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.format.DateTimeParseException; +import org.apache.fesod.sheet.converters.ConverterKeyBuild; +import org.apache.fesod.sheet.converters.DefaultConverterLoader; +import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeDateConverter; +import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeNumberConverter; +import org.apache.fesod.sheet.converters.offsetdatetime.OffsetDateTimeStringConverter; +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.DateTimeFormatProperty; +import org.apache.fesod.sheet.metadata.property.ExcelContentProperty; +import org.apache.fesod.sheet.testkit.Tags; +import org.apache.fesod.sheet.util.DateUtils; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +@Tag(Tags.UNIT) +class OffsetDateTimeConverterTest { + private static final OffsetDateTime VALUE = OffsetDateTime.of(2020, 1, 2, 3, 4, 5, 0, ZoneOffset.ofHours(8)); + + @Test + void dateConverterDropsOffsetWhilePreservingLocalDateTime() throws Exception { + WriteCellData result = + new OffsetDateTimeDateConverter().convertToExcelData(VALUE, null, new GlobalConfiguration()); + assertEquals(CellDataTypeEnum.DATE, result.getType()); + assertEquals(VALUE.toLocalDateTime(), result.getDateValue()); + } + + @Test + void numberConverterDropsOffsetWhilePreservingLocalDateTime() { + OffsetDateTimeNumberConverter converter = new OffsetDateTimeNumberConverter(); + GlobalConfiguration globalConfiguration = new GlobalConfiguration(); + WriteCellData written = converter.convertToExcelData(VALUE, null, globalConfiguration); + OffsetDateTime read = + converter.convertToJavaData(new ReadCellData<>(written.getNumberValue()), null, globalConfiguration); + assertEquals(VALUE.toLocalDateTime().atZone(ZoneId.systemDefault()).toOffsetDateTime(), read); + } + + @Test + void stringConverterPreservesOffsetInIsoText() throws Exception { + OffsetDateTimeStringConverter converter = new OffsetDateTimeStringConverter(); + GlobalConfiguration globalConfiguration = new GlobalConfiguration(); + WriteCellData written = converter.convertToExcelData(VALUE, null, globalConfiguration); + assertEquals( + VALUE, + converter.convertToJavaData(new ReadCellData<>(written.getStringValue()), null, globalConfiguration)); + } + + @Test + void stringConverterUsesConfiguredPattern() throws Exception { + OffsetDateTimeStringConverter converter = new OffsetDateTimeStringConverter(); + ExcelContentProperty property = new ExcelContentProperty(); + property.setDateTimeFormatProperty(new DateTimeFormatProperty("yyyy-MM-dd HH:mm:ss Z", false)); + GlobalConfiguration globalConfiguration = new GlobalConfiguration(); + assertEquals( + "2020-01-02 03:04:05 +0800", + converter + .convertToExcelData(VALUE, property, globalConfiguration) + .getStringValue()); + } + + @Test + void stringConverterFallsBackToLocalDateTimeWhenOffsetIsMissing() throws Exception { + OffsetDateTimeStringConverter converter = new OffsetDateTimeStringConverter(); + GlobalConfiguration globalConfiguration = new GlobalConfiguration(); + ReadCellData cellData = new ReadCellData<>("2020-01-02T03:04:05"); + assertEquals( + VALUE.toLocalDateTime().atZone(ZoneId.systemDefault()).toOffsetDateTime(), + converter.convertToJavaData(cellData, null, globalConfiguration)); + } + + @Test + void stringConverterParsesDefaultSpaceSeparatedFormat() throws Exception { + OffsetDateTimeStringConverter converter = new OffsetDateTimeStringConverter(); + GlobalConfiguration globalConfiguration = new GlobalConfiguration(); + ReadCellData cellData = new ReadCellData<>("2020-01-02 03:04:05"); + assertEquals( + VALUE.toLocalDateTime().atZone(ZoneId.systemDefault()).toOffsetDateTime(), + converter.convertToJavaData(cellData, null, globalConfiguration)); + } + + @Test + void stringConverterRejectsTextNotMatchingConfiguredPattern() { + OffsetDateTimeStringConverter converter = new OffsetDateTimeStringConverter(); + ExcelContentProperty property = new ExcelContentProperty(); + property.setDateTimeFormatProperty(new DateTimeFormatProperty("yyyy/MM/dd HH:mm:ss", false)); + GlobalConfiguration globalConfiguration = new GlobalConfiguration(); + ReadCellData cellData = new ReadCellData<>("2020-01-02T03:04:05"); + assertThrows( + DateTimeParseException.class, + () -> converter.convertToJavaData(cellData, property, globalConfiguration)); + } + + @Test + void stringConverterUsesDefaultLocaleWhenLocaleIsNull() throws Exception { + OffsetDateTimeStringConverter converter = new OffsetDateTimeStringConverter(); + ExcelContentProperty property = new ExcelContentProperty(); + property.setDateTimeFormatProperty(new DateTimeFormatProperty("yyyy-MM-dd HH:mm:ss Z", false)); + GlobalConfiguration globalConfiguration = new GlobalConfiguration(); + globalConfiguration.setLocale(null); + assertEquals( + "2020-01-02 03:04:05 +0800", + converter + .convertToExcelData(VALUE, property, globalConfiguration) + .getStringValue()); + } + + @Test + void numberConverterReturnsNullForInvalidExcelDate() { + OffsetDateTimeNumberConverter converter = new OffsetDateTimeNumberConverter(); + GlobalConfiguration globalConfiguration = new GlobalConfiguration(); + assertNull(converter.convertToJavaData(new ReadCellData<>(BigDecimal.valueOf(-1)), null, globalConfiguration)); + } + + @Test + void dateConverterFallsBackToDefaultFormatForEmptyDateTimeFormat() throws Exception { + OffsetDateTimeDateConverter converter = new OffsetDateTimeDateConverter(); + ExcelContentProperty property = new ExcelContentProperty(); + property.setDateTimeFormatProperty(new DateTimeFormatProperty("", false)); + WriteCellData result = converter.convertToExcelData(VALUE, property, new GlobalConfiguration()); + assertEquals( + DateUtils.defaultDateFormat, + result.getWriteCellStyle().getDataFormatData().getFormat()); + } + + @Test + void numberConverterDefaultsNullUse1904windowingToFalse() { + OffsetDateTimeNumberConverter converter = new OffsetDateTimeNumberConverter(); + GlobalConfiguration globalConfiguration = new GlobalConfiguration(); + globalConfiguration.setUse1904windowing(null); + WriteCellData written = converter.convertToExcelData(VALUE, null, globalConfiguration); + assertNotNull(written.getNumberValue()); + } + + @Test + void stringConverterWithEmptyOrNullPatternFallsBackToIso() throws Exception { + OffsetDateTimeStringConverter converter = new OffsetDateTimeStringConverter(); + GlobalConfiguration globalConfiguration = new GlobalConfiguration(); + + ExcelContentProperty emptyProperty = new ExcelContentProperty(); + emptyProperty.setDateTimeFormatProperty(new DateTimeFormatProperty("", false)); + WriteCellData writtenEmpty = converter.convertToExcelData(VALUE, emptyProperty, globalConfiguration); + assertEquals( + VALUE, + converter.convertToJavaData( + new ReadCellData<>(writtenEmpty.getStringValue()), emptyProperty, globalConfiguration)); + + ExcelContentProperty nullFormatProperty = new ExcelContentProperty(); + nullFormatProperty.setDateTimeFormatProperty(new DateTimeFormatProperty(null, false)); + WriteCellData writtenNullFormat = + converter.convertToExcelData(VALUE, nullFormatProperty, globalConfiguration); + assertEquals( + VALUE, + converter.convertToJavaData( + new ReadCellData<>(writtenNullFormat.getStringValue()), + nullFormatProperty, + globalConfiguration)); + } + + @Test + void convertersAreRegisteredForSupportedDirections() { + assertEquals( + OffsetDateTimeDateConverter.class, + DefaultConverterLoader.loadDefaultWriteConverter() + .get(ConverterKeyBuild.buildKey(OffsetDateTime.class)) + .getClass()); + assertEquals( + 2, + DefaultConverterLoader.loadAllConverter().entrySet().stream() + .filter(entry -> entry.getKey().getClazz() == OffsetDateTime.class) + .count()); + } +}