|
27 | 27 | @NonNullByDefault |
28 | 28 | public class DurationUtils { |
29 | 29 |
|
30 | | - private static final Pattern DURATION_PATTERN = Pattern.compile( |
31 | | - "(?:([0-9]+)D)?\\s*(?:([0-9]+)H)?\\s*(?:([0-9]+)M)?\\s*(?:([0-9]+)S)?\\s*(?:([0-9]+)MS)?", |
32 | | - Pattern.CASE_INSENSITIVE); |
| 30 | + private static final Pattern DURATION_PATTERN = Pattern.compile(""" |
| 31 | + (?:([0-9]+)\\s*(?:d|days?))? |
| 32 | + \\s* |
| 33 | + (?:([0-9]+)\\s*(?:h|hrs?|hours?))? |
| 34 | + \\s* |
| 35 | + (?:([0-9]+)\\s*(?:m|mins?|minutes?))? |
| 36 | + \\s* |
| 37 | + (?:([0-9]+)\\s*(?:s|secs?|seconds?))? |
| 38 | + \\s* |
| 39 | + (?:([0-9]+)\\s*(?:ms|milliseconds?))? |
| 40 | + """, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS); |
| 41 | + |
33 | 42 | private static final ChronoUnit[] DURATION_UNITS = { ChronoUnit.DAYS, ChronoUnit.HOURS, ChronoUnit.MINUTES, |
34 | 43 | ChronoUnit.SECONDS, ChronoUnit.MILLIS }; |
35 | 44 |
|
36 | 45 | /** |
37 | | - * Parses a duration string in ISO-8601 format or a custom format like |
38 | | - * "1d 1h 15m 30s 500ms" where |
39 | | - * 'd' stands for days, |
40 | | - * 'h' for hours, |
41 | | - * 'm' for minutes, |
42 | | - * 's' for seconds, and |
43 | | - * 'ms' for milliseconds. |
| 46 | + * Parses a duration string in ISO-8601 duration format or a custom format like |
| 47 | + * "1d 1h 15m 30s 500ms". |
| 48 | + * |
| 49 | + * When specifying a duration, the units must be specified in the order of |
| 50 | + * days, hours, minutes, seconds, and milliseconds, |
| 51 | + * although any individual unit may be omitted. |
| 52 | + * Each unit must be preceded by an integer value. |
| 53 | + * A space between the number and its corresponding unit is permitted but not required. |
| 54 | + * Likewise, whitespace between unit groups is optional. |
| 55 | + * |
| 56 | + * The units supported in the duration format are: |
| 57 | + * <ul> |
| 58 | + * <li>'d|day|days' for days, |
| 59 | + * <li>'h|hr|hrs|hour|hours' for hours, |
| 60 | + * <li>'m|min|mins|minute|minutes' for minutes, |
| 61 | + * <li>'s|sec|secs|second|seconds' for seconds, and |
| 62 | + * <li>'ms|millisecond|milliseconds' for milliseconds. |
| 63 | + * </ul> |
| 64 | + * |
| 65 | + * Examples of valid duration strings: |
| 66 | + * <ul> |
| 67 | + * <li>"1h" represents 1 hour |
| 68 | + * <li>"15m" represents 15 minutes |
| 69 | + * <li>"1h15m" represents 1 day and 15 minutes. It can also be written as "1h 15m", "1 h 15 m", "1 hr 15 mins", |
| 70 | + * "1hour 15 minutes", etc. |
| 71 | + * <li>"1d 1h 30s" represents 1 day, 1 hour, and 30 seconds |
| 72 | + * </ul> |
| 73 | + * |
| 74 | + * The ISO-8601 duration format is supported, but only the following units are recognized: |
| 75 | + * days, hours, minutes, and seconds. |
| 76 | + * Units such as years, months, and weeks are not supported. |
| 77 | + * The number of days, hours and minutes must parse to a long. |
| 78 | + * The number of seconds must parse to a long with optional fraction. |
| 79 | + * The decimal point may be either a dot or a comma. |
| 80 | + * The fractional part may have from zero to 9 digits. |
| 81 | + * |
| 82 | + * Examples of ISO-8601 durations: |
| 83 | + * <ul> |
| 84 | + * <li>"PT1H30M" represents 1 hour and 30 minutes |
| 85 | + * <li>"PT1D" represents 1 day |
| 86 | + * <li>"PT0.5S" represents 0.5 seconds (500 milliseconds) |
| 87 | + * </ul> |
44 | 88 | * |
45 | 89 | * @param durationString the string representation of the duration |
46 | 90 | * @return a Duration object representing the parsed duration |
|
0 commit comments