diff options
author | Marko Zajc <marko@zajc.eu.org> | 2023-09-07 02:45:46 +0200 |
---|---|---|
committer | Marko Zajc <marko@zajc.eu.org> | 2023-09-07 02:45:46 +0200 |
commit | 53938f16bdb475f2e0552990661cdaae62d2b1a4 (patch) | |
tree | 909b81f63c4a50dd65ea6377351ae73d5d6a9eac | |
parent | 418780be8482f2902e49cfc53b7b80ca0161d122 (diff) |
[core] Support week in duration parsing
-rw-r--r-- | core/src/main/java/libot/utils/ParseUtils.java | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/core/src/main/java/libot/utils/ParseUtils.java b/core/src/main/java/libot/utils/ParseUtils.java index 83b3d1f..fcbe628 100644 --- a/core/src/main/java/libot/utils/ParseUtils.java +++ b/core/src/main/java/libot/utils/ParseUtils.java | |||
@@ -3,11 +3,12 @@ package libot.utils; | |||
3 | import static java.lang.String.format; | 3 | import static java.lang.String.format; |
4 | import static java.lang.System.currentTimeMillis; | 4 | import static java.lang.System.currentTimeMillis; |
5 | import static java.time.ZoneOffset.UTC; | 5 | import static java.time.ZoneOffset.UTC; |
6 | import static java.util.concurrent.TimeUnit.*; | 6 | import static java.time.temporal.ChronoUnit.*; |
7 | import static java.util.regex.Pattern.*; | 7 | import static java.util.regex.Pattern.*; |
8 | import static org.apache.commons.lang3.math.NumberUtils.isDigits; | 8 | import static org.apache.commons.lang3.math.NumberUtils.isDigits; |
9 | 9 | ||
10 | import java.time.LocalDateTime; | 10 | import java.time.LocalDateTime; |
11 | import java.time.temporal.ChronoUnit; | ||
11 | import java.util.Map; | 12 | import java.util.Map; |
12 | import java.util.concurrent.*; | 13 | import java.util.concurrent.*; |
13 | import java.util.regex.*; | 14 | import java.util.regex.*; |
@@ -124,7 +125,8 @@ public class ParseUtils { | |||
124 | long duration = 0; | 125 | long duration = 0; |
125 | for (char c : chars) { | 126 | for (char c : chars) { |
126 | boolean append = false; | 127 | boolean append = false; |
127 | TimeUnit unit = switch (c) { | 128 | ChronoUnit unit = switch (c) { |
129 | case 'w' -> WEEKS; | ||
128 | case 'd' -> DAYS; | 130 | case 'd' -> DAYS; |
129 | case 'h' -> HOURS; | 131 | case 'h' -> HOURS; |
130 | case 'm' -> MINUTES; | 132 | case 'm' -> MINUTES; |
@@ -141,13 +143,14 @@ public class ParseUtils { | |||
141 | continue; | 143 | continue; |
142 | if (unit == null || durationBuffer.length() == 0) // not a number and not a unit | 144 | if (unit == null || durationBuffer.length() == 0) // not a number and not a unit |
143 | return -1; | 145 | return -1; |
144 | duration += unit.toMillis(Long.parseLong(durationBuffer.toString())); | 146 | |
147 | duration += unit.getDuration().toMillis() * Long.parseLong(durationBuffer.toString()); | ||
145 | durationBuffer.setLength(0); | 148 | durationBuffer.setLength(0); |
146 | } | 149 | } |
147 | 150 | ||
148 | if (durationBuffer.length() != 0) | 151 | if (durationBuffer.length() != 0) |
149 | duration += SECONDS.toMillis(Long.parseLong(durationBuffer.toString())); // any number without unit is | 152 | duration += TimeUnit.SECONDS.toMillis(Long.parseLong(durationBuffer.toString())); |
150 | // treated as seconds | 153 | // any number without unit is treated as seconds |
151 | 154 | ||
152 | return duration; | 155 | return duration; |
153 | } | 156 | } |