mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-25 11:45:49 +01:00
* Issue 1157 Enhance DecimalType to accept epoch seconds and milliseconds. Signed-off-by: Gaël L'hopital <gael@lhopital.org>
This commit is contained in:
parent
492526d74f
commit
694ff49954
@ -38,7 +38,7 @@ public class DateTimeItem extends GenericItem {
|
||||
private static List<Class<? extends Command>> acceptedCommandTypes = new ArrayList<>();
|
||||
|
||||
static {
|
||||
acceptedDataTypes.add((DateTimeType.class));
|
||||
acceptedDataTypes.add(DateTimeType.class);
|
||||
acceptedDataTypes.add(UnDefType.class);
|
||||
|
||||
acceptedCommandTypes.add(RefreshType.class);
|
||||
@ -66,7 +66,13 @@ public class DateTimeItem extends GenericItem {
|
||||
@Override
|
||||
public void setState(State state) {
|
||||
if (isAcceptedState(acceptedDataTypes, state)) {
|
||||
super.setState(state);
|
||||
// try conversion
|
||||
State convertedState = state.as(DateTimeType.class);
|
||||
if (convertedState != null) {
|
||||
applyState(convertedState);
|
||||
} else {
|
||||
applyState(state);
|
||||
}
|
||||
} else {
|
||||
logSetTypeError(state);
|
||||
}
|
||||
|
@ -13,8 +13,10 @@
|
||||
package org.eclipse.smarthome.core.library.types;
|
||||
|
||||
import java.time.DateTimeException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
@ -37,6 +39,7 @@ import org.eclipse.smarthome.core.types.State;
|
||||
* @author Jan N. Klug - add ability to use time or date only
|
||||
* @author Wouter Born - increase parsing and formatting precision
|
||||
* @author Laurent Garnier - added methods toLocaleZone and toZone
|
||||
* @author Gaël L'hopital - added ability to use second and milliseconds unix time
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class DateTimeType implements PrimitiveType, State, Command {
|
||||
@ -100,11 +103,23 @@ public class DateTimeType implements PrimitiveType, State, Command {
|
||||
try {
|
||||
date = parse("1970-01-01T" + zonedValue);
|
||||
} catch (DateTimeParseException timeOnlyException) {
|
||||
// date only
|
||||
if (zonedValue.length() == 10) {
|
||||
date = parse(zonedValue + "T00:00:00");
|
||||
} else {
|
||||
date = parse(zonedValue.substring(0, 10) + "T00:00:00" + zonedValue.substring(10));
|
||||
try {
|
||||
Long epoch = Long.valueOf(zonedValue);
|
||||
Instant i;
|
||||
// Assume that below 12 digits we're in seconds
|
||||
if (zonedValue.length() < 12) {
|
||||
i = Instant.ofEpochSecond(epoch);
|
||||
} else {
|
||||
i = Instant.ofEpochMilli(epoch);
|
||||
}
|
||||
date = ZonedDateTime.ofInstant(i, ZoneOffset.UTC);
|
||||
} catch (NumberFormatException notANumberException) {
|
||||
// date only
|
||||
if (zonedValue.length() == 10) {
|
||||
date = parse(zonedValue + "T00:00:00");
|
||||
} else {
|
||||
date = parse(zonedValue.substring(0, 10) + "T00:00:00" + zonedValue.substring(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -207,7 +222,7 @@ public class DateTimeType implements PrimitiveType, State, Command {
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((getZonedDateTime() == null) ? 0 : getZonedDateTime().hashCode());
|
||||
result = prime * result + getZonedDateTime().hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -223,14 +238,7 @@ public class DateTimeType implements PrimitiveType, State, Command {
|
||||
return false;
|
||||
}
|
||||
DateTimeType other = (DateTimeType) obj;
|
||||
if (zonedDateTime == null) {
|
||||
if (other.zonedDateTime != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (zonedDateTime.compareTo(other.zonedDateTime) != 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return zonedDateTime.compareTo(other.zonedDateTime) == 0;
|
||||
}
|
||||
|
||||
private ZonedDateTime parse(String value) throws DateTimeParseException {
|
||||
|
@ -96,7 +96,7 @@ public class DecimalType extends Number implements PrimitiveType, State, Command
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((value == null) ? 0 : value.hashCode());
|
||||
result = prime * result + value.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -112,14 +112,7 @@ public class DecimalType extends Number implements PrimitiveType, State, Command
|
||||
return false;
|
||||
}
|
||||
DecimalType other = (DecimalType) obj;
|
||||
if (value == null) {
|
||||
if (other.value != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (value.compareTo(other.value) != 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return value.compareTo(other.value) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -176,6 +169,8 @@ public class DecimalType extends Number implements PrimitiveType, State, Command
|
||||
} else if (target == HSBType.class) {
|
||||
return target.cast(new HSBType(DecimalType.ZERO, PercentType.ZERO,
|
||||
new PercentType(this.toBigDecimal().multiply(BigDecimal.valueOf(100)))));
|
||||
} else if (target == DateTimeType.class) {
|
||||
return target.cast(new DateTimeType(value.toString()));
|
||||
} else {
|
||||
return defaultConversion(target);
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ import org.junit.runners.Parameterized.Parameters;
|
||||
* @author Gaël L'hopital - Added Timezone and Milliseconds
|
||||
* @author Erdoan Hadzhiyusein - Added ZonedDateTime tests
|
||||
* @author Laurent Garnier - Enhanced tests
|
||||
* @author Gaël L'hopital - added ability to use second and milliseconds unix time
|
||||
*/
|
||||
@NonNullByDefault
|
||||
@RunWith(Parameterized.class)
|
||||
@ -304,6 +305,18 @@ public class DateTimeTypeTest {
|
||||
assertThat(zdt2, is(zdt3.withZoneSameInstant(zdt2.getZone())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void epochTest() {
|
||||
DateTimeType zdtEpoch = new DateTimeType("1970-01-01T00:00:00+0000");
|
||||
DateTimeType zdtStandard = new DateTimeType("2014-03-30T10:58:47+0000");
|
||||
DateTimeType epochSecond = new DateTimeType("0");
|
||||
DateTimeType epochStandard = new DateTimeType("1396177127");
|
||||
DateTimeType epochMilliseconds = new DateTimeType("000000000000");
|
||||
assertThat(epochSecond, is(zdtEpoch));
|
||||
assertThat(epochMilliseconds, is(zdtEpoch));
|
||||
assertThat(epochStandard, is(zdtStandard));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createDate() {
|
||||
Map<String, Integer> inputTimeMap = parameterSet.inputTimeMap;
|
||||
|
@ -117,6 +117,12 @@ public class DecimalTypeTest {
|
||||
assertEquals(new HSBType("0,0,50"), new DecimalType("0.5").as(HSBType.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConversionToDateTimeType() {
|
||||
assertEquals(new DateTimeType("2014-03-30T10:58:47+0000"),
|
||||
new DecimalType("1396177127").as(DateTimeType.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConversionToPercentType() {
|
||||
assertEquals(new PercentType(70), new DecimalType("0.7").as(PercentType.class));
|
||||
|
Loading…
Reference in New Issue
Block a user