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

Support mills timestamp format #263

Merged
merged 2 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.ERROR,
marandaneto marked this conversation as resolved.
Show resolved Hide resolved
"Error when deserializing UTC timestamp format, 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