Changeset 6661 in josm


Ignore:
Timestamp:
2014-01-10T16:39:29+01:00 (11 years ago)
Author:
simon04
Message:

fix #9561 - Bug in getDurationString

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r6652 r6661  
    833833     * Returns a simple human readable (hours, minutes, seconds) string for a given duration in milliseconds.
    834834     * @param elapsedTime The duration in milliseconds
    835      * @return A human redable string for the given duration
     835     * @return A human readable string for the given duration
    836836     * @throws IllegalArgumentException if elapsedTime is < 0
    837837     * @since 6354
    838838     */
    839839    public static String getDurationString(long elapsedTime) throws IllegalArgumentException {
     840        final int MILLIS_OF_SECOND = 1000;
     841        final int MILLIS_OF_MINUTE = 60000;
     842        final int MILLIS_OF_HOUR = 3600000;
     843        final int MILLIS_OF_DAY = 86400000;
    840844        if (elapsedTime < 0) {
    841845            throw new IllegalArgumentException("elapsedTime must be > 0");
    842846        }
    843847        // Is it less than 1 second ?
    844         if (elapsedTime < 1000) {
     848        if (elapsedTime < MILLIS_OF_SECOND) {
    845849            return String.format("%d %s", elapsedTime, tr("ms"));
    846850        }
    847851        // Is it less than 1 minute ?
    848         if (elapsedTime < 60*1000) {
    849             return String.format("%.1f %s", elapsedTime/1000f, tr("s"));
     852        if (elapsedTime < MILLIS_OF_MINUTE) {
     853            return String.format("%.1f %s", elapsedTime / (float) MILLIS_OF_SECOND, tr("s"));
    850854        }
    851855        // Is it less than 1 hour ?
    852         if (elapsedTime < 60*60*1000) {
    853             return String.format("%d %s %d %s", elapsedTime/60000, tr("min"), elapsedTime/1000, tr("s"));
     856        if (elapsedTime < MILLIS_OF_HOUR) {
     857            final long min = elapsedTime / MILLIS_OF_MINUTE;
     858            return String.format("%d %s %d %s", min, tr("min"), (elapsedTime - min * MILLIS_OF_MINUTE) / MILLIS_OF_SECOND, tr("s"));
    854859        }
    855860        // Is it less than 1 day ?
    856         if (elapsedTime < 24*60*60*1000) {
    857             return String.format("%d %s %d %s", elapsedTime/3600000, tr("h"), elapsedTime/60000, tr("min"));
    858         }
    859         long days = elapsedTime/86400000;
    860         return String.format("%d %s %d %s", days, trn("day", "days", days), elapsedTime/3600000, tr("h"));
     861        if (elapsedTime < MILLIS_OF_DAY) {
     862            final long hour = elapsedTime / MILLIS_OF_HOUR;
     863            return String.format("%d %s %d %s", hour, tr("h"), (elapsedTime - hour * MILLIS_OF_HOUR) / MILLIS_OF_MINUTE, tr("min"));
     864        }
     865        long days = elapsedTime / MILLIS_OF_DAY;
     866        return String.format("%d %s %d %s", days, trn("day", "days", days), (elapsedTime - days * MILLIS_OF_DAY) / MILLIS_OF_HOUR, tr("h"));
    861867    }
    862868
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r6652 r6661  
    99import java.net.URL;
    1010import java.util.Arrays;
     11import java.util.Locale;
    1112
    1213import static org.hamcrest.CoreMatchers.is;
     
    99100        assertThat(Utils.getPositionListString(Arrays.asList(1, 5, 2, 6, 7)), is("1-2,5-7"));
    100101    }
     102
     103    @Test
     104    public void testDurationString() throws Exception {
     105        Locale.setDefault(Locale.ENGLISH);
     106        assertThat(Utils.getDurationString(123), is("123 ms"));
     107        assertThat(Utils.getDurationString(1234), is("1.2 s"));
     108        assertThat(Utils.getDurationString(57 * 1000), is("57.0 s"));
     109        assertThat(Utils.getDurationString(507 * 1000), is("8 min 27 s"));
     110        assertThat(Utils.getDurationString((long) (8.4 * 60 * 60 * 1000)), is("8 h 24 min"));
     111        assertThat(Utils.getDurationString((long) (1.5 * 24 * 60 * 60 * 1000)), is("1 day 12 h"));
     112        assertThat(Utils.getDurationString((long) (8.5 * 24 * 60 * 60 * 1000)), is("8 days 12 h"));
     113    }
    101114}
Note: See TracChangeset for help on using the changeset viewer.