Mikkey-f commented on code in PR #1033: URL: https://github.com/apache/fesod/pull/1033#discussion_r3960161642
########## fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeStringConverter.java: ########## @@ -0,0 +1,112 @@ +/* + * 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. + */ + +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<>(); Review Comment: Agreed — I've consolidated this into `DateUtils`. It now provides `DateUtils#parseOffsetDateTime(String, String, Locale)` and a `format(OffsetDateTime, String, Locale)` overload, both backed by the bounded `DATE_TIME_FORMATTER_THREAD_LOCAL` cache that `removeThreadLocalCache()` clears at the end of each read/write context. `OffsetDateTimeStringConverter` now delegates to these helpers (as `LocalDateTimeStringConverter` does) and no longer keeps its own converter-local `ThreadLocal`. When the format is empty, both helpers fall back to `ISO_OFFSET_DATE_TIME`. ########## fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/offsetdatetime/OffsetDateTimeStringConverter.java: ########## @@ -0,0 +1,112 @@ +/* + * 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. + */ + +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; + } + } Review Comment: Agreed — offset-less text should not be silently re-interpreted in `ZoneId.systemDefault()`, since the same text would then resolve to different instants depending on the server timezone. I've removed the fallback: STRING reads now parse strictly with the configured pattern, or `ISO_OFFSET_DATE_TIME` when no format is set, and propagate `DateTimeParseException` for text without an explicit offset. Note this also applies to configured patterns without an offset section, which can only ever produce offset-less text — wall-clock-only columns are better served by the `LocalDateTime` converters. Tests updated: the two previous fallback cases now assert failure, and a round-trip case for a configured offset pattern was added. -- 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]
