Package com.enhancedechest.util
Class DurationFormat
java.lang.Object
com.enhancedechest.util.DurationFormat
Parses and formats human-friendly durations.
Accepted input units: s (second), m (minute), h (hour),
d (day), w (week), mo (month) and y (year). A month is
approximated as 30 days and a year as 365 days. Simple values look like 20s,
5m or 1h; complex values join several components with underscores, e.g.
1d_2h_30m_15s.
All methods are pure and side-effect free, which keeps them unit-testable.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordOne unit component of a formatted duration: a wholecountof the unit named byunit(one ofyear month week day hour minute second). -
Method Summary
Modifier and TypeMethodDescriptionstatic StringformatRemaining(long millis) Compact, non-localized form ofremainingParts(long)("1d 4h","0s") for logs and the startup banner.static longParses a duration string into milliseconds.static List<DurationFormat.Part> remainingParts(long millis) Breaks a remaining duration into the two most significant non-zero units, e.g.
-
Method Details
-
parse
Parses a duration string into milliseconds.Examples:
20s,5m,1h,1d_2h_30m_15s.- Parameters:
input- the duration string (case-insensitive, components separated by_)- Returns:
- the duration in milliseconds (always positive)
- Throws:
IllegalArgumentException- if the string is null, empty or malformed
-
remainingParts
Breaks a remaining duration into the two most significant non-zero units, e.g.[1d, 4h]or[23h, 45m], as locale-freeDurationFormat.Parts the caller renders into localized text. Rather than baking unit suffixes into a string (which could not be localized), this returns only the numbers and unit names.Edge cases mirror the old compact formatter: a zero or negative duration yields a single
0 secondpart, and a sub-second positive value rounds up to a single1 secondpart — so the result is never empty.- Parameters:
millis- the remaining time in milliseconds- Returns:
- an immutable list of one or two parts, most significant first
-
formatRemaining
Compact, non-localized form ofremainingParts(long)("1d 4h","0s") for logs and the startup banner. Player-facing text must localize the parts instead (seeLanguageManager#duration), since these suffixes are English.- Parameters:
millis- the remaining time in milliseconds- Returns:
- a short label;
"0s"when the duration is zero or negative
-