Skip to content

Commit 7420ab6

Browse files
jsharkeyAndroid (Google) Code Review
authored andcommitted
Merge "Utility to format human-friendly durations." into jb-mr1.1-dev
2 parents a319d65 + 53f6e8a commit 7420ab6

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

core/java/android/text/format/DateUtils.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,30 @@ public static final CharSequence timeString(long millis) {
606606
}
607607
}
608608

609+
/**
610+
* Return given duration in a human-friendly format. For example, "4
611+
* minutes" or "1 second". Returns only largest meaningful unit of time,
612+
* from seconds up to hours.
613+
*
614+
* @hide
615+
*/
616+
public static CharSequence formatDuration(long millis) {
617+
final Resources res = Resources.getSystem();
618+
if (millis >= HOUR_IN_MILLIS) {
619+
final int hours = (int) ((millis + 1800000) / HOUR_IN_MILLIS);
620+
return res.getQuantityString(
621+
com.android.internal.R.plurals.duration_hours, hours, hours);
622+
} else if (millis >= MINUTE_IN_MILLIS) {
623+
final int minutes = (int) ((millis + 30000) / MINUTE_IN_MILLIS);
624+
return res.getQuantityString(
625+
com.android.internal.R.plurals.duration_minutes, minutes, minutes);
626+
} else {
627+
final int seconds = (int) ((millis + 500) / SECOND_IN_MILLIS);
628+
return res.getQuantityString(
629+
com.android.internal.R.plurals.duration_seconds, seconds, seconds);
630+
}
631+
}
632+
609633
/**
610634
* Formats an elapsed time in the form "MM:SS" or "H:MM:SS"
611635
* for display on the call-in-progress screen.

core/res/res/values/strings.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,6 +2838,21 @@
28382838
<!-- Appened to express the value is this unit of time. -->
28392839
<string name="years">years</string>
28402840

2841+
<!-- Phrase describing a time duration using seconds [CHAR LIMIT=16] -->
2842+
<plurals name="duration_seconds">
2843+
<item quantity="one">1 second</item>
2844+
<item quantity="other"><xliff:g id="count">%d</xliff:g> seconds</item>
2845+
</plurals>
2846+
<!-- Phrase describing a time duration using minutes [CHAR LIMIT=16] -->
2847+
<plurals name="duration_minutes">
2848+
<item quantity="one">1 minute</item>
2849+
<item quantity="other"><xliff:g id="count">%d</xliff:g> minutes</item>
2850+
</plurals>
2851+
<!-- Phrase describing a time duration using hours [CHAR LIMIT=16] -->
2852+
<plurals name="duration_hours">
2853+
<item quantity="one">1 hour</item>
2854+
<item quantity="other"><xliff:g id="count">%d</xliff:g> hours</item>
2855+
</plurals>
28412856

28422857
<!-- Title for error alert when a video cannot be played. it can be used by any app. -->
28432858
<string name="VideoView_error_title">Video problem</string>

core/res/res/values/symbols.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,9 @@
870870
<java-symbol type="plurals" name="abbrev_num_hours_ago" />
871871
<java-symbol type="plurals" name="abbrev_num_minutes_ago" />
872872
<java-symbol type="plurals" name="abbrev_num_seconds_ago" />
873+
<java-symbol type="plurals" name="duration_hours" />
874+
<java-symbol type="plurals" name="duration_minutes" />
875+
<java-symbol type="plurals" name="duration_seconds" />
873876
<java-symbol type="plurals" name="in_num_days" />
874877
<java-symbol type="plurals" name="in_num_hours" />
875878
<java-symbol type="plurals" name="in_num_minutes" />
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2012 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.text.format;
18+
19+
import android.test.suitebuilder.annotation.SmallTest;
20+
21+
import junit.framework.TestCase;
22+
23+
public class DateUtilsTest extends TestCase {
24+
@SmallTest
25+
public void testFormatDurationSeconds() throws Exception {
26+
assertEquals("0 seconds", DateUtils.formatDuration(0));
27+
assertEquals("0 seconds", DateUtils.formatDuration(1));
28+
assertEquals("0 seconds", DateUtils.formatDuration(499));
29+
assertEquals("1 second", DateUtils.formatDuration(500));
30+
assertEquals("1 second", DateUtils.formatDuration(1000));
31+
assertEquals("2 seconds", DateUtils.formatDuration(1500));
32+
}
33+
34+
@SmallTest
35+
public void testFormatDurationMinutes() throws Exception {
36+
assertEquals("59 seconds", DateUtils.formatDuration(59000));
37+
assertEquals("60 seconds", DateUtils.formatDuration(59500));
38+
assertEquals("1 minute", DateUtils.formatDuration(60000));
39+
assertEquals("2 minutes", DateUtils.formatDuration(120000));
40+
}
41+
42+
@SmallTest
43+
public void testFormatDurationHours() throws Exception {
44+
assertEquals("59 minutes", DateUtils.formatDuration(3540000));
45+
assertEquals("1 hour", DateUtils.formatDuration(3600000));
46+
assertEquals("48 hours", DateUtils.formatDuration(172800000));
47+
}
48+
}

0 commit comments

Comments
 (0)