Added a version of getTimeOrderString where one can specify the maximum

time unit to use.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3070 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2004-08-09 22:27:45 +00:00
parent be14cd3877
commit 439f2b2d2b
+27 -10
View File
@@ -1,5 +1,5 @@
//
// $Id: TimeUtil.java,v 1.2 2004/06/04 22:14:11 ray Exp $
// $Id: TimeUtil.java,v 1.3 2004/08/09 22:27:45 ray Exp $
package com.threerings.util;
@@ -23,47 +23,64 @@ public class TimeUtil
/** Granularity constant. */
public static final byte DAY = 4;
// TODO: Weeks?, months?
/**
* Get a translatable string specifying the magnitude of the specified
* duration. Results will be between "1 second" and "X hours", with
* all times rounded to the nearest unit.
*/
public static String getTimeOrderString (long duration, byte granularity)
public static String getTimeOrderString (
long duration, byte minGranularity)
{
if (granularity == MILLISECOND) {
return getTimeOrderString(duration, minGranularity, Byte.MAX_VALUE);
}
/**
* Get a translatable string specifying the magnitude of the specified
* duration, with the units of time bounded between the minimum and
* maximum specified.
*/
public static String getTimeOrderString (
long duration, byte minGranularity, byte maxGranularity)
{
// enforce sanity
minGranularity = (byte) Math.min(minGranularity, maxGranularity);
if (minGranularity == MILLISECOND) {
if (duration < 2) {
return "m.1millisecond";
} else if (duration < 1000) {
} else if (duration < 1000 || maxGranularity == MILLISECOND) {
return MessageBundle.tcompose("m.milliseconds",
String.valueOf(duration));
}
}
int seconds = (int) Math.round(duration / 1000f);
if (granularity <= SECOND) {
if (minGranularity <= SECOND) {
if (seconds < 2) {
return "m.1second";
} else if (seconds < 60) {
} else if (seconds < 60 || maxGranularity == SECOND) {
return MessageBundle.tcompose("m.seconds",
String.valueOf(seconds));
}
}
int minutes = (int) Math.round(seconds / 60f);
if (granularity <= MINUTE) {
if (minGranularity <= MINUTE) {
if (minutes < 2) {
return "m.1minute";
} else if (minutes < 60) {
} else if (minutes < 60 || maxGranularity == MINUTE) {
return MessageBundle.tcompose("m.minutes",
String.valueOf(minutes));
}
}
int hours = (int) Math.round(minutes / 60f);
if (granularity <= HOUR) {
if (minGranularity <= HOUR) {
if (hours < 2) {
return "m.1hour";
} else if (hours < 24) {
} else if (hours < 24 || maxGranularity == HOUR) {
return MessageBundle.tcompose("m.hours", String.valueOf(hours));
}
}