package com.amplifyframework.core.model.temporal;

import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import s3.d;

/* loaded from: classes4.dex */
public final class Temporal {

    /* loaded from: classes4.dex */
    public static final class Date implements Comparable<Date>, Serializable {
        private static final long serialVersionUID = 1;
        private final LocalDate localDate;
        private final ZoneOffset zoneOffset;

        public Date(java.util.Date date) {
            this.zoneOffset = null;
            this.localDate = Instant.ofEpochMilli(date.getTime()).atOffset(ZoneOffset.UTC).toLocalDate();
        }

        private DateTimeFormatter getOffsetDateTimeFormatter() {
            return new DateTimeFormatterBuilder().append(DateTimeFormatter.ISO_OFFSET_DATE).parseDefaulting(ChronoField.HOUR_OF_DAY, 0L).parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0L).toFormatter();
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || Date.class != obj.getClass()) {
                return false;
            }
            Date date = (Date) obj;
            return d.a(this.localDate, date.localDate) && d.a(this.zoneOffset, date.zoneOffset);
        }

        public String format() {
            ZoneOffset zoneOffset = this.zoneOffset;
            if (zoneOffset == null) {
                return DateTimeFormatter.ISO_LOCAL_DATE.format(this.localDate);
            }
            return getOffsetDateTimeFormatter().format(OffsetDateTime.of(this.localDate, LocalTime.MIDNIGHT, zoneOffset));
        }

        public int getOffsetTotalSeconds() throws IllegalStateException {
            ZoneOffset zoneOffset = this.zoneOffset;
            if (zoneOffset != null) {
                return zoneOffset.getTotalSeconds();
            }
            throw new IllegalStateException("Temporal.Date instance does not have a timezone offset.");
        }

        public int hashCode() {
            int hashCode = this.localDate.hashCode() * 31;
            ZoneOffset zoneOffset = this.zoneOffset;
            return hashCode + (zoneOffset != null ? zoneOffset.hashCode() : 0);
        }

        public java.util.Date toDate() {
            ZoneOffset zoneOffset = this.zoneOffset;
            if (zoneOffset == null) {
                zoneOffset = ZoneOffset.UTC;
            }
            return new java.util.Date(OffsetDateTime.of(this.localDate, LocalTime.MIDNIGHT, zoneOffset).toInstant().toEpochMilli());
        }

        public String toString() {
            return "Temporal.Date{localDate='" + this.localDate + "', zoneOffset='" + this.zoneOffset + "'}";
        }

        @Override // java.lang.Comparable
        public int compareTo(Date date) {
            Objects.requireNonNull(date);
            return toDate().compareTo(date.toDate());
        }

        public Date(java.util.Date date, int i10) {
            ZoneOffset ofTotalSeconds = ZoneOffset.ofTotalSeconds(i10);
            this.zoneOffset = ofTotalSeconds;
            this.localDate = Instant.ofEpochMilli(date.getTime()).atOffset(ofTotalSeconds).toLocalDate();
        }

        public Date(String str) {
            LocalDate parse;
            ZoneOffset zoneOffset;
            try {
                OffsetDateTime parse2 = OffsetDateTime.parse(str, getOffsetDateTimeFormatter());
                parse = LocalDate.from((TemporalAccessor) parse2);
                zoneOffset = ZoneOffset.from((TemporalAccessor) parse2);
            } catch (Exception e10) {
                try {
                    parse = LocalDate.parse(str, DateTimeFormatter.ISO_LOCAL_DATE);
                    zoneOffset = null;
                } catch (Exception unused) {
                    throw new IllegalArgumentException("Failed to create Temporal.Date object from " + str, e10);
                }
            }
            this.localDate = parse;
            this.zoneOffset = zoneOffset;
        }
    }

    /* loaded from: classes4.dex */
    public static final class DateTime implements Comparable<DateTime>, Serializable {
        private static final long serialVersionUID = 1;
        private final OffsetDateTime offsetDateTime;

        public DateTime(java.util.Date date, int i10) {
            this.offsetDateTime = Instant.ofEpochMilli(date.getTime()).atOffset(ZoneOffset.ofTotalSeconds(i10));
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || DateTime.class != obj.getClass()) {
                return false;
            }
            return d.a(this.offsetDateTime, ((DateTime) obj).offsetDateTime);
        }

        public String format() {
            return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(this.offsetDateTime);
        }

        public int getOffsetTotalSeconds() {
            return this.offsetDateTime.getOffset().getTotalSeconds();
        }

        public int hashCode() {
            return this.offsetDateTime.hashCode();
        }

        public java.util.Date toDate() {
            return new java.util.Date(this.offsetDateTime.toInstant().toEpochMilli());
        }

        public String toString() {
            return "Temporal.DateTime{offsetDateTime='" + this.offsetDateTime + "'}";
        }

        @Override // java.lang.Comparable
        public int compareTo(DateTime dateTime) {
            Objects.requireNonNull(dateTime);
            return toDate().compareTo(dateTime.toDate());
        }

        public DateTime(String str) {
            try {
                this.offsetDateTime = OffsetDateTime.parse(str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
            } catch (Exception e10) {
                throw new IllegalArgumentException("Failed to create Temporal.DateTime object from " + str, e10);
            }
        }
    }

    /* loaded from: classes4.dex */
    public static final class Time implements Comparable<Time>, Serializable {
        private static final long serialVersionUID = 1;
        private final LocalTime localTime;
        private final ZoneOffset zoneOffset;

        public Time(java.util.Date date) {
            this.zoneOffset = null;
            this.localTime = Instant.ofEpochMilli(date.getTime()).atOffset(ZoneOffset.UTC).toLocalTime();
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || Time.class != obj.getClass()) {
                return false;
            }
            Time time = (Time) obj;
            return d.a(this.localTime, time.localTime) && d.a(this.zoneOffset, time.zoneOffset);
        }

        public String format() {
            ZoneOffset zoneOffset = this.zoneOffset;
            if (zoneOffset == null) {
                return DateTimeFormatter.ISO_LOCAL_TIME.format(this.localTime);
            }
            return DateTimeFormatter.ISO_OFFSET_TIME.format(OffsetTime.of(this.localTime, zoneOffset));
        }

        public int getOffsetTotalSeconds() throws IllegalStateException {
            ZoneOffset zoneOffset = this.zoneOffset;
            if (zoneOffset != null) {
                return zoneOffset.getTotalSeconds();
            }
            throw new IllegalStateException("Temporal.Time instance does not have a timezone offset.");
        }

        public int hashCode() {
            int hashCode = this.localTime.hashCode() * 31;
            ZoneOffset zoneOffset = this.zoneOffset;
            return hashCode + (zoneOffset != null ? zoneOffset.hashCode() : 0);
        }

        public java.util.Date toDate() {
            ZoneOffset zoneOffset = this.zoneOffset;
            if (zoneOffset == null) {
                zoneOffset = ZoneOffset.UTC;
            }
            return new java.util.Date(OffsetDateTime.of(LocalDate.ofEpochDay(0L), this.localTime, zoneOffset).toInstant().toEpochMilli());
        }

        public String toString() {
            return "Temporal.Time{localTime='" + this.localTime + "', zoneOffset='" + this.zoneOffset + "'}";
        }

        @Override // java.lang.Comparable
        public int compareTo(Time time) {
            Objects.requireNonNull(time);
            return toDate().compareTo(time.toDate());
        }

        public Time(java.util.Date date, int i10) {
            ZoneOffset ofTotalSeconds = ZoneOffset.ofTotalSeconds(i10);
            this.zoneOffset = ofTotalSeconds;
            this.localTime = Instant.ofEpochMilli(date.getTime()).atOffset(ofTotalSeconds).toLocalTime();
        }

        public Time(String str) {
            LocalTime parse;
            ZoneOffset zoneOffset;
            try {
                OffsetTime parse2 = OffsetTime.parse(str, DateTimeFormatter.ISO_OFFSET_TIME);
                parse = LocalTime.from(parse2);
                zoneOffset = ZoneOffset.from((TemporalAccessor) parse2);
            } catch (Exception e10) {
                try {
                    parse = LocalTime.parse(str, DateTimeFormatter.ISO_LOCAL_TIME);
                    zoneOffset = null;
                } catch (Exception unused) {
                    throw new IllegalArgumentException("Failed to create Temporal.Time object from " + str, e10);
                }
            }
            this.localTime = parse;
            this.zoneOffset = zoneOffset;
        }
    }

    /* loaded from: classes4.dex */
    public static final class Timestamp implements Comparable<Timestamp>, Serializable {
        private static final long serialVersionUID = 1;
        private final long secondsSinceEpoch;

        public Timestamp() {
            this(new java.util.Date());
        }

        public static Timestamp now() {
            return new Timestamp();
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            return obj != null && Timestamp.class == obj.getClass() && this.secondsSinceEpoch == ((Timestamp) obj).secondsSinceEpoch;
        }

        public long getSecondsSinceEpoch() {
            return this.secondsSinceEpoch;
        }

        public int hashCode() {
            long j10 = this.secondsSinceEpoch;
            return (int) (j10 ^ (j10 >>> 32));
        }

        public String toString() {
            return "Temporal.Timestamp{timestamp=" + this.secondsSinceEpoch + '}';
        }

        public Timestamp(long j10, TimeUnit timeUnit) {
            this.secondsSinceEpoch = timeUnit.toSeconds(j10);
        }

        @Override // java.lang.Comparable
        public int compareTo(Timestamp timestamp) {
            Objects.requireNonNull(timestamp);
            return Long.compare(getSecondsSinceEpoch(), timestamp.getSecondsSinceEpoch());
        }

        public Timestamp(java.util.Date date) {
            this(date.getTime(), TimeUnit.MILLISECONDS);
        }
    }

    private Temporal() {
    }
}
