Simplify DateTimeType handling for MQTT

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-19 21:17:30 +01:00 committed by lsiepel
parent d001636a5f
commit cb74dd6497

View File

@ -12,6 +12,8 @@
*/ */
package org.openhab.binding.mqtt.generic.values; package org.openhab.binding.mqtt.generic.values;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
@ -45,11 +47,12 @@ public class DateTimeValue extends Value {
@Override @Override
public String getMQTTpublishValue(Command command, @Nullable String pattern) { public String getMQTTpublishValue(Command command, @Nullable String pattern) {
ZonedDateTime zonedDateTime = ((DateTimeType) command).getZonedDateTime(ZoneId.systemDefault());
String formatPattern = pattern; String formatPattern = pattern;
if (formatPattern == null || "%s".contentEquals(formatPattern)) { if (formatPattern == null || "%s".contentEquals(formatPattern)) {
return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(((DateTimeType) command).getZonedDateTime()); return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(zonedDateTime);
} }
return String.format(formatPattern, ((DateTimeType) command).getZonedDateTime()); return String.format(formatPattern, zonedDateTime);
} }
@Override @Override