Skip to content

Commit

Permalink
Added ZoneId preference so ZoneDateTime comes back equivalent
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Feb 12, 2024
1 parent b8aec84 commit 5a3ff6a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/cedarsoftware/util/DateUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,10 @@ public static ZonedDateTime parseDate(String dateStr, ZoneId defaultZoneId, bool
tz = matcher.group(5).trim();
}
if (matcher.group(6) != null) {
if (StringUtilities.isEmpty(tz)) { // Only use timezone name when offset is not used
tz = stripBrackets(matcher.group(6).trim());
}
// to make round trip of ZonedDateTime equivalent we need to use the original Zone as ZoneId
// ZoneId is a much broader definition handling multiple possible dates, and we want this to
// be equivalent to the original zone that was used if one was present.
tz = stripBrackets(matcher.group(6).trim());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ static ZoneId toZoneId(Object from, Converter converter) {
ConverterOptions options = converter.getOptions();
ZoneId zoneId = converter.convert(map.get(ZONE), ZoneId.class);
return zoneId;
} else if (map.containsKey(ID)) {
ConverterOptions options = converter.getOptions();
ZoneId zoneId = converter.convert(map.get(ID), ZoneId.class);
return zoneId;
} else {
return fromSingleKey(from, converter, ZONE, ZoneId.class);
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/cedarsoftware/util/TestDateUtilities.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.cedarsoftware.util;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
Expand All @@ -17,6 +11,12 @@
import java.util.TimeZone;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.assertj.core.api.Assertions.assertThat;
import com.cedarsoftware.util.DeepEquals;

import static org.junit.jupiter.api.Assertions.assertTrue;

class ZonedDateTimeConversionsTests {

Expand Down Expand Up @@ -44,11 +47,13 @@ private static Stream<Arguments> roundTripZDT() {
void testZonedDateTime(ZonedDateTime zdt) {

String value = this.converter.convert(zdt, String.class);
System.out.println(value);
ZonedDateTime actual = this.converter.convert(value, ZonedDateTime.class);
System.out.println(actual);

assertThat(actual).isEqualTo(zdt);
}
assertTrue(DeepEquals.deepEquals(actual, zdt));

value = DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zdt);
actual = this.converter.convert(value, ZonedDateTime.class);

assertTrue(DeepEquals.deepEquals(actual, zdt));
}
}

0 comments on commit 5a3ff6a

Please sign in to comment.