Changeset 6661 in josm for trunk/src


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

fix #9561 - Bug in getDurationString

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.