mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Add time zone support for ZonedDateTime action inputs (#5235)
* Add time zone support for ZonedDateTime action inputs Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
This commit is contained in:
+20
-5
@@ -19,7 +19,10 @@ import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -177,8 +180,9 @@ public class ActionInputsHelper {
|
||||
.create(input.getName(), parameterType).withLabel(input.getLabel())
|
||||
.withDescription(input.getDescription()).withReadOnly(false)
|
||||
.withRequired(required || input.isRequired()).withContext(context);
|
||||
if (input.getDefaultValue() != null && !input.getDefaultValue().isEmpty()) {
|
||||
builder = builder.withDefault(input.getDefaultValue());
|
||||
String inputDefaultValue = input.getDefaultValue();
|
||||
if (inputDefaultValue != null && !inputDefaultValue.isEmpty()) {
|
||||
builder = builder.withDefault(inputDefaultValue);
|
||||
} else if (defaultValue != null) {
|
||||
builder = builder.withDefault(defaultValue);
|
||||
}
|
||||
@@ -291,9 +295,20 @@ public class ActionInputsHelper {
|
||||
case "java.util.Date" ->
|
||||
// Accepted format is: 2007-12-03T10:15:30
|
||||
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(valueString);
|
||||
case "java.time.ZonedDateTime" ->
|
||||
// Accepted format is: 2007-12-03T10:15:30
|
||||
LocalDateTime.parse(valueString).atZone(timeZoneProvider.getTimeZone());
|
||||
case "java.time.ZonedDateTime" -> {
|
||||
/*
|
||||
* Accepted format is one of:
|
||||
* - 2007-12-03T10:15:30
|
||||
* - 2007-12-03T10:15:30+01:00
|
||||
* - 2007-12-03T10:15:30+01:00[Europe/Paris]
|
||||
*/
|
||||
TemporalAccessor dt = DateTimeFormatter.ISO_DATE_TIME.parseBest(valueString,
|
||||
ZonedDateTime::from, LocalDateTime::from);
|
||||
if (dt instanceof ZonedDateTime zdt) {
|
||||
yield zdt;
|
||||
}
|
||||
yield ((LocalDateTime) dt).atZone(timeZoneProvider.getTimeZone());
|
||||
}
|
||||
case "java.time.Instant" ->
|
||||
// Accepted format is: 2007-12-03T10:15:30
|
||||
LocalDateTime.parse(valueString).atZone(timeZoneProvider.getTimeZone()).toInstant();
|
||||
|
||||
+23
-8
@@ -25,7 +25,9 @@ import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@@ -398,7 +400,7 @@ public class ActionInputHelperTest {
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, val), is(val));
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, valAsString), is(val));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replaceAll("-", " ")));
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replace("-", " ")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -409,7 +411,7 @@ public class ActionInputHelperTest {
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, val), is(val));
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, valAsString), is(val));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replaceAll(":", " ")));
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replace(":", " ")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -420,7 +422,7 @@ public class ActionInputHelperTest {
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, val), is(val));
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, valAsString), is(val));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replaceAll("T", " ")));
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replace("T", " ")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -430,8 +432,21 @@ public class ActionInputHelperTest {
|
||||
Input input = buildInput("java.time.ZonedDateTime");
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, val), is(val));
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, valAsString), is(val));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replaceAll("T", " ")));
|
||||
String s1 = valAsString.replace("T", " ");
|
||||
assertThrows(IllegalArgumentException.class, () -> helper.mapSerializedInputToActionInput(input, s1));
|
||||
|
||||
valAsString = "2007-12-03T10:15:30+04:00";
|
||||
val = ZonedDateTime.parse(valAsString, DateTimeFormatter.ISO_DATE_TIME);
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, val), is(val));
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, valAsString), is(val));
|
||||
assertThat(val.getOffset(), is(ZoneOffset.of("+04:00")));
|
||||
|
||||
// The offset intentionally mismatches with the specified zone, to verify that the zone parsing works
|
||||
valAsString = "2007-12-03T10:15:30+04:00[Europe/Kyiv]";
|
||||
val = ZonedDateTime.parse(valAsString, DateTimeFormatter.ISO_DATE_TIME);
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, val), is(val));
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, valAsString), is(val));
|
||||
assertThat(val.getOffset(), is(ZoneOffset.of("+02:00")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -448,7 +463,7 @@ public class ActionInputHelperTest {
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, val), is(val));
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, valAsString), is(val));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replaceAll("T", " ")));
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replace("T", " ")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -459,7 +474,7 @@ public class ActionInputHelperTest {
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, val), is(val));
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, valAsString), is(val));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replaceAll("T", " ")));
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replace("T", " ")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -470,7 +485,7 @@ public class ActionInputHelperTest {
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, val), is(val));
|
||||
assertThat(helper.mapSerializedInputToActionInput(input, valAsString), is(val));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replaceAll("T", " ")));
|
||||
() -> helper.mapSerializedInputToActionInput(input, valAsString.replace("T", " ")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user