package com.revenuecat.purchases.utils;

import com.revenuecat.purchases.common.Constants;
import j$.util.DesugarTimeZone;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/* JADX INFO: loaded from: classes3.dex */
public final class Iso8601Utils {
    private static final String GMT_ID = "GMT";
    private static final TimeZone TIMEZONE_Z = DesugarTimeZone.getTimeZone(GMT_ID);

    private static boolean checkOffset(String str, int i6, char c6) {
        return i6 < str.length() && str.charAt(i6) == c6;
    }

    public static String format(Date date) {
        GregorianCalendar gregorianCalendar = new GregorianCalendar(TIMEZONE_Z, Locale.US);
        gregorianCalendar.setTime(date);
        StringBuilder sb = new StringBuilder(24);
        padInt(sb, gregorianCalendar.get(1), 4);
        sb.append('-');
        padInt(sb, gregorianCalendar.get(2) + 1, 2);
        sb.append('-');
        padInt(sb, gregorianCalendar.get(5), 2);
        sb.append('T');
        padInt(sb, gregorianCalendar.get(11), 2);
        sb.append(':');
        padInt(sb, gregorianCalendar.get(12), 2);
        sb.append(':');
        padInt(sb, gregorianCalendar.get(13), 2);
        sb.append('.');
        padInt(sb, gregorianCalendar.get(14), 3);
        sb.append('Z');
        return sb.toString();
    }

    private static int indexOfNonDigit(String str, int i6) {
        while (i6 < str.length()) {
            char cCharAt = str.charAt(i6);
            if (cCharAt < '0' || cCharAt > '9') {
                return i6;
            }
            i6++;
        }
        return str.length();
    }

    private static void padInt(StringBuilder sb, int i6, int i7) {
        String string = Integer.toString(i6);
        for (int length = i7 - string.length(); length > 0; length--) {
            sb.append('0');
        }
        sb.append(string);
    }

