diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2010-06-18 12:46:16 -0700 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2010-06-18 12:46:16 -0700 |
commit | f1f33c4eb2ded794c74285d8a036073ae5419f25 (patch) | |
tree | 027051d32d7d47cc99330c51a674042289b0a3d1 /core | |
parent | 766d1b7458e5dae1cca36b01cf13e4d16536a091 (diff) | |
parent | 6b9a1a5124321253a75a17027292080bf6a946e8 (diff) | |
download | frameworks_base-f1f33c4eb2ded794c74285d8a036073ae5419f25.zip frameworks_base-f1f33c4eb2ded794c74285d8a036073ae5419f25.tar.gz frameworks_base-f1f33c4eb2ded794c74285d8a036073ae5419f25.tar.bz2 |
merge from open-source master
Change-Id: If7ae0c5629d1b3c540cacfa517425c2e6b5822d1
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/text/format/DateUtils.java | 83 |
1 files changed, 53 insertions, 30 deletions
diff --git a/core/java/android/text/format/DateUtils.java b/core/java/android/text/format/DateUtils.java index dde0889..89b3cba 100644 --- a/core/java/android/text/format/DateUtils.java +++ b/core/java/android/text/format/DateUtils.java @@ -496,7 +496,7 @@ public class DateUtils } } } else if (duration < WEEK_IN_MILLIS && minResolution < WEEK_IN_MILLIS) { - count = duration / DAY_IN_MILLIS; + count = getNumberOfDaysPassed(time, now); if (past) { if (abbrevRelative) { resId = com.android.internal.R.plurals.abbrev_num_days_ago; @@ -521,6 +521,24 @@ public class DateUtils } /** + * Returns the number of days passed between two dates. + * + * @param date1 first date + * @param date2 second date + * @return number of days passed between to dates. + */ + private synchronized static long getNumberOfDaysPassed(long date1, long date2) { + if (sThenTime == null) { + sThenTime = new Time(); + } + sThenTime.set(date1); + int day1 = Time.getJulianDay(date1, sThenTime.gmtoff); + sThenTime.set(date2); + int day2 = Time.getJulianDay(date2, sThenTime.gmtoff); + return Math.abs(day2 - day1); + } + + /** * Return string describing the elapsed time since startTime formatted like * "[relative time/date], [time]". * <p> @@ -1550,40 +1568,45 @@ public class DateUtils public static CharSequence getRelativeTimeSpanString(Context c, long millis, boolean withPreposition) { + String result; long now = System.currentTimeMillis(); long span = now - millis; - if (sNowTime == null) { - sNowTime = new Time(); - sThenTime = new Time(); - } + synchronized (DateUtils.class) { + if (sNowTime == null) { + sNowTime = new Time(); + } - sNowTime.set(now); - sThenTime.set(millis); + if (sThenTime == null) { + sThenTime = new Time(); + } - String result; - int prepositionId; - if (span < DAY_IN_MILLIS && sNowTime.weekDay == sThenTime.weekDay) { - // Same day - int flags = FORMAT_SHOW_TIME; - result = formatDateRange(c, millis, millis, flags); - prepositionId = R.string.preposition_for_time; - } else if (sNowTime.year != sThenTime.year) { - // Different years - int flags = FORMAT_SHOW_DATE | FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE; - result = formatDateRange(c, millis, millis, flags); - - // This is a date (like "10/31/2008" so use the date preposition) - prepositionId = R.string.preposition_for_date; - } else { - // Default - int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH; - result = formatDateRange(c, millis, millis, flags); - prepositionId = R.string.preposition_for_date; - } - if (withPreposition) { - Resources res = c.getResources(); - result = res.getString(prepositionId, result); + sNowTime.set(now); + sThenTime.set(millis); + + int prepositionId; + if (span < DAY_IN_MILLIS && sNowTime.weekDay == sThenTime.weekDay) { + // Same day + int flags = FORMAT_SHOW_TIME; + result = formatDateRange(c, millis, millis, flags); + prepositionId = R.string.preposition_for_time; + } else if (sNowTime.year != sThenTime.year) { + // Different years + int flags = FORMAT_SHOW_DATE | FORMAT_SHOW_YEAR | FORMAT_NUMERIC_DATE; + result = formatDateRange(c, millis, millis, flags); + + // This is a date (like "10/31/2008" so use the date preposition) + prepositionId = R.string.preposition_for_date; + } else { + // Default + int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH; + result = formatDateRange(c, millis, millis, flags); + prepositionId = R.string.preposition_for_date; + } + if (withPreposition) { + Resources res = c.getResources(); + result = res.getString(prepositionId, result); + } } return result; } |