Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Support mills timestamp format (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Feb 11, 2020
1 parent 4bc3715 commit 48c23d1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationConte
try {
return json == null ? null : DateUtils.getDateTime(json.getAsString());
} catch (Exception e) {
logger.log(SentryLevel.ERROR, "Error when deserializing Date", e);
logger.log(
SentryLevel.DEBUG,
"Error when deserializing UTC timestamp format, it might be mills timestamp format.",
e);
}
try {
return DateUtils.getDateTimeWithMillsPrecision(json.getAsString());
} catch (Exception e) {
logger.log(SentryLevel.ERROR, "Error when deserializing mills timestamp format.", e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ class AndroidSerializerTest {
assertEquals(expected, actual.timestamp)
}

@Test
fun `when deserializing mills timestamp, it should become a SentryEvent-Date`() {
val dateIsoFormat = "1581410911"
val expected = DateUtils.getDateTimeWithMillsPrecision(dateIsoFormat)

val jsonEvent = "{\"timestamp\":\"$dateIsoFormat\"}"

val actual = serializer.deserializeEvent(StringReader(jsonEvent))

assertEquals(expected, actual.timestamp)
}

@Test
fun `when deserializing mills timestamp with mills precision, it should become a SentryEvent-Date`() {
val dateIsoFormat = "1581410911.988"
val expected = DateUtils.getDateTimeWithMillsPrecision(dateIsoFormat)

val jsonEvent = "{\"timestamp\":\"$dateIsoFormat\"}"

val actual = serializer.deserializeEvent(StringReader(jsonEvent))

assertEquals(expected, actual.timestamp)
}

@Test
fun `when deserializing unknown properties, it should be added to unknown field`() {
val sentryEvent = generateEmptySentryEvent()
Expand Down
23 changes: 21 additions & 2 deletions sentry-core/src/main/java/io/sentry/core/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public static Date getCurrentDateTime() {
}

/**
* Get date
* Get Java Date from UTC timestamp format
*
* @param timestamp already UTC format
* @param timestamp UTC format eg 2000-12-31T23:59:58Z
* @return the Date
*/
public static Date getDateTime(String timestamp) throws IllegalArgumentException {
Expand All @@ -54,6 +54,25 @@ public static Date getDateTime(String timestamp) throws IllegalArgumentException
}
}

/**
* Get Java Date from mills timestamp format
*
* @param timestamp mills format eg 1581410911.988 (1581410911 seconds and 988 mills)
* @return the Date
*/
public static Date getDateTimeWithMillsPrecision(String timestamp)
throws IllegalArgumentException {
try {
String[] times = timestamp.split("\\.", -1);
long seconds = Long.parseLong(times[0]);
long mills = times.length > 1 ? Long.parseLong(times[1]) : 0;

return new Date((seconds * 1000) + mills);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("timestamp is not mills format " + timestamp);
}
}

/**
* Get date formatted as expected by Sentry.
*
Expand Down

0 comments on commit 48c23d1

Please sign in to comment.