Skip to content

Commit

Permalink
DateTime Zone corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Jan 29, 2024
1 parent 243e0dc commit 24450d6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static LocalDate toLocalDate(Object from, Converter converter, ConverterOptions
return null;
}
// Bring the LocalDate to a user-specifiable timezone
return instant.atZone(options.getSourceZoneIdForLocalDates()).toLocalDate();
return instant.atZone(options.getZoneId()).toLocalDate();
}

static LocalDateTime toLocalDateTime(Object from, Converter converter, ConverterOptions options) {
Expand All @@ -325,7 +325,7 @@ static LocalDateTime toLocalDateTime(Object from, Converter converter, Converter
return null;
}
// Bring the LocalDateTime to a user-specifiable timezone
return instant.atZone(options.getSourceZoneIdForLocalDates()).toLocalDateTime();
return instant.atZone(options.getZoneId()).toLocalDateTime();
}

static LocalTime toLocalTime(Object from, Converter converter, ConverterOptions options) {
Expand Down Expand Up @@ -362,7 +362,7 @@ private static Instant getInstant(String from, ConverterOptions options) {
if (str == null) {
return null;
}
TemporalAccessor dateTime = DateUtilities.parseDate(str, options.getZoneId(), true);
TemporalAccessor dateTime = DateUtilities.parseDate(str, options.getSourceZoneIdForLocalDates(), true);

Instant instant;
if (dateTime instanceof LocalDateTime) {
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/com/cedarsoftware/util/convert/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,90 @@ void testToBoolean_falseCases(Object input) {
}


private static Stream<Arguments> dateStringInIsoOffsetDateTime() {
return Stream.of(
Arguments.of("2000-01-01T13:59:59+09:00"),
Arguments.of("2000-01-01T05:59:59+01:00"),
Arguments.of("2000-01-01T04:59:59Z"),
Arguments.of("1999-12-31T23:59:59-05:00"),
Arguments.of("1999-12-31T22:59:59-06:00"),
Arguments.of("1999-12-31T20:59:59-08:00")
);
}

private static Stream<Arguments> dateStringInIsoOffsetDateTimeWithMillis() {
return Stream.of(
Arguments.of("2000-01-01T13:59:59.959+09:00"),
Arguments.of("2000-01-01T05:59:59.959+01:00"),
Arguments.of("2000-01-01T04:59:59.959Z"),
Arguments.of("1999-12-31T23:59:59.959-05:00"),
Arguments.of("1999-12-31T22:59:59.959-06:00"),
Arguments.of("1999-12-31T20:59:59.959-08:00")
);
}

private static Stream<Arguments> dateStringInIsoZoneDateTime() {
return Stream.of(
Arguments.of("2000-01-01T13:59:59.959+09:00[Asia/Tokyo]"),
Arguments.of("2000-01-01T05:59:59.959+01:00[Europe/Paris]"),
Arguments.of("2000-01-01T04:59:59.959Z[GMT]"),
Arguments.of("1999-12-31T23:59:59.959-05:00[America/New_York]"),
Arguments.of("1999-12-31T22:59:59.959-06:00[America/Chicago]"),
Arguments.of("1999-12-31T20:59:59.959-08:00[America/Los_Angeles]")
);
}


@ParameterizedTest
@MethodSource("dateStringInIsoOffsetDateTime")
void testStringDateWithTimeZoneToLocalDateTime(String date) {
// source is TOKYO, bu should be ignored when zone is provided on string.
LocalDateTime localDateTime = this.converter.convert(date, LocalDateTime.class, createCustomZones(TOKYO, NEW_YORK));

assertThat(localDateTime)
.hasYear(1999)
.hasMonthValue(12)
.hasDayOfMonth(31)
.hasHour(23)
.hasMinute(59)
.hasSecond(59);
}

/*
@ParameterizedTest
@MethodSource("dateStringInIsoOffsetDateTimeWithMillis")
void testStringDateWithTimeZoneToLocalDateTimeIncludeMillis(String date) {
// source is TOKYO, bu should be ignored when zone is provided on string.
LocalDateTime localDateTime = this.converter.convert(date, LocalDateTime.class, createCustomZones(TOKYO, NEW_YORK));
assertThat(localDateTime)
.hasYear(1999)
.hasMonthValue(12)
.hasDayOfMonth(31)
.hasHour(23)
.hasMinute(59)
.hasSecond(59)
.hasNano(959);
}
@ParameterizedTest
@MethodSource("dateStringInIsoZoneDateTime")
void testStringDateWithTimeZoneToLocalDateTimeWithZone(String date) {
// source is TOKYO, bu should be ignored when zone is provided on string.
LocalDateTime localDateTime = this.converter.convert(date, LocalDateTime.class, createCustomZones(TOKYO, NEW_YORK));
assertThat(localDateTime)
.hasYear(1999)
.hasMonthValue(12)
.hasDayOfMonth(31)
.hasHour(23)
.hasMinute(59)
.hasSecond(59)
.hasNano(959);
}
*/

private static Stream<Arguments> epochMillis_withLocalDateTimeInformation() {
return Stream.of(
Arguments.of(1687622249729L, TOKYO, LDT_2023_TOKYO),
Expand All @@ -716,6 +800,8 @@ void testCalendarToLocalDateTime(long epochMilli, ZoneId zoneId, LocalDateTime e
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(epochMilli);

System.out.println(Instant.ofEpochMilli(epochMilli).atZone(zoneId).toString());

LocalDateTime localDateTime = this.converter.convert(calendar, LocalDateTime.class, createCustomZones(zoneId, zoneId));

assertThat(localDateTime).isEqualTo(expected);
Expand Down

0 comments on commit 24450d6

Please sign in to comment.