use of org.joda.time.Seconds in project android-oss by kickstarter.
the class DateTimeUtils method relative.
/**
* Returns a string indicating the distance between {@link DateTime}s. Defaults to comparing the input {@link DateTime} to
* the current time.
*/
@NonNull
public static String relative(@NonNull final Context context, @NonNull final KSString ksString, @NonNull final DateTime dateTime, @NonNull final RelativeDateTimeOptions options) {
final DateTime relativeToDateTime = ObjectUtils.coalesce(options.relativeToDateTime(), DateTime.now());
final Seconds seconds = Seconds.secondsBetween(dateTime, relativeToDateTime);
final int secondsDifference = seconds.getSeconds();
if (secondsDifference >= 0.0 && secondsDifference <= 60.0) {
return context.getString(R.string.dates_just_now);
} else if (secondsDifference >= -60.0 && secondsDifference <= 0.0) {
return context.getString(R.string.dates_right_now);
}
final Pair<String, Integer> unitAndDifference = unitAndDifference(secondsDifference, options.threshold());
if (unitAndDifference == null) {
// Couldn't find a good match, just render the date.
return mediumDate(dateTime);
}
final String unit = unitAndDifference.first;
final int difference = unitAndDifference.second;
boolean willHappenIn = false;
boolean happenedAgo = false;
if (!options.absolute()) {
if (secondsDifference < 0) {
willHappenIn = true;
} else if (secondsDifference > 0) {
happenedAgo = true;
}
}
if (happenedAgo && "days".equals(unit) && difference == 1) {
return context.getString(R.string.dates_yesterday);
}
final StringBuilder baseKeyPath = new StringBuilder();
if (willHappenIn) {
baseKeyPath.append(String.format("dates_time_in_%s", unit));
} else if (happenedAgo) {
baseKeyPath.append(String.format("dates_time_%s_ago", unit));
} else {
baseKeyPath.append(String.format("dates_time_%s", unit));
}
if (options.abbreviated()) {
baseKeyPath.append("_abbreviated");
}
return ksString.format(baseKeyPath.toString(), difference, "time_count", NumberUtils.format(difference, NumberOptions.builder().build()));
}