2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.generic.values;
15 import java.time.format.DateTimeFormatter;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.library.CoreItemFactory;
21 import org.openhab.core.library.types.DateTimeType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.types.Command;
26 * Implements a datetime value.
28 * @author David Graeff - Initial contribution
31 public class DateTimeValue extends Value {
32 public DateTimeValue() {
33 super(CoreItemFactory.DATETIME, List.of(DateTimeType.class, StringType.class));
37 public DateTimeType parseCommand(Command command) throws IllegalArgumentException {
38 if (command instanceof DateTimeType dateTimeCommand) {
39 return dateTimeCommand;
41 return DateTimeType.valueOf(command.toString());
46 public String getMQTTpublishValue(Command command, @Nullable String pattern) {
47 String formatPattern = pattern;
48 if (formatPattern == null || "%s".contentEquals(formatPattern)) {
49 return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(((DateTimeType) command).getZonedDateTime());
51 return String.format(formatPattern, ((DateTimeType) command).getZonedDateTime());