Skip to content

Commit

Permalink
[microtik] Fix changed date format in firmware v7.10 (openhab#15362)
Browse files Browse the repository at this point in the history
Signed-off-by: lsiepel <leosiepel@gmail.com>
  • Loading branch information
lsiepel authored Aug 6, 2023
1 parent faceef6 commit 3b9b023
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.util.Locale;
import java.util.regex.Matcher;
Expand All @@ -32,15 +33,26 @@
public class Converter {
private static final DateTimeFormatter ROUTEROS_FORMAT = DateTimeFormatter.ofPattern("MMM/dd/yyyy kk:mm:ss",
Locale.ENGLISH);
private static final DateTimeFormatter ROUTEROS_FORMAT_NEW = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss",
Locale.ENGLISH);

private static final Pattern PERIOD_PATTERN = Pattern.compile("(\\d+)([a-z]+){1,3}");

public @Nullable static LocalDateTime fromRouterosTime(@Nullable String dateTimeString) {
if (dateTimeString == null) {
if (dateTimeString == null || dateTimeString.length() < 19) {
return null;
}
try {
// As of Firmware 7.10 the date format has changed to "yyyy-MM-dd HH:mm:ss"
if (dateTimeString.length() == 19) {
return LocalDateTime.parse(dateTimeString, ROUTEROS_FORMAT_NEW);
} else {
String fixedTs = dateTimeString.substring(0, 1).toUpperCase() + dateTimeString.substring(1);
return LocalDateTime.parse(fixedTs, ROUTEROS_FORMAT);
}
} catch (DateTimeParseException e) {
return null;
}
String fixedTs = dateTimeString.substring(0, 1).toUpperCase() + dateTimeString.substring(1);
return LocalDateTime.parse(fixedTs, ROUTEROS_FORMAT);
}

public @Nullable static LocalDateTime routerosPeriodBack(@Nullable String durationString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public void testFromRouterosTime() {
is(equalTo(LocalDateTime.of(2021, 1, 7, 9, 14, 11, 0))));
assertThat(Converter.fromRouterosTime("feb/13/2021 23:59:59"),
is(equalTo(LocalDateTime.of(2021, 2, 13, 23, 59, 59, 0))));

assertThat(Converter.fromRouterosTime("2021-02-13 23:59:59"),
is(equalTo(LocalDateTime.of(2021, 2, 13, 23, 59, 59, 0))));
assertThat(Converter.fromRouterosTime("2020-12-11 20:45:40"),
is(equalTo(LocalDateTime.of(2020, 12, 11, 20, 45, 40, 0))));
assertThat(Converter.fromRouterosTime("2021-01-07 09:14:11"),
is(equalTo(LocalDateTime.of(2021, 1, 7, 9, 14, 11, 0))));

assertNull(Converter.fromRouterosTime(null));
assertNull(Converter.fromRouterosTime(""));
assertNull(Converter.fromRouterosTime("2021-18-07 09:14:11"));
assertNull(Converter.fromRouterosTime("feb/41/2021 23:59:59"));
}

@Test
Expand Down

0 comments on commit 3b9b023

Please sign in to comment.