Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Fix: Handling missing Spring Security on classpath on Java 8 (#1552)
* Feat: Support transaction waiting for children to finish. (#1535)
* Feat: Capture logged marker in log4j2 and logback appenders (#1551)
* Fix: Clock drift issue when calling DateUtils#getDateTimeWithMillisPrecision (#1557)
* Feat: Set mechanism type in SentryExceptionResolver (#1556)

## 5.1.0-beta.1
Expand Down
9 changes: 4 additions & 5 deletions sentry/src/main/java/io/sentry/DateUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.sentry;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -86,11 +88,8 @@ private DateUtils() {}
public static @NotNull Date getDateTimeWithMillisPrecision(final @NotNull String timestamp)
throws IllegalArgumentException {
try {
final String[] times = timestamp.split("\\.", -1);
final long seconds = Long.parseLong(times[0]);
final long millis = times.length > 1 ? Long.parseLong(times[1]) : 0;

return getDateTime((seconds * 1000) + millis);
return getDateTime(
new BigDecimal(timestamp).setScale(3, RoundingMode.DOWN).movePointRight(3).longValue());
} catch (NumberFormatException e) {
throw new IllegalArgumentException("timestamp is not millis format " + timestamp);
}
Expand Down
25 changes: 17 additions & 8 deletions sentry/src/test/java/io/sentry/DateUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,23 @@ class DateUtilsTest {

@Test
fun `Millis timestamp with millis precision, it should be UTC`() {
// Jun 7, 2020 12:38:12 PM UTC
val dateIsoFormat = "1591533492.631"
val actual = DateUtils.getDateTimeWithMillisPrecision(dateIsoFormat)

val utcActual = convertDate(actual)
val timestamp = utcActual.format(isoFormat)

assertEquals("2020-06-07T12:38:12.631Z", timestamp)
val input = listOf(
Pair("1591533492.631", "2020-06-07T12:38:12.631Z"),
Pair("1591533492.63", "2020-06-07T12:38:12.630Z"),
Pair("1591533492.6", "2020-06-07T12:38:12.600Z"),
Pair("1591533492", "2020-06-07T12:38:12.000Z"),
Pair("1591533492.631631", "2020-06-07T12:38:12.631Z"),
Pair("1591533492.999999", "2020-06-07T12:38:12.999Z")
)

input.forEach {
val actual = DateUtils.getDateTimeWithMillisPrecision(it.first)

val utcActual = convertDate(actual)
val timestamp = utcActual.format(isoFormat)

assertEquals(it.second, timestamp)
}
}

@Test
Expand Down