Apply TimeFormat for labels of Number:Time items (#1470)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
pull/1517/head
Christoph Weitkamp 2020-06-18 22:25:10 +02:00 committed by GitHub
parent 68405036f1
commit 6deb3255ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 3 deletions

View File

@ -15,6 +15,9 @@ package org.openhab.core.library.types;
import static org.eclipse.jdt.annotation.DefaultLocation.*;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.IllegalFormatConversionException;
@ -29,6 +32,7 @@ import javax.measure.quantity.Dimensionless;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.internal.library.unit.UnitInitializer;
import org.openhab.core.library.unit.MetricPrefix;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.types.Command;
import org.openhab.core.types.PrimitiveType;
@ -229,15 +233,30 @@ public class QuantityType<T extends Quantity<T>> extends Number
@Override
public String format(String pattern) {
boolean unitPlaceholder = pattern.contains(UnitUtils.UNIT_PLACEHOLDER);
final String formatPattern;
if (pattern.contains(UnitUtils.UNIT_PLACEHOLDER)) {
String unitSymbol = SmartHomeUnits.PERCENT.equals(getUnit()) ? "%%" : getUnit().toString();
if (unitPlaceholder) {
String unitSymbol = getUnit().equals(SmartHomeUnits.PERCENT) ? "%%" : getUnit().toString();
formatPattern = pattern.replace(UnitUtils.UNIT_PLACEHOLDER, unitSymbol);
} else {
formatPattern = pattern;
}
// The dimension could be a time value thus we want to support patterns to format datetime
if (quantity.getUnit().isCompatible(SmartHomeUnits.SECOND) && !unitPlaceholder) {
QuantityType<T> millis = toUnit(MetricPrefix.MILLI(SmartHomeUnits.SECOND));
if (millis != null) {
try {
return String.format(formatPattern,
ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis.longValue()), ZoneId.systemDefault()));
} catch (IllegalFormatConversionException ifce) {
// The conversion is not valid for the type ZonedDateTime. This happens, if the format is like
// "%.1f". Fall through to default behavior.
}
}
}
// The value could be an integer value. Try to convert to BigInteger in
// order to have access to more conversion formats.
BigDecimal bd = toBigDecimal();

View File

@ -17,6 +17,7 @@ import static org.junit.Assert.*;
import static org.openhab.core.library.unit.MetricPrefix.CENTI;
import java.math.BigDecimal;
import java.text.DecimalFormatSymbols;
import javax.measure.quantity.Dimensionless;
import javax.measure.quantity.Energy;
@ -24,6 +25,7 @@ import javax.measure.quantity.Length;
import javax.measure.quantity.Pressure;
import javax.measure.quantity.Speed;
import javax.measure.quantity.Temperature;
import javax.measure.quantity.Time;
import org.junit.Test;
import org.openhab.core.library.dimension.DataAmount;
@ -33,6 +35,7 @@ import org.openhab.core.library.dimension.Intensity;
import org.openhab.core.library.unit.MetricPrefix;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.types.util.UnitUtils;
import tec.uom.se.quantity.QuantityDimension;
import tec.uom.se.unit.Units;
@ -40,9 +43,12 @@ import tec.uom.se.unit.Units;
/**
* @author Gaël L'hopital - Initial contribution
*/
@SuppressWarnings({ "rawtypes", "unchecked", "null" })
@SuppressWarnings("null")
public class QuantityTypeTest {
// we need to get the decimal separator of the default locale for our tests
private static final char SEP = new DecimalFormatSymbols().getDecimalSeparator();
@Test
public void testDimensionless() {
// Dimensionless value that works
@ -80,6 +86,7 @@ public class QuantityTypeTest {
QuantityType.class.newInstance();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testUnits() {
QuantityType<Length> dt2 = new QuantityType<>("2 m");
@ -112,6 +119,23 @@ public class QuantityTypeTest {
}
}
@Test
public void testFormats() {
QuantityType<Time> seconds = new QuantityType<>(80, SmartHomeUnits.SECOND);
QuantityType<Time> millis = seconds.toUnit(MetricPrefix.MILLI(SmartHomeUnits.SECOND));
QuantityType<Time> minutes = seconds.toUnit(SmartHomeUnits.MINUTE);
assertThat(seconds.format("%.1f " + UnitUtils.UNIT_PLACEHOLDER), is("80" + SEP + "0 s"));
assertThat(millis.format("%.1f " + UnitUtils.UNIT_PLACEHOLDER), is("80000" + SEP + "0 ms"));
assertThat(minutes.format("%.1f " + UnitUtils.UNIT_PLACEHOLDER), is("1" + SEP + "3 min"));
assertThat(seconds.format("%.1f"), is("80" + SEP + "0"));
assertThat(minutes.format("%.1f"), is("1" + SEP + "3"));
assertThat(seconds.format("%1$tH:%1$tM:%1$tS"), is("00:01:20"));
assertThat(millis.format("%1$tHh %1$tMm %1$tSs"), is("00h 01m 20s"));
}
@Test
public void testConverters() {
QuantityType<?> dt2 = QuantityType.valueOf("2 m");