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 @@ -55,6 +55,7 @@
import org.apache.fesod.sheet.converters.floatconverter.FloatNumberConverter;
import org.apache.fesod.sheet.converters.floatconverter.FloatStringConverter;
import org.apache.fesod.sheet.converters.inputstream.InputStreamImageConverter;
import org.apache.fesod.sheet.converters.instant.InstantStringConverter;
import org.apache.fesod.sheet.converters.integer.IntegerBooleanConverter;
import org.apache.fesod.sheet.converters.integer.IntegerNumberConverter;
import org.apache.fesod.sheet.converters.integer.IntegerStringConverter;
Expand Down Expand Up @@ -123,6 +124,8 @@ private static void initAllConverter() {
putAllConverter(new LocalTimeNumberConverter());
putAllConverter(new LocalTimeStringConverter());

putAllConverter(new InstantStringConverter());

putAllConverter(new DoubleBooleanConverter());
putAllConverter(new DoubleNumberConverter());
putAllConverter(new DoubleStringConverter());
Expand Down Expand Up @@ -160,6 +163,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new LocalDateDateConverter());
putWriteConverter(new LocalTimeDateConverter());
putWriteConverter(new InstantStringConverter());
putWriteConverter(new DoubleNumberConverter());
putWriteConverter(new FloatNumberConverter());
putWriteConverter(new IntegerNumberConverter());
Expand All @@ -181,6 +185,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new LocalDateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new LocalTimeStringConverter());
putWriteStringConverter(new InstantStringConverter());
putWriteStringConverter(new DoubleStringConverter());
putWriteStringConverter(new FloatStringConverter());
putWriteStringConverter(new IntegerStringConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.instant;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
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;

/**
* Instant and ISO-8601 string converter.
*/
public class InstantStringConverter implements Converter<Instant> {

@Override
public Class<Instant> supportJavaTypeKey() {
return Instant.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}

@Override
public Instant convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
String value = cellData.getStringValue();
OffsetDateTime offsetDateTime = OffsetDateTime.parse(value);
if (!offsetDateTime.getOffset().equals(ZoneOffset.UTC)) {
throw new DateTimeParseException("Instant value must use a UTC offset", value, 0);
}
return offsetDateTime.toInstant();
}
Comment on lines +48 to +57

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The current implementation of InstantStringConverter has a semantic inconsistency between read and write behavior.

  • convertToJavaData(...): uses OffsetDateTime.parse(...).toInstant(), which accepts any offset (e.g., +08:00) and normalizes the value to UTC.
  • convertToExcelData(): always outputs Instant.toString(), which is strictly UTC.

This means that an Excel value like "2026-09-13T12:12:12+08:00" will be read as "2026-09-13T04:12:12Z", and writing it back will produce a different string. The round‑trip behavior is not symmetric.

Since Instant represents an absolute UTC timestamp, the converter should enforce that the input string is already in UTC. Otherwise the converter silently changes the timestamp.

I suggest adding a validation step in convertToJavaData to reject non‑UTC offsets. This keeps the read/write semantics consistent and avoids unexpected timezone normalization.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated. The read path now parses the offset explicitly and rejects any non-zero offset with DateTimeParseException; Z and +00:00 remain valid UTC inputs, and writes stay canonical Z. I added a regression test for rejecting +08:00. The focused tests, Spotless, and the full package run all pass (924 tests) on commit f0af0eb.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggest using getOffset().equals(ZoneOffset.UTC) ensures that the input is truly UTC (Z or +00:00) and improves readability.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated to compare the parsed offset directly with ZoneOffset.UTC, as suggested. The focused tests, Spotless, and full package run all pass (924 tests) on commit 4a7c6bb.


@Override
public WriteCellData<?> convertToExcelData(
Instant value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(value.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

package org.apache.fesod.sheet.converters;

import java.time.Instant;
import java.time.LocalTime;
import java.util.Map;
import org.apache.fesod.sheet.converters.ConverterKeyBuild.ConverterKey;
import org.apache.fesod.sheet.converters.instant.InstantStringConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeDateConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeNumberConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeStringConverter;
Expand Down Expand Up @@ -67,6 +69,21 @@ void loadConvertersRegistersLocalTimeFamily() {
writeConverter.get(ConverterKeyBuild.buildKey(LocalTime.class, CellDataTypeEnum.STRING)));
}

@Test
void loadConvertersRegistersInstantStringConverter() {
Map<ConverterKey, Converter<?>> allConverter = DefaultConverterLoader.loadAllConverter();
Assertions.assertInstanceOf(
InstantStringConverter.class,
allConverter.get(ConverterKeyBuild.buildKey(Instant.class, CellDataTypeEnum.STRING)));

Map<ConverterKey, Converter<?>> writeConverter = DefaultConverterLoader.loadDefaultWriteConverter();
Assertions.assertInstanceOf(
InstantStringConverter.class, writeConverter.get(ConverterKeyBuild.buildKey(Instant.class)));
Assertions.assertInstanceOf(
InstantStringConverter.class,
writeConverter.get(ConverterKeyBuild.buildKey(Instant.class, CellDataTypeEnum.STRING)));
}

private static void assertLoadIsImmutableAndCopyIsMutable(
Map<ConverterKey, Converter<?>> loaded, Map<ConverterKey, Converter<?>> copy) {
Map.Entry<ConverterKey, Converter<?>> entry =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.instant;

import java.time.Instant;
import java.time.format.DateTimeParseException;
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.testkit.Tags;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
* Tests {@link InstantStringConverter}.
*/
@Tag(Tags.UNIT)
class InstantStringConverterTest {

private static final GlobalConfiguration GLOBAL_CONFIGURATION = new GlobalConfiguration();
private final InstantStringConverter converter = new InstantStringConverter();

@Test
void supportKeys() {
Assertions.assertEquals(Instant.class, converter.supportJavaTypeKey());
Assertions.assertEquals(CellDataTypeEnum.STRING, converter.supportExcelTypeKey());
}

@ParameterizedTest
@CsvSource({
"1970-01-01T00:00:00Z, 1970-01-01T00:00:00Z",
"2026-09-08T12:34:56.123456789Z, 2026-09-08T12:34:56.123456789Z",
"2026-09-08T12:34:56+00:00, 2026-09-08T12:34:56Z"
})
void convertToJavaDataParsesUtcIso8601(String value, String expected) {
Instant actual = converter.convertToJavaData(new ReadCellData<>(value), null, GLOBAL_CONFIGURATION);

Assertions.assertEquals(Instant.parse(expected), actual);
}

@Test
void convertToJavaDataRejectsNonUtcOffset() {
Assertions.assertThrows(
DateTimeParseException.class,
() -> converter.convertToJavaData(
new ReadCellData<>("2026-09-08T20:34:56+08:00"), null, GLOBAL_CONFIGURATION));
}

@Test
void convertRoundTripPreservesNanoseconds() {
Instant value = Instant.parse("2026-09-08T12:34:56.123456789Z");

WriteCellData<?> written = converter.convertToExcelData(value, null, GLOBAL_CONFIGURATION);
Instant actual =
converter.convertToJavaData(new ReadCellData<>(written.getStringValue()), null, GLOBAL_CONFIGURATION);

Assertions.assertEquals(CellDataTypeEnum.STRING, written.getType());
Assertions.assertEquals("2026-09-08T12:34:56.123456789Z", written.getStringValue());
Assertions.assertEquals(value, actual);
}

@Test
void convertToJavaDataRejectsInvalidInstant() {
Assertions.assertThrows(
DateTimeParseException.class,
() -> converter.convertToJavaData(new ReadCellData<>("not-an-instant"), null, GLOBAL_CONFIGURATION));
}
}
Loading