Skip to content

Commit

Permalink
Simplify DateUtils with ISO8601Utils (#1837)
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Jan 18, 2022
1 parent 0f6e95e commit 49003d3
Show file tree
Hide file tree
Showing 7 changed files with 487 additions and 53 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Ref: Simplify DateUtils with ISO8601Utils (#1837)

## 6.0.0-alpha.1

* Feat: Use float instead of Date for protocol types for higher precision (#1737)
Expand Down
4 changes: 0 additions & 4 deletions gradle.properties
Expand Up @@ -5,11 +5,7 @@ org.gradle.jvmargs=-Xmx4g -XX:MaxPermSize=512m -XX:MaxMetaspaceSize=1536m -XX:+H
android.useAndroidX=true

# Release information
<<<<<<< HEAD
versionName=6.0.0-alpha.2-SNAPSHOT
=======
versionName=5.5.4-SNAPSHOT
>>>>>>> main

# disable renderscript, it's enabled by default
android.defaults.buildfeatures.renderscript=false
Expand Down
9 changes: 9 additions & 0 deletions sentry/api/sentry.api
Expand Up @@ -2709,6 +2709,15 @@ 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 static final field TIMEZONE_UTC Ljava/util/TimeZone;
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
51 changes: 8 additions & 43 deletions sentry/src/main/java/io/sentry/DateUtils.java
@@ -1,48 +1,20 @@
package io.sentry;

import static io.sentry.vendor.gson.internal.bind.util.ISO8601Utils.TIMEZONE_UTC;

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;

/** Utilities to deal with dates */
@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 @@ -53,7 +25,7 @@ private DateUtils() {}
*/
@SuppressWarnings("JdkObsolete")
public static @NotNull Date getCurrentDateTime() {
final Calendar calendar = Calendar.getInstance(UTC_TIMEZONE);
final Calendar calendar = Calendar.getInstance(TIMEZONE_UTC);
return calendar.getTime();
}

Expand All @@ -66,14 +38,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 +68,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 All @@ -113,7 +78,7 @@ private DateUtils() {}
* @return the UTC Date
*/
public static @NotNull Date getDateTime(final long millis) {
final Calendar calendar = Calendar.getInstance(UTC_TIMEZONE);
final Calendar calendar = Calendar.getInstance(TIMEZONE_UTC);
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
Expand Down

0 comments on commit 49003d3

Please sign in to comment.