// 
// Decompiled by Procyon v0.6.0
// 

package io.sentry;

import org.jetbrains.annotations.Nullable;
import java.math.RoundingMode;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.ParsePosition;
import org.jetbrains.annotations.NotNull;
import java.util.Calendar;
import io.sentry.vendor.gson.internal.bind.util.ISO8601Utils;
import java.util.Date;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class DateUtils
{
    private DateUtils() {
    }
    
    @NotNull
    public static Date getCurrentDateTime() {
        final Calendar calendar = Calendar.getInstance(ISO8601Utils.TIMEZONE_UTC);
        return calendar.getTime();
    }
    
    @NotNull
    public static Date getDateTime(@NotNull final String timestamp) throws IllegalArgumentException {
        try {
            return ISO8601Utils.parse(timestamp, new ParsePosition(0));
        }
        catch (final ParseException e) {
            throw new IllegalArgumentException("timestamp is not ISO format " + timestamp);
        }
    }
    
    @NotNull
    public static Date getDateTimeWithMillisPrecision(@NotNull final String timestamp) throws IllegalArgumentException {
        try {
            return getDateTime(new BigDecimal(timestamp).setScale(3, RoundingMode.DOWN).movePointRight(3).longValue());
        }
        catch (final NumberFormatException e) {
            throw new IllegalArgumentException("timestamp is not millis format " + timestamp);
        }
    }
    
    @NotNull
    public static String getTimestamp(@NotNull final Date date) {
        return ISO8601Utils.format(date, true);
    }
    
    @NotNull
    public static Date getDateTime(final long millis) {
        final Calendar calendar = Calendar.getInstance(ISO8601Utils.TIMEZONE_UTC);
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    }
    
    public static double millisToSeconds(final double millis) {
        return millis / 1000.0;
    }
    
    public static long millisToNanos(final long millis) {
        return millis * 1000000L;
    }
    
    public static double nanosToMillis(final double nanos) {
        return nanos / 1000000.0;
    }
    
    public static Date nanosToDate(final long nanos) {
        final Double millis = nanosToMillis((double)nanos);
        return getDateTime(millis.longValue());
    }
    
    @Nullable
    public static Date toUtilDate(@Nullable final SentryDate sentryDate) {
        if (sentryDate == null) {
            return null;
        }
        return toUtilDateNotNull(sentryDate);
    }
    
    @NotNull
    public static Date toUtilDateNotNull(@NotNull final SentryDate sentryDate) {
        return nanosToDate(sentryDate.nanoTimestamp());
    }
    
    public static double nanosToSeconds(final long nanos) {
        return nanos / 1.0E9;
    }
    
    public static double dateToSeconds(@NotNull final Date date) {
        return millisToSeconds((double)date.getTime());
    }
    
    public static long dateToNanos(@NotNull final Date date) {
        return millisToNanos(date.getTime());
    }
    
    public static long secondsToNanos(@NotNull final long seconds) {
        return seconds * 1000000000L;
    }
    
    @NotNull
    public static BigDecimal doubleToBigDecimal(@NotNull final Double value) {
        return BigDecimal.valueOf(value).setScale(6, RoundingMode.DOWN);
    }
}
