Skip to content

Commit fb6d1dd

Browse files
DerStoeckida-Kai
andauthored
Common: TimeLeapClock constructor with Instant, and "now" method (OpenEMS#2407)
This commit adds a constructor, with an Instant Option only. This commit is one of many prepare PRs / commits for a bigger PR: SumStateAlert OpenEMS#2260 By Seperating the big OpenEMS#2260 into smaller commits we would like to make the review easier. Co-authored-by: Kai Jeschek <99220919+da-Kai@users.noreply.github.com>
1 parent da07725 commit fb6d1dd

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

io.openems.common/src/io/openems/common/test/TimeLeapClock.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.time.Instant;
55
import java.time.ZoneId;
66
import java.time.ZoneOffset;
7+
import java.time.ZonedDateTime;
78
import java.time.temporal.TemporalUnit;
89

910
public class TimeLeapClock extends Clock {
@@ -21,6 +22,10 @@ public TimeLeapClock(ZoneId zone) {
2122
this(Instant.now(), zone);
2223
}
2324

25+
public TimeLeapClock(Instant start) {
26+
this(start, ZoneOffset.UTC);
27+
}
28+
2429
public TimeLeapClock() {
2530
this(Instant.now(), ZoneOffset.UTC);
2631
}
@@ -30,7 +35,7 @@ public Clock withZone(ZoneId zone) {
3035
if (zone.equals(this.zone)) { // intentional NPE
3136
return this;
3237
}
33-
return new TimeLeapClock(zone);
38+
return new TimeLeapClock(this.instant, zone);
3439
}
3540

3641
@Override
@@ -60,11 +65,10 @@ public Instant instant() {
6065

6166
@Override
6267
public boolean equals(Object obj) {
63-
if (obj instanceof TimeLeapClock) {
64-
var other = (TimeLeapClock) obj;
65-
return this.instant.equals(other.instant) && this.zone.equals(other.zone);
66-
}
67-
return false;
68+
return obj == this //
69+
|| obj instanceof Clock other //
70+
&& this.instant.equals(other.instant()) //
71+
&& this.zone.equals(other.getZone());
6872
}
6973

7074
@Override
@@ -74,6 +78,15 @@ public int hashCode() {
7478

7579
@Override
7680
public String toString() {
77-
return "TimeLeapClock[" + this.instant + "," + this.zone + "]";
81+
return "TimeLeapClock[" + this.instant + ", " + this.zone + "]";
82+
}
83+
84+
/**
85+
* Get current DateTime as {@link ZonedDateTime}.
86+
*
87+
* @return current date and time.
88+
*/
89+
public ZonedDateTime now() {
90+
return ZonedDateTime.ofInstant(this.instant, this.zone);
7891
}
7992
}

0 commit comments

Comments
 (0)