Skip to content

Commit

Permalink
🐛 fix for issue #274
Browse files Browse the repository at this point in the history
  • Loading branch information
kcpeppe committed Feb 28, 2023
1 parent cdc3dfd commit 129f06c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 133 deletions.
14 changes: 2 additions & 12 deletions api/src/main/java/com/microsoft/gctoolkit/io/GCLogFileSegment.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ public String getSegmentName() {
public double getStartTime() {
try {
ageOfJVMAtLogStart();
if ( startTime.hasTimeStamp())
return startTime.getTimeStamp();
else if ( startTime.hasDateStamp())
return startTime.toEpochInMillis();
else
return Double.MAX_VALUE;
return startTime.getTimeStamp();
} catch (NullPointerException ex) {
return Double.MAX_VALUE;
}
Expand All @@ -96,12 +91,7 @@ else if ( startTime.hasDateStamp())
public double getEndTime() {
try {
ageOfJVMAtLogEnd();
if ( endTime.hasTimeStamp())
return endTime.getTimeStamp();
else if ( endTime.hasDateStamp())
return endTime.toEpochInMillis();
else
return Double.MAX_VALUE;
return endTime.getTimeStamp();
} catch (NullPointerException|IOException ex) {
return Double.MIN_VALUE;
}
Expand Down
79 changes: 19 additions & 60 deletions api/src/main/java/com/microsoft/gctoolkit/time/DateTimeStamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ public class DateTimeStamp implements Comparable<DateTimeStamp> {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

private static ZonedDateTime dateFromString(String iso8601DateTime) {
if (iso8601DateTime != null) {
TemporalAccessor temporalAccessor = formatter.parse(iso8601DateTime);
return ZonedDateTime.from(temporalAccessor);
}
return null;
if ( iso8601DateTime == null)
return null;
return ZonedDateTime.from(formatter.parse(iso8601DateTime));
}

private static double ageFromString(String doubleFormat) {
Expand Down Expand Up @@ -151,6 +149,8 @@ public DateTimeStamp(ZonedDateTime dateTime, double timeStamp) {
* @return The time stamp value, in decimal seconds.
*/
public double getTimeStamp() {
if (! hasTimeStamp())
return toEpochInMillis();
return timeStamp;
}

Expand Down Expand Up @@ -179,14 +179,11 @@ public boolean hasTimeStamp() {
public boolean equals(Object obj) {
if (obj instanceof DateTimeStamp) {
DateTimeStamp other = (DateTimeStamp) obj;
boolean hasTimeStamp = hasTimeStamp() && other.hasTimeStamp();
boolean hasDateStamp = hasDateStamp() && other.hasDateStamp();
if (hasDateStamp && hasTimeStamp)
return equals(other.getTimeStamp()) && getDateTime().equals(other.getDateTime());
if (hasTimeStamp)
return equals(other.getTimeStamp());
if (hasDateStamp)
return getDateTime().equals(other.getDateTime());
if (this.hasDateStamp())
return this.getDateTime().equals(other.getDateTime()) &&
(this.getTimeStamp() == other.getTimeStamp());
else
return getTimeStamp() == other.getTimeStamp();
}
return false;
}
Expand All @@ -202,47 +199,17 @@ public String toString() {
if (hasDateStamp())
buffer.append(getDateTime().toString());
if (hasTimeStamp())
buffer.append("@").append(String.format(Locale.US,"%.3f",timeStamp));
buffer.append("@").append(String.format(Locale.US,"%.3f",getTimeStamp()));
return buffer.toString();
}

/**
* Return {@code true} if this time stamp is less than the other.
* @param other The other time stamp, in decimal seconds.
* @return {@code true} if this time stamp is less than the other.
*/
public boolean before(double other) {
if ( other < 0.000d || Double.isNaN(other)) return false;
if ( Double.isNaN(getTimeStamp())) return false;
return getTimeStamp() < other;
}

/**
* Return {@code true} if this time stamp is greater than the other.
* @param other The other time stamp, in decimal seconds.
* @return {@code true} if this time stamp is greater than the other.
*/
public boolean after(double other) {
if ( other < 0.000d || Double.isNaN(other)) return false;
if ( Double.isNaN(getTimeStamp())) return false;
return getTimeStamp() > other;
}

public boolean equals(double other) {
if ( other < 0.000d || Double.isNaN(other)) return false;
if ( Double.isNaN(getTimeStamp())) return false;
return getTimeStamp() == other;
}

/**
* Return {@code true} if this DateTimeStamp comes before the other.
* @param other The other DateTimeStamp.
* @return {@code true} if this time stamp is less than the other.
*/
public boolean before(DateTimeStamp other) {
return ( hasTimeStamp() && other.hasTimeStamp())
? before(other.getTimeStamp())
: compare(other.getDateTime()) < 0;
return getTimeStamp() < other.getTimeStamp();
}

/**
Expand All @@ -251,9 +218,7 @@ public boolean before(DateTimeStamp other) {
* @return {@code true} if this time stamp is less than the other.
*/
public boolean after(DateTimeStamp other) {
return ( hasTimeStamp() && other.hasTimeStamp())
? after(other.getTimeStamp())
: compare(other.getDateTime()) > 0;
return getTimeStamp() > other.getTimeStamp();
}

/**
Expand Down Expand Up @@ -284,26 +249,20 @@ public DateTimeStamp add(double offsetInDecimalSeconds) {
if (Double.isNaN(offsetInDecimalSeconds))
throw new IllegalArgumentException("Cannot add " + Double.NaN);

DateTimeStamp now;
double adjustedTimeStamp = Double.NaN;
if ( ! Double.isNaN(getTimeStamp())) {
ZonedDateTime adjustedDateStamp = null;
if ( hasTimeStamp()) {
adjustedTimeStamp = getTimeStamp() + offsetInDecimalSeconds;
}

// Need to scale the values from ZonedDateTime
if (getDateTime() != null) {
if (hasDateStamp()) {
double offset = (Double.isNaN(offsetInDecimalSeconds)) ? 0.000d : offsetInDecimalSeconds;
int seconds = (int) offset;
long nanos = ((long) ((offset % 1) * 1_000L)) * 1_000_000L;
ZonedDateTime adjustedDateStamp = dateTime.plusSeconds(seconds).plusNanos(nanos);
if ( Double.isNaN(getTimeStamp()))
now = new DateTimeStamp(adjustedDateStamp);
else
now = new DateTimeStamp(adjustedDateStamp, adjustedTimeStamp);
} else
now = new DateTimeStamp(adjustedTimeStamp);
adjustedDateStamp = dateTime.plusSeconds(seconds).plusNanos(nanos);
}

return now;
return new DateTimeStamp(adjustedDateStamp,adjustedTimeStamp);
}

/**
Expand Down
Loading

0 comments on commit 129f06c

Please sign in to comment.