]> git.basschouten.com Git - openhab-addons.git/blob
c9a6794c96f67df7c2b875d39e3f6a554f241dff
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mikrotik.internal.util;
14
15 import java.time.LocalDateTime;
16 import java.time.format.DateTimeFormatter;
17 import java.time.temporal.ChronoField;
18 import java.util.Locale;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24
25 /**
26  * The {@link Converter} is a utility class having functions to convert RouterOS-specific data representation strings
27  * to regular Java types.
28  *
29  * @author Oleg Vivtash - Initial contribution
30  */
31 @NonNullByDefault
32 public class Converter {
33     private static final DateTimeFormatter ROUTEROS_FORMAT = DateTimeFormatter.ofPattern("MMM/dd/yyyy kk:mm:ss",
34             Locale.ENGLISH);
35
36     private static final Pattern PERIOD_PATTERN = Pattern.compile("(\\d+)([a-z]+){1,3}");
37
38     public @Nullable static LocalDateTime fromRouterosTime(@Nullable String dateTimeString) {
39         if (dateTimeString == null) {
40             return null;
41         }
42         String fixedTs = dateTimeString.substring(0, 1).toUpperCase() + dateTimeString.substring(1);
43         return LocalDateTime.parse(fixedTs, ROUTEROS_FORMAT);
44     }
45
46     public @Nullable static LocalDateTime routerosPeriodBack(@Nullable String durationString) {
47         return routerosPeriodBack(durationString, LocalDateTime.now());
48     }
49
50     public @Nullable static LocalDateTime routerosPeriodBack(@Nullable String durationString,
51             LocalDateTime fromDateTime) {
52         if (durationString == null) {
53             return null;
54         }
55
56         Matcher m = PERIOD_PATTERN.matcher(durationString);
57         LocalDateTime ts = fromDateTime;
58         while (m.find()) {
59             int amount = Integer.parseInt(m.group(1));
60             String periodKey = m.group(2);
61             switch (periodKey) {
62                 case "y":
63                     ts = ts.minusYears(amount);
64                     break;
65                 case "w":
66                     ts = ts.minusWeeks(amount);
67                     break;
68                 case "d":
69                     ts = ts.minusDays(amount);
70                     break;
71                 case "h":
72                     ts = ts.minusHours(amount);
73                     break;
74                 case "m":
75                     ts = ts.minusMinutes(amount);
76                     break;
77                 case "s":
78                     ts = ts.minusSeconds(amount);
79                     break;
80                 case "ms":
81                     ts = ts.plus(amount, ChronoField.MILLI_OF_SECOND.getBaseUnit());
82                     break;
83                 default:
84                     throw new IllegalArgumentException(
85                             String.format("Unable to parse duration %s - %s is unknown", durationString, periodKey));
86             }
87         }
88         return ts;
89     }
90 }