Skip to content

Commit

Permalink
fixed bug - fractional seconds must be converted, correctly, to nanos…
Browse files Browse the repository at this point in the history
… of second
  • Loading branch information
jdereg committed Jan 23, 2024
1 parent 3c0c732 commit 278099d
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/main/java/com/cedarsoftware/util/DateUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static ZonedDateTime parseDate(String dateStr, ZoneId defaultZoneId, bool
}

// For the remaining String, match the time portion (which could have appeared ahead of the date portion)
String hour = null, min = null, sec = "00", milli = "0";
String hour = null, min = null, sec = "00", fracSec = "0";
remains = remains.trim();
matcher = timePattern.matcher(remains);
remnant = matcher.replaceFirst("");
Expand All @@ -254,7 +254,7 @@ public static ZonedDateTime parseDate(String dateStr, ZoneId defaultZoneId, bool
sec = matcher.group(3);
}
if (matcher.group(4) != null) {
milli = matcher.group(4).substring(1);
fracSec = "0." + matcher.group(4).substring(1);
}
if (matcher.group(5) != null) {
tz = stripBrackets(matcher.group(5).trim());
Expand All @@ -266,7 +266,7 @@ public static ZonedDateTime parseDate(String dateStr, ZoneId defaultZoneId, bool
}

ZoneId zoneId = StringUtilities.isEmpty(tz) ? defaultZoneId : getTimeZone(tz);
ZonedDateTime zonedDateTime = getDate(dateStr, zoneId, year, month, day, hour, min, sec, milli);
ZonedDateTime zonedDateTime = getDate(dateStr, zoneId, year, month, day, hour, min, sec, fracSec);
return zonedDateTime;
}

Expand All @@ -278,7 +278,7 @@ private static ZonedDateTime getDate(String dateStr,
String hour,
String min,
String sec,
String nanos) {
String fracSec) {
// Build Calendar from date, time, and timezone components, and retrieve Date instance from Calendar.
int y = Integer.parseInt(year);
int d = Integer.parseInt(day);
Expand All @@ -297,7 +297,7 @@ private static ZonedDateTime getDate(String dateStr,
int h = Integer.parseInt(hour);
int mn = Integer.parseInt(min);
int s = Integer.parseInt(sec);
int ns = Integer.parseInt(nanos);
long nanoOfSec = convertFractionToNanos(fracSec);

if (h > 23) {
throw new IllegalArgumentException("Hour must be between 0 and 23 inclusive, time: " + dateStr);
Expand All @@ -309,11 +309,16 @@ private static ZonedDateTime getDate(String dateStr,
throw new IllegalArgumentException("Second must be between 0 and 59 inclusive, time: " + dateStr);
}

ZonedDateTime zdt = ZonedDateTime.of(y, month, d, h, mn, s, ns, zoneId);
ZonedDateTime zdt = ZonedDateTime.of(y, month, d, h, mn, s, (int) nanoOfSec, zoneId);
return zdt;
}
}

private static long convertFractionToNanos(String fracSec) {
double fractionalSecond = Double.parseDouble(fracSec);
return (long) (fractionalSecond * 1_000_000_000);
}

private static ZoneId getTimeZone(String tz) {
if (tz != null) {
if (tz.startsWith("-") || tz.startsWith("+")) {
Expand Down

0 comments on commit 278099d

Please sign in to comment.