    /* JADX WARN: Removed duplicated region for block: B:47:0x00d6 A[Catch: IllegalArgumentException -> 0x0045, IndexOutOfBoundsException -> 0x0048, TryCatch #2 {IllegalArgumentException -> 0x0045, IndexOutOfBoundsException -> 0x0048, blocks: (B:3:0x000a, B:6:0x0017, B:8:0x0023, B:9:0x0025, B:11:0x0034, B:13:0x003a, B:21:0x0051, B:23:0x0061, B:24:0x0063, B:26:0x006f, B:27:0x0072, B:29:0x0078, B:33:0x0082, B:38:0x0092, B:40:0x009a, B:45:0x00d0, B:47:0x00d6, B:49:0x00de, B:70:0x0160, B:54:0x00e9, B:55:0x00ff, B:56:0x0100, B:58:0x010c, B:61:0x0115, B:63:0x012f, B:66:0x013e, B:67:0x015b, B:69:0x015e, B:72:0x018f, B:73:0x0196), top: B:76:0x000a }] */
    /* JADX WARN: Removed duplicated region for block: B:72:0x018f A[Catch: IllegalArgumentException -> 0x0045, IndexOutOfBoundsException -> 0x0048, TryCatch #2 {IllegalArgumentException -> 0x0045, IndexOutOfBoundsException -> 0x0048, blocks: (B:3:0x000a, B:6:0x0017, B:8:0x0023, B:9:0x0025, B:11:0x0034, B:13:0x003a, B:21:0x0051, B:23:0x0061, B:24:0x0063, B:26:0x006f, B:27:0x0072, B:29:0x0078, B:33:0x0082, B:38:0x0092, B:40:0x009a, B:45:0x00d0, B:47:0x00d6, B:49:0x00de, B:70:0x0160, B:54:0x00e9, B:55:0x00ff, B:56:0x0100, B:58:0x010c, B:61:0x0115, B:63:0x012f, B:66:0x013e, B:67:0x015b, B:69:0x015e, B:72:0x018f, B:73:0x0196), top: B:76:0x000a }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static Date parse(String str) {
        int i6;
        int iPow;
        int i7;
        int i8;
        TimeZone timeZone;
        char cCharAt;
        try {
            int i9 = parseInt(str, 0, 4);
            int i10 = checkOffset(str, 4, '-') ? 5 : 4;
            int i11 = i10 + 2;
            int i12 = parseInt(str, i10, i11);
            if (checkOffset(str, i11, '-')) {
                i11 = i10 + 3;
            }
            int i13 = i11 + 2;
            int i14 = parseInt(str, i11, i13);
            boolean zCheckOffset = checkOffset(str, i13, 'T');
            if (!zCheckOffset && str.length() <= i13) {
                return new GregorianCalendar(i9, i12 - 1, i14).getTime();
            }
            if (zCheckOffset) {
                int i15 = i11 + 5;
                int i16 = parseInt(str, i11 + 3, i15);
                if (checkOffset(str, i15, ':')) {
                    i15 = i11 + 6;
                }
                int i17 = i15 + 2;
                int i18 = parseInt(str, i15, i17);
                if (checkOffset(str, i17, ':')) {
                    i17 = i15 + 3;
                }
                if (str.length() > i17 && (cCharAt = str.charAt(i17)) != 'Z' && cCharAt != '+' && cCharAt != '-') {
                    int i19 = i17 + 2;
                    i8 = parseInt(str, i17, i19);
                    if (i8 > 59 && i8 < 63) {
                        i8 = 59;
                    }
                    if (checkOffset(str, i19, '.')) {
                        int iIndexOfNonDigit = indexOfNonDigit(str, i17 + 4);
                        iPow = (int) (Math.pow(10.0d, 3 - (r8 - r9)) * ((double) parseInt(str, i17 + 3, Math.min(iIndexOfNonDigit, i17 + 6))));
                        i7 = i18;
                        i6 = i16;
                        i13 = iIndexOfNonDigit;
                    } else {
                        i6 = i16;
                        i13 = i19;
                        i7 = i18;
                        iPow = 0;
                    }
                    if (str.length() > i13) {
                        throw new IllegalArgumentException("No time zone indicator");
                    }
                    char cCharAt2 = str.charAt(i13);
                    if (cCharAt2 == 'Z') {
                        timeZone = TIMEZONE_Z;
                    } else {
                        if (cCharAt2 != '+' && cCharAt2 != '-') {
                            throw new IndexOutOfBoundsException("Invalid time zone indicator '" + cCharAt2 + "'");
                        }
                        String strSubstring = str.substring(i13);
                        if ("+0000".equals(strSubstring) || "+00:00".equals(strSubstring)) {
                            timeZone = TIMEZONE_Z;
                        } else {
                            String str2 = GMT_ID + strSubstring;
                            TimeZone timeZone2 = DesugarTimeZone.getTimeZone(str2);
                            String id = timeZone2.getID();
                            if (!id.equals(str2) && !id.replace(Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR, "").equals(str2)) {
                                throw new IndexOutOfBoundsException("Mismatching time zone indicator: " + str2 + " given, resolves to " + timeZone2.getID());
                            }
                            timeZone = timeZone2;
                        }
                    }
                    GregorianCalendar gregorianCalendar = new GregorianCalendar(timeZone);
                    gregorianCalendar.setLenient(false);
                    gregorianCalendar.set(1, i9);
                    gregorianCalendar.set(2, i12 - 1);
                    gregorianCalendar.set(5, i14);
                    gregorianCalendar.set(11, i6);
                    gregorianCalendar.set(12, i7);
                    gregorianCalendar.set(13, i8);
                    gregorianCalendar.set(14, iPow);
                    return gregorianCalendar.getTime();
                }
                i6 = i16;
                i13 = i17;
                i7 = i18;
                iPow = 0;
            } else {
                i6 = 0;
                iPow = 0;
                i7 = 0;
            }
            i8 = 0;
            if (str.length() > i13) {
            }
        } catch (IllegalArgumentException e6) {
            e = e6;
            throw new SerializationException(A0.a.n("Not an RFC 3339 date: ", str), e);
        } catch (IndexOutOfBoundsException e7) {
            e = e7;
            throw new SerializationException(A0.a.n("Not an RFC 3339 date: ", str), e);
        }
    }

    private static int parseInt(String str, int i6, int i7) {
        int i8;
        int i9;
        if (i6 < 0 || i7 > str.length() || i6 > i7) {
            throw new NumberFormatException(str);
        }
        if (i6 < i7) {
            i9 = i6 + 1;
            int iDigit = Character.digit(str.charAt(i6), 10);
            if (iDigit < 0) {
                throw new NumberFormatException("Invalid number: " + str.substring(i6, i7));
            }
            i8 = -iDigit;
        } else {
            i8 = 0;
            i9 = i6;
        }
        while (i9 < i7) {
            int i10 = i9 + 1;
            int iDigit2 = Character.digit(str.charAt(i9), 10);
            if (iDigit2 < 0) {
                throw new NumberFormatException("Invalid number: " + str.substring(i6, i7));
            }
            i8 = (i8 * 10) - iDigit2;
            i9 = i10;
        }
        return -i8;
    }
}
