Skip to content

Commit b863c78

Browse files
committed
Added support for month (mo) durations (#104).
1 parent 65f3cbe commit b863c78

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
public class DurationUtils {
77

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 };
10-
11-
private static Pattern durationPattern = Pattern.compile("(\\s*(\\d+)([a-z]))");
8+
private static final String[] TIME_UNITS = { "mo", "w", "d", "h", "m", "s" };
9+
private static final int[] TIME_UNIT_MULTIPLIERS = { 60 * 60 * 24 * 30, 60 * 60 * 24 * 7, 60 * 60 * 24, 60 * 60, 60, 1 };
10+
private static Pattern durationPattern = Pattern.compile("(\\s*(\\d+)(mo|[wdhms]))");
1211

1312
/**
1413
* Create a human readable duration string from seconds.
@@ -18,15 +17,33 @@ public class DurationUtils {
1817
*/
1918
public static final String toString(int durationSeconds) {
2019

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]);
20+
int months = durationSeconds / TIME_UNIT_MULTIPLIERS[0];
21+
int weeks = (durationSeconds - months * TIME_UNIT_MULTIPLIERS[0]) / TIME_UNIT_MULTIPLIERS[1];
22+
int days = (durationSeconds - months * TIME_UNIT_MULTIPLIERS[0] - weeks * TIME_UNIT_MULTIPLIERS[1]) / TIME_UNIT_MULTIPLIERS[2];
23+
int seconds = durationSeconds - (months * TIME_UNIT_MULTIPLIERS[0]) - (weeks * TIME_UNIT_MULTIPLIERS[1]) - (days * TIME_UNIT_MULTIPLIERS[2]);
2424
int hours = seconds / 3600;
2525
int minutes = (seconds % 3600) / 60;
2626
seconds = seconds % 60;
2727

2828
StringBuilder buf = new StringBuilder();
29-
if (weeks > 0) {
29+
if (months > 0) {
30+
31+
buf.append(months).append("mo");
32+
if (weeks > 0) {
33+
buf.append(weeks).append('w');
34+
}
35+
36+
if (seconds > 0) {
37+
buf.append(days).append('d').append(hours).append('h').append(minutes).append('m').append(seconds).append('s');
38+
} else if (minutes > 0) {
39+
buf.append(days).append('d').append(hours).append('h').append(minutes).append('m');
40+
} else if (hours > 0) {
41+
buf.append(days).append('d').append(hours).append('h');
42+
} else if (days > 0) {
43+
buf.append(days).append('d');
44+
}
45+
46+
} else if (weeks > 0) {
3047

3148
buf.append(weeks).append('w');
3249
if (seconds > 0) {
@@ -93,7 +110,7 @@ public static final int parse(String durationString) {
93110
int numGroups = matcher.groupCount();
94111
if (numGroups == 3) {
95112

96-
char unit = matcher.group(3).charAt(0);
113+
String unit = matcher.group(3);
97114
int nextUnitIndex = getUnitIndex(unit);
98115
if (nextUnitIndex > currentUnitIndex) {
99116

@@ -119,10 +136,10 @@ public static final int parse(String durationString) {
119136
return (seconds);
120137
}
121138

122-
private static final int getUnitIndex(char unit) {
139+
private static final int getUnitIndex(String unit) {
123140

124141
for (int i = 0; i < TIME_UNITS.length; i++) {
125-
if (unit == TIME_UNITS[i])
142+
if (unit.equals(TIME_UNITS[i]))
126143
return (i);
127144
}
128145

src/test/java/org/gitlab4j/api/TestDuration.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ public class TestDuration {
1111
@Test
1212
public void testParse() {
1313

14-
int seconds = DurationUtils.parse("1w1d1h1m1s");
14+
int seconds = DurationUtils.parse("7mo1w1d1h1m1s");
15+
assertEquals(60 * 60 * 24 * 30 * 7 + 60 * 60 * 24 * 7 + 60 * 60 * 24 + 60 * 60 + 60 + 1, seconds);
16+
17+
seconds = DurationUtils.parse("1w1d1h1m1s");
1518
assertEquals(60 * 60 * 24 * 7 + 60 * 60 * 24 + 60 * 60 + 60 + 1, seconds);
1619

1720
seconds = DurationUtils.parse("1d1h1m1s");
1821
assertEquals(60 * 60 * 24 + 60 * 60 + 60 + 1, seconds);
1922

2023
seconds = DurationUtils.parse("60m");
2124
assertEquals(60 * 60, seconds);
22-
25+
2326
seconds = DurationUtils.parse("1h");
2427
assertEquals(60 * 60, seconds);
2528
}
@@ -77,5 +80,8 @@ public void testToString() {
7780

7881
duration = DurationUtils.toString(60 * 60 * 24 * 7 + 60 * 60 * 24 * 2 + 60 * 60 * 3 + 60 * 4 + 5);
7982
assertEquals("1w2d3h4m5s", duration);
83+
84+
duration = DurationUtils.toString(60 * 60 * 24 * 30 * 3 + 60 * 60 * 24 * 2 + 60 * 60 * 3 + 60 * 6 + 8);
85+
assertEquals("3mo2d3h6m8s", duration);
8086
}
8187
}

0 commit comments

Comments
 (0)