summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/com/android/contacts/common/util/DateUtils.java48
-rw-r--r--tests/src/com/android/contacts/common/util/DateUtilTests.java76
2 files changed, 124 insertions, 0 deletions
diff --git a/src/com/android/contacts/common/util/DateUtils.java b/src/com/android/contacts/common/util/DateUtils.java
index f527eb95..5d8ba70f 100644
--- a/src/com/android/contacts/common/util/DateUtils.java
+++ b/src/com/android/contacts/common/util/DateUtils.java
@@ -18,6 +18,7 @@ package com.android.contacts.common.util;
import android.content.Context;
import android.text.format.DateFormat;
+import android.text.format.Time;
import java.text.ParsePosition;
@@ -268,4 +269,51 @@ public class DateUtils {
}
return anniversary.getTime();
}
+
+ /**
+ * Retrieves a locale-specific string for "Today".
+ *
+ * @return Locale-specific string for "Today".
+ */
+ public static CharSequence getTodayString() {
+ return android.text.format.DateUtils.getRelativeTimeSpanString(
+ -android.text.format.DateUtils.HOUR_IN_MILLIS,
+ 0,
+ android.text.format.DateUtils.DAY_IN_MILLIS,
+ android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE);
+
+ }
+
+ /**
+ * Retrieves a locale-specific string for "Yesterday".
+ *
+ * @return Locale-specific string for "Yesterday".
+ */
+ public static CharSequence getYesterdayString() {
+ return android.text.format.DateUtils.getRelativeTimeSpanString(
+ -android.text.format.DateUtils.DAY_IN_MILLIS,
+ 0,
+ android.text.format.DateUtils.DAY_IN_MILLIS,
+ android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE);
+
+ }
+
+ /**
+ * Determine the difference, in days between two dates. Uses similar logic as the
+ * {@link android.text.format.DateUtils.getRelativeTimeSpanString} method.
+ *
+ * @param time Instance of time object to use for calculations.
+ * @param date1 First date to check.
+ * @param date2 Second date to check.
+ * @return The absolute difference in days between the two dates.
+ */
+ public static int getDayDifference(Time time, long date1, long date2) {
+ time.set(date1);
+ int startDay = Time.getJulianDay(date1, time.gmtoff);
+
+ time.set(date2);
+ int currentDay = Time.getJulianDay(date2, time.gmtoff);
+
+ return Math.abs(currentDay - startDay);
+ }
}
diff --git a/tests/src/com/android/contacts/common/util/DateUtilTests.java b/tests/src/com/android/contacts/common/util/DateUtilTests.java
new file mode 100644
index 00000000..c225780e
--- /dev/null
+++ b/tests/src/com/android/contacts/common/util/DateUtilTests.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.contacts.common.util;
+
+import junit.framework.TestCase;
+
+import android.test.suitebuilder.annotation.SmallTest;
+import android.text.format.Time;
+
+/**
+ * Unit tests for {@link com.android.contacts.common.util.DateUtils}.
+ */
+@SmallTest
+public class DateUtilTests extends TestCase {
+
+ /**
+ * Test ability to get the word "Yesterday".
+ */
+ public void testGetYesterday() {
+ assertEquals("Yesterday", DateUtils.getYesterdayString());
+ }
+
+ /**
+ * Test ability to get the word "Today".
+ */
+ public void testGetToday() {
+ assertEquals("Yesterday", DateUtils.getYesterdayString());
+ }
+
+ /**
+ * Test date differences which are in the same day.
+ */
+ public void testDayDiffNone() {
+ Time time = new Time();
+ long date1 = System.currentTimeMillis();
+ long date2 = System.currentTimeMillis() + android.text.format.DateUtils.HOUR_IN_MILLIS;
+ assertEquals(0, DateUtils.getDayDifference(time, date1, date2));
+ assertEquals(0, DateUtils.getDayDifference(time, date2, date1));
+ }
+
+ /**
+ * Test date differences which are a day apart.
+ */
+ public void testDayDiffOne() {
+ Time time = new Time();
+ long date1 = System.currentTimeMillis();
+ long date2 = date1 + android.text.format.DateUtils.DAY_IN_MILLIS;
+ assertEquals(1, DateUtils.getDayDifference(time, date1, date2));
+ assertEquals(1, DateUtils.getDayDifference(time, date2, date1));
+ }
+
+ /**
+ * Test date differences which are two days apart.
+ */
+ public void testDayDiffTwo() {
+ Time time = new Time();
+ long date1 = System.currentTimeMillis();
+ long date2 = date1 + 2*android.text.format.DateUtils.DAY_IN_MILLIS;
+ assertEquals(2, DateUtils.getDayDifference(time, date1, date2));
+ assertEquals(2, DateUtils.getDayDifference(time, date2, date1));
+ }
+}