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 @@ -75,6 +75,7 @@
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.offsettime.OffsetTimeStringConverter;
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 @@ -143,6 +144,7 @@ private static void initAllConverter() {
putAllConverter(new TimestampStringConverter());
putAllConverter(new OffsetDateTimeNumberConverter());
putAllConverter(new OffsetDateTimeStringConverter());
putAllConverter(new OffsetTimeStringConverter());

putAllConverter(new InstantStringConverter());

Expand Down Expand Up @@ -192,6 +194,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new SqlTimeDateConverter());
putWriteConverter(new TimestampDateConverter());
putWriteConverter(new OffsetDateTimeDateConverter());
putWriteConverter(new OffsetTimeStringConverter());
putWriteConverter(new DoubleNumberConverter());
putWriteConverter(new FloatNumberConverter());
putWriteConverter(new IntegerNumberConverter());
Expand Down Expand Up @@ -220,6 +223,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new SqlTimeStringConverter());
putWriteStringConverter(new TimestampStringConverter());
putWriteStringConverter(new OffsetDateTimeStringConverter());
putWriteStringConverter(new OffsetTimeStringConverter());
putWriteStringConverter(new DoubleStringConverter());
putWriteStringConverter(new FloatStringConverter());
putWriteStringConverter(new IntegerStringConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.offsettime;

import java.time.OffsetTime;
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;

/** OffsetTime and string converter. */
public class OffsetTimeStringConverter implements Converter<OffsetTime> {
@Override
public Class<?> supportJavaTypeKey() {
return OffsetTime.class;
}

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

@Override
public OffsetTime convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return DateUtils.parseOffsetTime(
cellData.getStringValue(), format(contentProperty), globalConfiguration.getLocale());
}

@Override
public WriteCellData<?> convertToExcelData(
OffsetTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(DateUtils.format(value, format(contentProperty), globalConfiguration.getLocale()));
}

private String format(ExcelContentProperty contentProperty) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return null;
}
return contentProperty.getDateTimeFormatProperty().getFormat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
Expand Down Expand Up @@ -185,6 +186,21 @@ public static OffsetDateTime parseOffsetDateTime(String dateString, String dateF
return OffsetDateTime.parse(dateString, getCacheDateTimeFormat(dateFormat, local));
}

/**
* convert string to time, requiring the string to carry an explicit offset
*
* @param timeString time string, e.g. 03:04:05+08:00
* @param timeFormat time format, empty means ISO_OFFSET_TIME
* @param local local
* @return
*/
public static OffsetTime parseOffsetTime(String timeString, String timeFormat, Locale local) {
if (StringUtils.isEmpty(timeFormat)) {
return OffsetTime.parse(timeString, DateTimeFormatter.ISO_OFFSET_TIME);
}
return OffsetTime.parse(timeString, getCacheDateTimeFormat(timeFormat, local));
}

/**
* convert string to date
*
Expand Down Expand Up @@ -404,6 +420,24 @@ public static String format(OffsetDateTime date, String dateFormat, Locale local
return date.format(getCacheDateTimeFormat(dateFormat, local));
}

/**
* Format time, preserving the offset in the output
*
* @param time time
* @param timeFormat time format, empty means ISO_OFFSET_TIME
* @param local local
* @return format string
*/
public static String format(OffsetTime time, String timeFormat, Locale local) {
if (time == null) {
return null;
}
if (StringUtils.isEmpty(timeFormat)) {
return time.format(DateTimeFormatter.ISO_OFFSET_TIME);
}
return time.format(getCacheDateTimeFormat(timeFormat, local));
}

/**
* Format date
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.util.Map;
import java.util.UUID;
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;
import org.apache.fesod.sheet.converters.offsettime.OffsetTimeStringConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeDateConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeNumberConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeStringConverter;
Expand Down Expand Up @@ -143,6 +145,21 @@ void loadConvertersRegistersTimestampFamily() {
writeConverter.get(ConverterKeyBuild.buildKey(Timestamp.class, CellDataTypeEnum.STRING)));
}

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

Map<ConverterKey, Converter<?>> writeConverter = DefaultConverterLoader.loadDefaultWriteConverter();
Assertions.assertInstanceOf(
OffsetTimeStringConverter.class, writeConverter.get(ConverterKeyBuild.buildKey(OffsetTime.class)));
Assertions.assertInstanceOf(
OffsetTimeStringConverter.class,
writeConverter.get(ConverterKeyBuild.buildKey(OffsetTime.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,49 @@
/*
* 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.offsettime;

import java.time.OffsetTime;
import org.apache.fesod.sheet.annotation.ExcelProperty;
import org.apache.fesod.sheet.annotation.format.DateTimeFormat;

public class OffsetTimeRoundTripData {
@ExcelProperty("iso")
private OffsetTime iso;

@ExcelProperty("custom")
@DateTimeFormat("HH:mm:ss Z")
private OffsetTime custom;

public OffsetTime getIso() {
return iso;
}

public void setIso(OffsetTime iso) {
this.iso = iso;
}

public OffsetTime getCustom() {
return custom;
}

public void setCustom(OffsetTime custom) {
this.custom = custom;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.offsettime;

import java.io.File;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.List;
import org.apache.fesod.sheet.testkit.Tags;
import org.apache.fesod.sheet.testkit.base.AbstractExcelTest;
import org.apache.fesod.sheet.testkit.enums.ExcelFormat;
import org.apache.fesod.sheet.testkit.helpers.RoundTripHelper;
import org.apache.fesod.sheet.testkit.params.ExcelFormatSource;
import org.apache.fesod.sheet.testkit.params.FormatScope;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;

@Tag(Tags.ROUND_TRIP)
class OffsetTimeRoundTripTest extends AbstractExcelTest {
@ParameterizedTest
@ExcelFormatSource(FormatScope.ALL)
void roundTrip(ExcelFormat format) throws Exception {
File file = createTempFile(format);
OffsetTimeRoundTripData input = new OffsetTimeRoundTripData();
input.setIso(OffsetTime.of(9, 30, 45, 0, ZoneOffset.ofHours(8)));
input.setCustom(OffsetTime.of(18, 5, 1, 0, ZoneOffset.ofHours(-5)));
List<OffsetTimeRoundTripData> result =
RoundTripHelper.writeAndRead(file, OffsetTimeRoundTripData.class, Collections.singletonList(input));

Assertions.assertEquals(1, result.size());
Assertions.assertEquals(input.getIso(), result.get(0).getIso());
Assertions.assertEquals(input.getCustom(), result.get(0).getCustom());
}
}
Loading
Loading