Some tweaks to getTimeString.

First & easiest, no longer include zeros in our built-up string. It used to
say things like "3 days, 0 hours, 2 minutes, 0 seconds" if your minUnit was
seconds. Now we'll just tell you it's 3 days & 2 minutes.

Second, it used to (and still does, if you use the old-signature wrapper)
round DOWN to the nearest minUnit.

For example, 59 minutes, shown with minUnit of hours, would claim to be
0 hours. For some things (e.g. showing time remaining on a subscription), that
kind of pessimistic display is the safe approach. For other things
(e.g. come back in 0 hours and your temp ban is over), we'd rather err on the
side of saying TOO much time (e.g. telling someone their suspension will be
over in 2 hours when they really only need to wait 61 minutes)

There's other rework that I'm itching to do in this stuff, but no, let's just
give ourselves an option on this particular thing so we can clean up one thing
in yohoho and be done with it for now.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6096 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Dave Hoover
2010-07-14 20:59:02 +00:00
parent 6bff220be7
commit dc11108af4
+33 -6
View File
@@ -86,8 +86,7 @@ public class TimeUtil
int quantity = getQuantityPerUnit(uu);
if ((minUnit <= uu) && (duration < quantity || maxUnit == uu)) {
duration = Math.max(1, duration);
return MessageBundle.tcompose(getTransKey(uu),
String.valueOf(duration));
return MessageBundle.tcompose(getTransKey(uu), String.valueOf(duration));
}
duration = Math.round(duration / quantity);
}
@@ -101,19 +100,42 @@ public class TimeUtil
* Get a translatable string specifying the duration, down to the minimum granularity.
*/
public static String getTimeString (long duration, byte minUnit)
{
return getTimeString(duration, minUnit, false);
}
/**
* Get a translatable string specifying the duration, down to the minimum granularity.
*
* Normally rounds down to the nearest minUnit, but optionally rounds up.
*/
public static String getTimeString (long duration, byte minUnit, boolean roundUp)
{
// sanity
minUnit = (byte) Math.min(minUnit, MAX_UNIT);
duration = Math.abs(duration);
if (roundUp) {
long quantity = 1;
for (byte uu = MILLISECOND; uu < MAX_UNIT - 1; uu++) {
quantity *= getQuantityPerUnit(uu);
}
if (duration % quantity > 0) {
duration += quantity;
}
}
ArrayList<String> list = Lists.newArrayList();
int parts = 0; // how many parts are in the translation string?
for (byte uu = MILLISECOND; uu <= MAX_UNIT; uu++) {
int quantity = getQuantityPerUnit(uu);
if (minUnit <= uu) {
list.add(MessageBundle.tcompose(getTransKey(uu),
String.valueOf(duration % quantity)));
parts++;
long amt = duration % quantity;
if (amt != 0) {
list.add(MessageBundle.tcompose(getTransKey(uu), String.valueOf(amt)));
parts++;
}
}
duration /= quantity;
if (duration <= 0 && parts > 0) {
@@ -121,8 +143,13 @@ public class TimeUtil
}
}
if (parts == 1) {
if (parts == 0) {
// Wow, we didn't get ANYTHING? Okay, I guess that means it's zero of our minUnit
return MessageBundle.tcompose(getTransKey(minUnit), 0);
} else if (parts == 1) {
return list.get(0);
} else {
return MessageBundle.compose("m.times_" + parts, list.toArray());
}