Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<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();
if (StringUtils.isEmpty(format)) {
format = null;
}
}
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat);
return cellData;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Comment on lines +73 to +77

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use1904windowing DEFAULT being unboxed to false — Agreed this is a real issue, but it's a pre-existing framework-level problem: DateTimeFormatProperty.build converts BooleanEnum.DEFAULT (null) to false (DateTimeFormatProperty.java:55-57), and all existing number converters (Date, LocalDate, LocalDateTime, ZonedDateTime) consume the property value without a global fallback. This PR's null-safe fallback covers the no-annotation path; the annotated path behaves identically to the existing converter families. Fixing it properly means changing DateTimeFormatProperty (preserving DEFAULT as null) and updating every date-number converter — a framework-wide change that deserves its own issue/PR. Happy to open one if that's useful.

}
}
Boolean globalUse1904windowing = globalConfiguration.getUse1904windowing();
return globalUse1904windowing != null && globalUse1904windowing;
}
}
Original file line number Diff line number Diff line change
@@ -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<OffsetDateTime> {
/**
* 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<Map<String, DateTimeFormatter>> 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<String, DateTimeFormatter> 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));
}
}
Loading
Loading