-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeUtil.java
More file actions
365 lines (329 loc) · 12.3 KB
/
TimeUtil.java
File metadata and controls
365 lines (329 loc) · 12.3 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package games.negative.engine.util;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Utility class for formatting and parsing time durations in human-readable formats.
*/
public final class TimeUtil {
private static final Pattern COLON_FORMAT_PATTERN = Pattern.compile("^\\d+(:\\d{2})*$");
private TimeUtil() {
throw new UnsupportedOperationException("Utility class cannot be instantiated");
}
/**
* Formats a duration into a human-readable string.
*
* @param duration The duration to format
* @param compact Whether to use compact format (e.g., "1d" vs "1 day")
* @return The formatted duration string, or "0s" if duration is zero
* @throws NullPointerException if duration is null
*/
public static String format(Duration duration, boolean compact) {
long days = duration.toDays();
long hours = duration.toHoursPart();
long minutes = duration.toMinutesPart();
long seconds = duration.toSecondsPart();
List<String> parts = new ArrayList<>(4);
if (days > 0) {
parts.add(formatUnit(days, "d", "day", "days", compact));
}
if (hours > 0) {
parts.add(formatUnit(hours, "h", "hour", "hours", compact));
}
if (minutes > 0) {
parts.add(formatUnit(minutes, "m", "minute", "minutes", compact));
}
if (seconds > 0) {
parts.add(formatUnit(seconds, "s", "second", "seconds", compact));
}
if (parts.isEmpty()) {
return compact ? "0s" : "0 seconds";
}
return String.join(" ", parts);
}
/**
* Formats a duration into a human-readable string using full format.
*
* @param duration The duration to format
* @return The formatted duration string
* @throws NullPointerException if duration is null
*/
public static String format(Duration duration) {
return format(duration, false);
}
/**
* Formats a duration into colon-separated time format.
* <p>
* Examples:
* <ul>
* <li>5 seconds → "0:05"</li>
* <li>3 hours 5 minutes → "3:05:00"</li>
* <li>1 day 3 hours 5 minutes → "1:03:05:00"</li>
* <li>2 days 0 hours 0 minutes 30 seconds → "2:00:00:30"</li>
* </ul>
*
* @param duration The duration to format
* @return The formatted duration in colon-separated format
* @throws NullPointerException if duration is null
*/
public static String formatColonSeparated(Duration duration) {
long days = duration.toDays();
long hours = duration.toHoursPart();
long minutes = duration.toMinutesPart();
long seconds = duration.toSecondsPart();
if (days > 0) {
return String.format("%d:%02d:%02d:%02d", days, hours, minutes, seconds);
} else if (hours > 0) {
return String.format("%d:%02d:%02d", hours, minutes, seconds);
} else if (minutes > 0) {
return String.format("%d:%02d", minutes, seconds);
} else {
return String.format("0:%02d", seconds);
}
}
/**
* Formats a time in milliseconds into a human-readable string.
*
* @param millis Time in milliseconds to format
* @param compact Whether to use compact format
* @return Formatted time string
* @throws IllegalArgumentException if millis is negative
* @deprecated Use {@link #format(Duration, boolean)} instead
*/
@Deprecated
public static String format(long millis, boolean compact) {
return format(Duration.ofMillis(millis), compact);
}
/**
* Formats a time in milliseconds into a human-readable string.
*
* @param millis Time in milliseconds to format
* @return Formatted time string
* @throws IllegalArgumentException if millis is negative
* @deprecated Use {@link #format(Duration)} instead
*/
@Deprecated
public static String format(long millis) {
return format(millis, false);
}
/**
* Parses a duration string into a Duration object.
* <p>
* Supports two formats:
* <ul>
* <li>Unit format: "1d2h30m" (units: w, d, h, m, s)</li>
* <li>Colon format: "1:03:05:00" (days:hours:minutes:seconds)</li>
* </ul>
*
* @param input The duration string to parse
* @return The parsed Duration object
* @throws NullPointerException if input is null
* @throws IllegalArgumentException if input is empty or has invalid format
*/
public static Duration parse(String input) {
String trimmed = input.trim();
// Check if it's colon-separated format
if (trimmed.contains(":")) {
return parseColonSeparated(trimmed);
} else {
return parseUnitFormat(trimmed);
}
}
/**
* Parses a colon-separated duration string (e.g., "1:03:05:00").
* <p>
* Format can be:
* <ul>
* <li>M:SS (minutes:seconds)</li>
* <li>H:MM:SS (hours:minutes:seconds)</li>
* <li>D:HH:MM:SS (days:hours:minutes:seconds)</li>
* </ul>
*
* @param input The colon-separated duration string
* @return The parsed Duration object
* @throws IllegalArgumentException if input has invalid format
*/
public static Duration parseColonSeparated(String input) {
String trimmed = input.trim();
if (!COLON_FORMAT_PATTERN.matcher(trimmed).matches()) {
throw new IllegalArgumentException(
"Invalid colon format: '" + input + "'. " +
"Expected format: D:HH:MM:SS, H:MM:SS, or M:SS"
);
}
String[] parts = trimmed.split(":");
if (parts.length < 2 || parts.length > 4) {
throw new IllegalArgumentException(
"Invalid colon format: '" + input + "'. " +
"Must have 2-4 components (M:SS, H:MM:SS, or D:HH:MM:SS)"
);
}
try {
long days = 0;
long hours = 0;
long minutes;
long seconds;
if (parts.length == 4) {
// D:HH:MM:SS
days = Long.parseLong(parts[0]);
hours = Long.parseLong(parts[1]);
minutes = Long.parseLong(parts[2]);
seconds = Long.parseLong(parts[3]);
} else if (parts.length == 3) {
// H:MM:SS
hours = Long.parseLong(parts[0]);
minutes = Long.parseLong(parts[1]);
seconds = Long.parseLong(parts[2]);
} else {
// M:SS
minutes = Long.parseLong(parts[0]);
seconds = Long.parseLong(parts[1]);
}
// Validate ranges
if (hours < 0) {
throw new IllegalArgumentException(
"Hours cannot be negative, got: " + hours
);
}
if (parts.length == 4 && hours > 23) {
throw new IllegalArgumentException(
"Hours must be between 0 and 23 in D:HH:MM:SS format, got: " + hours
);
}
if (minutes < 0 || minutes > 59) {
throw new IllegalArgumentException(
"Minutes must be between 0 and 59, got: " + minutes
);
}
if (seconds < 0 || seconds > 59) {
throw new IllegalArgumentException(
"Seconds must be between 0 and 59, got: " + seconds
);
}
if (days < 0) {
throw new IllegalArgumentException(
"Days cannot be negative, got: " + days
);
}
return Duration.ofDays(days)
.plusHours(hours)
.plusMinutes(minutes)
.plusSeconds(seconds);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"Invalid number in colon format: '" + input + "'", e
);
}
}
/**
* Parses a unit-based duration string (e.g., "1d2h30m").
*
* @param input The unit-based duration string
* @return The parsed Duration object
* @throws IllegalArgumentException if input has invalid format
*/
private static Duration parseUnitFormat(String input) {
StringBuilder numberBuffer = new StringBuilder();
Duration duration = Duration.ZERO;
for (char c : input.toCharArray()) {
if (Character.isDigit(c)) {
numberBuffer.append(c);
} else if (Character.isWhitespace(c)) {
// Allow whitespace.
} else {
if (numberBuffer.isEmpty()) {
throw new IllegalArgumentException(
"Invalid duration format: unit '" + c +
"' without preceding number"
);
}
long value = Long.parseLong(numberBuffer.toString());
duration = addUnit(duration, value, c);
numberBuffer.setLength(0);
}
}
if (!numberBuffer.isEmpty()) {
throw new IllegalArgumentException(
"Invalid duration format: number without unit at end"
);
}
return duration;
}
/**
* Converts a Duration object into a compact string format (e.g., "1d2h30m").
*
* @param duration The Duration object to convert
* @return A compact string representation of the duration
* @throws NullPointerException if duration is null
*/
public static String toCompactString(Duration duration) {
long days = duration.toDays();
long hours = duration.toHoursPart();
long minutes = duration.toMinutesPart();
long seconds = duration.toSecondsPart();
StringBuilder builder = new StringBuilder();
if (days > 0) builder.append(days).append('d');
if (hours > 0) builder.append(hours).append('h');
if (minutes > 0) builder.append(minutes).append('m');
if (seconds > 0) builder.append(seconds).append('s');
return !builder.isEmpty() ? builder.toString() : "0s";
}
/**
* Parses a duration string into milliseconds.
*
* @param input The duration string to parse
* @return Duration in milliseconds
* @throws NullPointerException if input is null
* @throws IllegalArgumentException if input is empty or has invalid format
* @deprecated Use {@link #parse(String)} instead
*/
@Deprecated
public static long fromString(String input) {
return parse(input).toMillis();
}
/**
* Formats a single time unit.
*
* @param value The numeric value
* @param compact The compact suffix (e.g., "d")
* @param singular The singular form (e.g., "day")
* @param plural The plural form (e.g., "days")
* @param useCompact Whether to use compact format
* @return The formatted unit string
*/
private static String formatUnit(
long value,
String compact,
String singular,
String plural,
boolean useCompact
) {
if (useCompact) {
return value + compact;
}
return value + " " + (value == 1 ? singular : plural);
}
/**
* Adds a time unit to a duration.
*
* @param duration The current duration
* @param value The value to add
* @param unit The unit character
* @return The updated duration
* @throws IllegalArgumentException if unit is not recognized
*/
private static Duration addUnit(Duration duration, long value, char unit) {
return switch (unit) {
case 's', 'S' -> duration.plusSeconds(value);
case 'm', 'M' -> duration.plusMinutes(value);
case 'h', 'H' -> duration.plusHours(value);
case 'd', 'D' -> duration.plusDays(value);
case 'w', 'W' -> duration.plusDays(value * 7);
default -> throw new IllegalArgumentException(
"Invalid duration unit: '" + unit + "'. " +
"Supported units: s, m, h, d, w"
);
};
}
}