-
Notifications
You must be signed in to change notification settings - Fork 519
feat: add java.time.OffsetDateTime converters (#1017) #1033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mikkey-f
wants to merge
4
commits into
apache:main
Choose a base branch
from
Mikkey-f:feat/offsetdatetime-converters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9b5dcd3
feat: add OffsetDateTime converter family (#1017)
Mikkey-f 49ea421
fix: harden OffsetDateTime converters after review (#1017)
Mikkey-f d43e58a
fix: null-safe global use1904windowing default in OffsetDateTime conv…
Mikkey-f 7dd544e
fix: normalize empty date format and cache formatters (#1017)
Mikkey-f File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...in/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeDateConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
83 changes: 83 additions & 0 deletions
83
.../java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeNumberConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| Boolean globalUse1904windowing = globalConfiguration.getUse1904windowing(); | ||
| return globalUse1904windowing != null && globalUse1904windowing; | ||
| } | ||
| } | ||
118 changes: 118 additions & 0 deletions
118
.../java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeStringConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.