-
Notifications
You must be signed in to change notification settings - Fork 532
feat: support Instant string conversion #1089
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
56wj
wants to merge
4
commits into
apache:main
Choose a base branch
from
56wj:feat/instant-string-converter
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
3b7ae8f
feat: support Instant string conversion
56wj 02a9f85
Merge branch 'main' into feat/instant-string-converter
bengbengbalabalabeng f0af0eb
fix: reject non-UTC offsets in Instant converter
56wj 4a7c6bb
refactor: compare Instant offset with UTC
56wj 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
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
64 changes: 64 additions & 0 deletions
64
...sheet/src/main/java/org/apache/fesod/sheet/converters/instant/InstantStringConverter.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,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(); | ||
| } | ||
|
|
||
| @Override | ||
| public WriteCellData<?> convertToExcelData( | ||
| Instant value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { | ||
| return new WriteCellData<>(value.toString()); | ||
| } | ||
| } | ||
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
89 changes: 89 additions & 0 deletions
89
...t/src/test/java/org/apache/fesod/sheet/converters/instant/InstantStringConverterTest.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,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)); | ||
| } | ||
| } |
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.
The current implementation of
InstantStringConverterhas a semantic inconsistency between read and write behavior.convertToJavaData(...): usesOffsetDateTime.parse(...).toInstant(), which accepts any offset (e.g.,+08:00) and normalizes the value toUTC.convertToExcelData(): always outputsInstant.toString(), which is strictlyUTC.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
UTCtimestamp, the converter should enforce that the input string is already inUTC. Otherwise the converter silently changes the timestamp.I suggest adding a validation step in convertToJavaData to reject
non‑UTCoffsets. This keeps the read/write semantics consistent and avoids unexpected timezone normalization.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.
Updated. The read path now parses the offset explicitly and rejects any non-zero offset with
DateTimeParseException;Zand+00:00remain valid UTC inputs, and writes stay canonicalZ. I added a regression test for rejecting+08:00. The focused tests, Spotless, and the full package run all pass (924 tests) on commitf0af0eb.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.
Suggest using
getOffset().equals(ZoneOffset.UTC)ensures that the input is trulyUTC(Z or +00:00) and improves readability.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.
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 commit4a7c6bb.