Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify DateUtils with ISO8601Utils #1837

Merged
merged 10 commits into from Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions sentry/api/sentry.api
Expand Up @@ -2652,6 +2652,14 @@ public class io/sentry/vendor/Base64 {
public static fun encodeToString ([BIII)Ljava/lang/String;
}

public class io/sentry/vendor/gson/internal/bind/util/ISO8601Utils {
public fun <init> ()V
public static fun format (Ljava/util/Date;)Ljava/lang/String;
public static fun format (Ljava/util/Date;Z)Ljava/lang/String;
public static fun format (Ljava/util/Date;ZLjava/util/TimeZone;)Ljava/lang/String;
public static fun parse (Ljava/lang/String;Ljava/text/ParsePosition;)Ljava/util/Date;
}

public class io/sentry/vendor/gson/stream/JsonReader : java/io/Closeable {
public fun <init> (Ljava/io/Reader;)V
public fun beginArray ()V
Expand Down
40 changes: 4 additions & 36 deletions sentry/src/main/java/io/sentry/DateUtils.java
@@ -1,13 +1,12 @@
package io.sentry;

import io.sentry.vendor.gson.internal.bind.util.ISO8601Utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.ParsePosition;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand All @@ -16,34 +15,10 @@
@ApiStatus.Internal
public final class DateUtils {
private static final String UTC = "UTC";
// ISO 8601
private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static final String ISO_FORMAT_WITH_MILLIS = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

// if UTC is not found, it fallback to "GMT" which is UTC equivalent
private static final @NotNull TimeZone UTC_TIMEZONE = TimeZone.getTimeZone(UTC);

private static final @NotNull ThreadLocal<SimpleDateFormat> SDF_ISO_FORMAT_WITH_MILLIS_UTC =
new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
final SimpleDateFormat simpleDateFormat =
new SimpleDateFormat(ISO_FORMAT_WITH_MILLIS, Locale.ROOT);
simpleDateFormat.setTimeZone(UTC_TIMEZONE);
return simpleDateFormat;
}
};

private static final @NotNull ThreadLocal<SimpleDateFormat> SDF_ISO_FORMAT_UTC =
new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ISO_FORMAT, Locale.ROOT);
simpleDateFormat.setTimeZone(UTC_TIMEZONE);
return simpleDateFormat;
}
};

private DateUtils() {}

/**
Expand All @@ -66,14 +41,8 @@ private DateUtils() {}
public static @NotNull Date getDateTime(final @NotNull String timestamp)
throws IllegalArgumentException {
try {
return SDF_ISO_FORMAT_WITH_MILLIS_UTC.get().parse(timestamp);
return ISO8601Utils.parse(timestamp, new ParsePosition(0));
} catch (ParseException e) {
try {
// to keep compatibility with older envelopes
return SDF_ISO_FORMAT_UTC.get().parse(timestamp);
} catch (ParseException ignored) {
// invalid timestamp format
}
throw new IllegalArgumentException("timestamp is not ISO format " + timestamp);
}
}
Expand Down Expand Up @@ -102,8 +71,7 @@ private DateUtils() {}
* @return the UTC/ISO 8601 timestamp
*/
public static @NotNull String getTimestamp(final @NotNull Date date) {
final DateFormat df = SDF_ISO_FORMAT_WITH_MILLIS_UTC.get();
return df.format(date);
return ISO8601Utils.format(date, true);
}

/**
Expand Down