Skip to content

Commit

Permalink
Issue openhab#1157 Enhance DecimalType to accept epoch seconds and mi…
Browse files Browse the repository at this point in the history
…lliseconds. (openhab#1158)

* Issue 1157 Enhance DecimalType to accept epoch seconds and milliseconds.

Signed-off-by: Gaël L'hopital <gael@lhopital.org>
  • Loading branch information
clinique authored and kaikreuzer committed Nov 10, 2019
1 parent 492526d commit 694ff49
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
Expand Up @@ -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);
Expand Down Expand Up @@ -66,7 +66,13 @@ public void send(DateTimeType command) {
@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);
}
Expand Down
Expand Up @@ -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;
Expand All @@ -37,6 +39,7 @@
* @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 {
Expand Down Expand Up @@ -100,11 +103,23 @@ public DateTimeType(String zonedValue) {
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));
}
}
}
}
Expand Down Expand Up @@ -207,7 +222,7 @@ public String toFullString() {
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;
}

Expand All @@ -223,14 +238,7 @@ public boolean equals(@Nullable Object obj) {
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 {
Expand Down
Expand Up @@ -96,7 +96,7 @@ public BigDecimal toBigDecimal() {
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;
}

Expand All @@ -112,14 +112,7 @@ public boolean equals(@Nullable Object obj) {
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
Expand Down Expand Up @@ -176,6 +169,8 @@ public long longValue() {
} 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);
}
Expand Down
Expand Up @@ -42,6 +42,7 @@
* @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)
Expand Down Expand Up @@ -304,6 +305,18 @@ public void parsingTest() {
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;
Expand Down
Expand Up @@ -117,6 +117,12 @@ public void testConversionToHSBType() {
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));
Expand Down

0 comments on commit 694ff49

Please sign in to comment.