From 439f2b2d2b8ea93e6e4eee97452014b658c39a7b Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Mon, 9 Aug 2004 22:27:45 +0000 Subject: [PATCH] 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 --- src/java/com/threerings/util/TimeUtil.java | 37 ++++++++++++++++------ 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/java/com/threerings/util/TimeUtil.java b/src/java/com/threerings/util/TimeUtil.java index a8bca3707..75e60d61e 100644 --- a/src/java/com/threerings/util/TimeUtil.java +++ b/src/java/com/threerings/util/TimeUtil.java @@ -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)); } }