Created
November 20, 2015 06:49
-
-
Save jaydeepw/4e34cf0ff1624707e7af to your computer and use it in GitHub Desktop.
relative-time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns a string describing 'time' as a time relative to 'now'. | |
* <p> | |
* Time spans in the past are formatted like "42 minutes ago". Time spans in | |
* the future are formatted like "in 42 minutes". | |
* <p> | |
* Can use {@link #FORMAT_ABBREV_RELATIVE} flag to use abbreviated relative | |
* times, like "42 mins ago". | |
* | |
* @param time the time to describe, in milliseconds | |
* @param now the current time in milliseconds | |
* @param minResolution the minimum timespan to report. For example, a time | |
* 3 seconds in the past will be reported as "0 minutes ago" if | |
* this is set to MINUTE_IN_MILLIS. Pass one of 0, | |
* MINUTE_IN_MILLIS, HOUR_IN_MILLIS, DAY_IN_MILLIS, | |
* WEEK_IN_MILLIS | |
* @param flags a bit mask of formatting options, such as | |
* {@link #FORMAT_NUMERIC_DATE} or | |
* {@link #FORMAT_ABBREV_RELATIVE} | |
*/ | |
public static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution, | |
int flags) { | |
Resources r = Resources.getSystem(); | |
boolean abbrevRelative = (flags & (FORMAT_ABBREV_RELATIVE | FORMAT_ABBREV_ALL)) != 0; | |
boolean past = (now >= time); | |
long duration = Math.abs(now - time); | |
int resId; | |
long count; | |
if (duration < MINUTE_IN_MILLIS && minResolution < MINUTE_IN_MILLIS) { | |
count = duration / SECOND_IN_MILLIS; | |
if (past) { | |
if (abbrevRelative) { | |
resId = com.android.internal.R.plurals.abbrev_num_seconds_ago; | |
} else { | |
resId = com.android.internal.R.plurals.num_seconds_ago; | |
} | |
} else { | |
if (abbrevRelative) { | |
resId = com.android.internal.R.plurals.abbrev_in_num_seconds; | |
} else { | |
resId = com.android.internal.R.plurals.in_num_seconds; | |
} | |
} | |
} else if (duration < HOUR_IN_MILLIS && minResolution < HOUR_IN_MILLIS) { | |
count = duration / MINUTE_IN_MILLIS; | |
if (past) { | |
if (abbrevRelative) { | |
resId = com.android.internal.R.plurals.abbrev_num_minutes_ago; | |
} else { | |
resId = com.android.internal.R.plurals.num_minutes_ago; | |
} | |
} else { | |
if (abbrevRelative) { | |
resId = com.android.internal.R.plurals.abbrev_in_num_minutes; | |
} else { | |
resId = com.android.internal.R.plurals.in_num_minutes; | |
} | |
} | |
} else if (duration < DAY_IN_MILLIS && minResolution < DAY_IN_MILLIS) { | |
count = duration / HOUR_IN_MILLIS; | |
if (past) { | |
if (abbrevRelative) { | |
resId = com.android.internal.R.plurals.abbrev_num_hours_ago; | |
} else { | |
resId = com.android.internal.R.plurals.num_hours_ago; | |
} | |
} else { | |
if (abbrevRelative) { | |
resId = com.android.internal.R.plurals.abbrev_in_num_hours; | |
} else { | |
resId = com.android.internal.R.plurals.in_num_hours; | |
} | |
} | |
} else if (duration < WEEK_IN_MILLIS && minResolution < WEEK_IN_MILLIS) { | |
return getRelativeDayString(r, time, now); | |
} else { | |
// We know that we won't be showing the time, so it is safe to pass | |
// in a null context. | |
return formatDateRange(null, time, time, flags); | |
} | |
String format = r.getQuantityString(resId, (int) count); | |
return String.format(format, count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment