Skip to content

Commit

Permalink
Fixed Strings without local time
Browse files Browse the repository at this point in the history
  • Loading branch information
kpartlow committed Jan 29, 2024
1 parent 24450d6 commit 73525d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/main/java/com/cedarsoftware/util/DateUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public static TemporalAccessor parseDate(String dateStr, ZoneId defaultZoneId, b
dateStr = dateStr.trim();

if (allDigits.matcher(dateStr).matches()) {
return Instant.ofEpochMilli(Long.parseLong(dateStr)).atZone(ZoneId.of("UTC"));
return Instant.ofEpochMilli(Long.parseLong(dateStr)).atZone(defaultZoneId);
}

String year, day, remains, tz = null;
Expand Down Expand Up @@ -274,7 +274,7 @@ public static TemporalAccessor parseDate(String dateStr, ZoneId defaultZoneId, b
verifyNoGarbageLeft(remnant);
}

ZoneId zoneId = StringUtilities.isEmpty(tz) ? null : getTimeZone(tz);
ZoneId zoneId = StringUtilities.isEmpty(tz) ? defaultZoneId : getTimeZone(tz);
TemporalAccessor dateTime = getDate(dateStr, zoneId, year, month, day, hour, min, sec, fracSec);
return dateTime;
}
Expand All @@ -299,8 +299,12 @@ private static TemporalAccessor getDate(String dateStr,
throw new IllegalArgumentException("Day must be between 1 and 31 inclusive, date: " + dateStr);
}

if (hour == null) { // no [valid] time portion
return LocalDateTime.of(y, month, d, 0, 0, 0);
if (hour == null) { // no [valid] time portion
if (zoneId == null) {
return LocalDateTime.of(y, month, d, 0, 0, 0);
} else {
return ZonedDateTime.of(y, month, d, 0, 0, 0, 0, zoneId);
}
} else {
// Regex prevents these from ever failing to parse.
int h = Integer.parseInt(hour);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ private static Instant getInstant(String from, ConverterOptions options) {
Instant instant;
if (dateTime instanceof LocalDateTime) {
LocalDateTime localDateTime = LocalDateTime.from(dateTime);
instant = localDateTime.atZone(options.getZoneId()).toInstant();
instant = localDateTime.atZone(options.getSourceZoneIdForLocalDates()).toInstant();
} else {
instant = Instant.from(dateTime);
}
Expand Down
30 changes: 28 additions & 2 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,18 @@ void testToBoolean_falseCases(Object input) {
}


private static Stream<Arguments> dateStringNoZoneOffset() {
return Stream.of(
Arguments.of("2000-01-01T13:59:59", TOKYO),
Arguments.of("2000-01-01T05:59:59", PARIS),
Arguments.of("2000-01-01T04:59:59", GMT),
Arguments.of("1999-12-31T23:59:59", NEW_YORK),
Arguments.of("1999-12-31T22:59:59", CHICAGO),
Arguments.of("1999-12-31T20:59:59", LOS_ANGELES)
);
}


private static Stream<Arguments> dateStringInIsoOffsetDateTime() {
return Stream.of(
Arguments.of("2000-01-01T13:59:59+09:00"),
Expand Down Expand Up @@ -726,6 +738,22 @@ private static Stream<Arguments> dateStringInIsoZoneDateTime() {
}


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

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


@ParameterizedTest
@MethodSource("dateStringInIsoOffsetDateTime")
void testStringDateWithTimeZoneToLocalDateTime(String date) {
Expand Down Expand Up @@ -800,8 +828,6 @@ 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 73525d4

Please sign in to comment.