-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTimeFormatter.java
103 lines (88 loc) · 3.57 KB
/
TimeFormatter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main.java.kyu4;
/**
* 4 kyu - Human readable duration format
*
* https://www.codewars.com/kata/52742f58faf5485cae000b9a/train/java
*
* Details:
*
* our task in order to complete this Kata is to write a function which formats a duration, given
* as a number of seconds, in a human-friendly way.
*
* The function must accept a non-negative integer. If it is zero, it just returns "now".
* Otherwise, the duration is expressed as a combination of years, days, hours, minutes and
* seconds.
*
* For the purpose of this Kata, a year is 365 days and a day is 24 hours.
*
* Note that spaces are important.
* Detailed rules
*
* The resulting expression is made of components like 4 seconds, 1 year, etc. In general, a
* positive integer and one of the valid units of time, separated by a space. The unit of time is
* used in plural if the integer is greater than 1.
*
* The components are separated by a comma and a space (", "). Except the last component, which is
* separated by " and ", just like it would be written in English.
*
* A more significant units of time will occur before than a least significant one. Therefore, 1
* second and 1 year is not correct, but 1 year and 1 second is.
*
* Different components have different unit of times. So there is not repeated units like in 5
* seconds and 1 second.
*
* A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0
* seconds is not valid, but it should be just 1 minute.
*
* A unit of time must be used "as much as possible". It means that the function should not return
* 61 seconds, but 1 minute and 1 second instead. Formally, the duration specified by of a
* component must not be greater than any valid more significant unit of time.
*/
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TimeFormatter {
static class TimeUnit {
private String name;
private int value;
TimeUnit(String name, int value) {
this.name = name;
this.value = value;
}
}
public static String formatDuration(int seconds) {
if (seconds <= 0) {
return "now";
}
List<TimeUnit> time = new ArrayList<>();
time.add(new TimeUnit("year", (seconds / (60 * 60 * 24 * 365))));
time.add(new TimeUnit("day", (seconds / (60 * 60 * 24)) % 365));
time.add(new TimeUnit("hour", (seconds / (60 * 60)) % 24));
time.add(new TimeUnit("minute", (seconds / 60) % 60));
time.add(new TimeUnit("second", seconds % 60));
time.removeIf(timeUnit -> timeUnit.value == 0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < time.size(); i++) {
final TimeUnit unit = time.get(i);
sb.append(unit.value)
.append(" ")
.append(unit.name);
if (unit.value > 1) {
sb.append("s");
} else if (i < time.size() - 2) {
sb.append(", ");
} else if (i == time.size() - 2) {
sb.append(" and ");
}
}
return new String(sb);
}
@Test
public void exampleTests() {
assertEquals("1 second", TimeFormatter.formatDuration(1));
assertEquals("1 minute and 2 seconds", TimeFormatter.formatDuration(62));
assertEquals("2 minutes", TimeFormatter.formatDuration(120));
assertEquals("1 hour", TimeFormatter.formatDuration(3600));
assertEquals("1 hour, 1 minute and 2 seconds", TimeFormatter.formatDuration(3662));
}
}