Changeset 6661 in josm
- Timestamp:
- 2014-01-10T16:39:29+01:00 (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Utils.java
r6652 r6661 833 833 * Returns a simple human readable (hours, minutes, seconds) string for a given duration in milliseconds. 834 834 * @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 836 836 * @throws IllegalArgumentException if elapsedTime is < 0 837 837 * @since 6354 838 838 */ 839 839 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; 840 844 if (elapsedTime < 0) { 841 845 throw new IllegalArgumentException("elapsedTime must be > 0"); 842 846 } 843 847 // Is it less than 1 second ? 844 if (elapsedTime < 1000) {848 if (elapsedTime < MILLIS_OF_SECOND) { 845 849 return String.format("%d %s", elapsedTime, tr("ms")); 846 850 } 847 851 // 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")); 850 854 } 851 855 // 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")); 854 859 } 855 860 // 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")); 861 867 } 862 868 -
trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
r6652 r6661 9 9 import java.net.URL; 10 10 import java.util.Arrays; 11 import java.util.Locale; 11 12 12 13 import static org.hamcrest.CoreMatchers.is; … … 99 100 assertThat(Utils.getPositionListString(Arrays.asList(1, 5, 2, 6, 7)), is("1-2,5-7")); 100 101 } 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 } 101 114 }
Note:
See TracChangeset
for help on using the changeset viewer.