Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a2e7dc9c authored by Dikra Prasetya's avatar Dikra Prasetya Committed by Cherrypicker Worker
Browse files

Refactor ObexTime to use java.time.Instant to prevent introducing new usages of java.util.Date.

Bug: 266796373
Bug: 271386921
Tag: #refactor
Test: atest BluetoothInstrumentationTests
(cherry picked from https://android-review.googlesource.com/q/commit:f6ce0f2f1137ea35e8501f9e00c66415955876ca)
Merged-In: I4317963a1ad324896609dd4ba98d0a893632ed23
Change-Id: I4317963a1ad324896609dd4ba98d0a893632ed23
Bug: 263323082
parent 1261ab65
Loading
Loading
Loading
Loading
+26 −7
Original line number Diff line number Diff line
@@ -16,8 +16,12 @@

package com.android.bluetooth.mapclient;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
@@ -25,7 +29,7 @@ import java.util.regex.Pattern;

public final class ObexTime {

    private Date mDate;
    private Instant mInstant;

    public ObexTime(String time) {
        /*
@@ -86,26 +90,41 @@ public final class ObexTime {
                builder.setTimeZone(tz);
            }

            mDate = builder.build().getTime();
            mInstant = builder.build().toInstant();
        }
    }

    public ObexTime(Date date) {
        mDate = date;
        mInstant = date.toInstant();
    }

    public ObexTime(Instant instant) {
        mInstant = instant;
    }

    /**
     * @deprecated Use #{@link #getInstant()} instead.
     */
    @Deprecated
    public Date getTime() {
        return mDate;
        if (mInstant == null) {
            return null;
        }
        return Date.from(mInstant);
    }

    public Instant getInstant() {
        return mInstant;
    }

    @Override
    public String toString() {
        if (mDate == null) {
        if (mInstant == null) {
            return null;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(mDate);
        Calendar cal = GregorianCalendar.from(
                ZonedDateTime.ofInstant(mInstant, ZoneId.systemDefault()));

        /* note that months are numbered stating from 0 */
        return String.format(Locale.US, "%04d%02d%02dT%02d%02d%02d", cal.get(Calendar.YEAR),
+30 −6
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.time.Instant;
import java.util.Date;
import java.util.TimeZone;

@@ -56,34 +57,57 @@ public class ObexTimeTest {
    private static final Date VALID_DATE_WITH_OFFSET_POS = new Date(VALID_TS_OFFSET_POS);
    private static final Date VALID_DATE_WITH_OFFSET_NEG = new Date(VALID_TS_OFFSET_NEG);

    private static final Instant VALID_INSTANT = Instant.ofEpochMilli(VALID_TS);
    private static final Instant VALID_INSTANT_WITH_OFFSET_POS =
            Instant.ofEpochMilli(VALID_TS_OFFSET_POS);
    private static final Instant VALID_INSTANT_WITH_OFFSET_NEG =
            Instant.ofEpochMilli(VALID_TS_OFFSET_NEG);

    @Test
    public void createWithValidDateTimeString_TimestampCorrect() {
        ObexTime timestamp = new ObexTime(VALID_TIME_STRING);
        Assert.assertEquals("Parsed timestamp must match expected", VALID_DATE_LOCAL_TZ,
                timestamp.getTime());
        Assert.assertEquals("Parsed instant must match expected", VALID_INSTANT,
                timestamp.getInstant());
        Assert.assertEquals("Parsed date must match expected",
                VALID_DATE_LOCAL_TZ, timestamp.getTime());
    }

    @Test
    public void createWithValidDateTimeStringWithPosOffset_TimestampCorrect() {
        ObexTime timestamp = new ObexTime(VALID_TIME_STRING_WITH_OFFSET_POS);
        Assert.assertEquals("Parsed timestamp must match expected", VALID_DATE_WITH_OFFSET_POS,
                timestamp.getTime());
        Assert.assertEquals("Parsed instant must match expected", VALID_INSTANT_WITH_OFFSET_POS,
                timestamp.getInstant());
        Assert.assertEquals("Parsed date must match expected",
                VALID_DATE_WITH_OFFSET_POS, timestamp.getTime());
    }

    @Test
    public void createWithValidDateTimeStringWithNegOffset_TimestampCorrect() {
        ObexTime timestamp = new ObexTime(VALID_TIME_STRING_WITH_OFFSET_NEG);
        Assert.assertEquals("Parsed timestamp must match expected", VALID_DATE_WITH_OFFSET_NEG,
                timestamp.getTime());
        Assert.assertEquals("Parsed instant must match expected", VALID_INSTANT_WITH_OFFSET_NEG,
                timestamp.getInstant());
        Assert.assertEquals("Parsed date must match expected",
                VALID_DATE_WITH_OFFSET_NEG, timestamp.getTime());
    }

    @Test
    public void createWithValidDate_TimestampCorrect() {
        ObexTime timestamp = new ObexTime(VALID_DATE_LOCAL_TZ);
        Assert.assertEquals("ObexTime created with a date must return the expected instant",
                VALID_INSTANT, timestamp.getInstant());
        Assert.assertEquals("ObexTime created with a date must return the same date",
                VALID_DATE_LOCAL_TZ, timestamp.getTime());
    }

    @Test
    public void createWithValidInstant_TimestampCorrect() {
        ObexTime timestamp = new ObexTime(VALID_INSTANT);
        Assert.assertEquals("ObexTime created with a instant must return the same instant",
                VALID_INSTANT, timestamp.getInstant());
        Assert.assertEquals("ObexTime created with a instant must return the expected date",
                VALID_DATE_LOCAL_TZ, timestamp.getTime());
    }

    @Test
    public void printValidTime_TimestampMatchesInput() {
        ObexTime timestamp = new ObexTime(VALID_TIME_STRING);