Skip to content

Commit a251494

Browse files
committed
Added support for w (week) in duration.
1 parent c4dd092 commit a251494

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/main/java/org/gitlab4j/api/utils/DurationUtils.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
public class DurationUtils {
77

8-
private static char[] TIME_UNITS = { 'd', 'h', 'm', 's'};
9-
private static int[] TIME_UNIT_MULTIPLIERS = { 60 * 60 * 24, 60 * 60, 60, 1 };
8+
private static char[] TIME_UNITS = { 'w', 'd', 'h', 'm', 's'};
9+
private static int[] TIME_UNIT_MULTIPLIERS = { 60 * 60 * 24 * 7, 60 * 60 * 24, 60 * 60, 60, 1 };
1010

1111
private static Pattern durationPattern = Pattern.compile("(\\s*(\\d+)([a-z]))");
1212

@@ -18,14 +18,26 @@ public class DurationUtils {
1818
*/
1919
public static final String toString(int durationSeconds) {
2020

21-
int days = durationSeconds / TIME_UNIT_MULTIPLIERS[0];
22-
int seconds = durationSeconds - (days * TIME_UNIT_MULTIPLIERS[0]);
21+
int weeks = durationSeconds / TIME_UNIT_MULTIPLIERS[0];
22+
int days = (durationSeconds - weeks * TIME_UNIT_MULTIPLIERS[0]) / TIME_UNIT_MULTIPLIERS[1];
23+
int seconds = durationSeconds - (weeks * TIME_UNIT_MULTIPLIERS[0]) - (days * TIME_UNIT_MULTIPLIERS[1]);
2324
int hours = seconds / 3600;
2425
int minutes = (seconds % 3600) / 60;
2526
seconds = seconds % 60;
2627

2728
StringBuilder buf = new StringBuilder();
28-
if (days > 0) {
29+
if (weeks > 0) {
30+
31+
buf.append(weeks).append('w');
32+
if (seconds > 0) {
33+
buf.append(days).append('d').append(hours).append('h').append(minutes).append('m').append(seconds).append('s');
34+
} else if (minutes > 0) {
35+
buf.append(days).append('d').append(hours).append('h').append(minutes).append('m');
36+
} else if (hours > 0) {
37+
buf.append(days).append('d').append(hours).append('h');
38+
}
39+
40+
} else if (days > 0) {
2941

3042
buf.append(days).append('d');
3143
if (seconds > 0) {

0 commit comments

Comments
 (0)