Use InstantSource where time zone is not used (#20067)

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen
2026-01-16 09:48:12 +01:00
committed by GitHub
parent 9a224cb13a
commit 158e0411aa
4 changed files with 22 additions and 23 deletions
@@ -13,9 +13,9 @@
package org.openhab.binding.energidataservice.internal;
import java.math.BigDecimal;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.InstantSource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
@@ -39,14 +39,14 @@ import org.openhab.binding.energidataservice.internal.provider.cache.Electricity
@NonNullByDefault
public class PriceListParser {
private final Clock clock;
private final InstantSource instantSource;
public PriceListParser() {
this(Clock.system(EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
this(InstantSource.system());
}
public PriceListParser(Clock clock) {
this.clock = clock;
public PriceListParser(InstantSource instantSource) {
this.instantSource = instantSource;
}
public Map<Instant, BigDecimal> toHourly(Collection<DatahubPricelistRecord> records) {
@@ -59,11 +59,10 @@ public class PriceListParser {
}
private Map<Instant, BigDecimal> toResolution(Collection<DatahubPricelistRecord> records, Duration resolution) {
Instant now = instantSource.instant();
Instant firstStart = truncateToResolution(
Instant.now(clock).minus(ElectricityPriceSubscriptionCache.NUMBER_OF_HISTORIC_HOURS, ChronoUnit.HOURS),
resolution);
Instant lastStart = Instant.now(clock).truncatedTo(ChronoUnit.HOURS).plus(2, ChronoUnit.DAYS)
.truncatedTo(ChronoUnit.DAYS);
now.minus(ElectricityPriceSubscriptionCache.NUMBER_OF_HISTORIC_HOURS, ChronoUnit.HOURS), resolution);
Instant lastStart = now.truncatedTo(ChronoUnit.HOURS).plus(2, ChronoUnit.DAYS).truncatedTo(ChronoUnit.DAYS);
return toResolution(records, firstStart, lastStart, resolution);
}
@@ -36,7 +36,7 @@ public class DatahubPriceSubscriptionCache
public static final int MAX_CACHE_SIZE = 24 * 2 + NUMBER_OF_HISTORIC_HOURS;
private final PriceListParser priceListParser = new PriceListParser();
private final PriceListParser priceListParser;
private Collection<DatahubPricelistRecord> datahubRecords = new CopyOnWriteArrayList<>();
@@ -46,6 +46,7 @@ public class DatahubPriceSubscriptionCache
public DatahubPriceSubscriptionCache(Clock clock) {
super(clock);
this.priceListParser = new PriceListParser(clock);
}
/**
@@ -20,8 +20,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Instant;
import java.time.InstantSource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
@@ -67,7 +67,7 @@ public class PriceListParserTest {
@Test
void toHourlyNoChanges() throws IOException {
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(Instant.parse("2023-01-23T12:00:00Z"), EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
InstantSource.fixed(Instant.parse("2023-01-23T12:00:00Z")));
DatahubPricelistRecords records = getObjectFromJson("DatahubPricelistN1.json", DatahubPricelistRecords.class);
Map<Instant, BigDecimal> tariffMap = priceListParser.toHourly(Arrays.stream(records.records()).toList());
@@ -81,7 +81,7 @@ public class PriceListParserTest {
@Test
void toHourlyNewTariffTomorrowWhenSummertime() throws IOException {
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(Instant.parse("2023-03-31T12:00:00Z"), EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
InstantSource.fixed(Instant.parse("2023-03-31T12:00:00Z")));
DatahubPricelistRecords records = getObjectFromJson("DatahubPricelistN1.json", DatahubPricelistRecords.class);
Map<Instant, BigDecimal> tariffMap = priceListParser.toHourly(Arrays.stream(records.records()).toList());
@@ -95,7 +95,7 @@ public class PriceListParserTest {
@Test
void toHourlyNewTariffAtMidnight() throws IOException {
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(Instant.parse("2022-12-31T12:00:00Z"), EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
InstantSource.fixed(Instant.parse("2022-12-31T12:00:00Z")));
DatahubPricelistRecords records = getObjectFromJson("DatahubPricelistN1.json", DatahubPricelistRecords.class);
Map<Instant, BigDecimal> tariffMap = priceListParser
.toHourly(Arrays.stream(records.records()).filter(r -> r.chargeTypeCode().equals("CD")).toList());
@@ -109,7 +109,7 @@ public class PriceListParserTest {
@Test
void toHourlyDiscount() throws IOException {
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(Instant.parse("2022-12-31T12:00:00Z"), EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
InstantSource.fixed(Instant.parse("2022-12-31T12:00:00Z")));
DatahubPricelistRecords records = getObjectFromJson("DatahubPricelistN1.json", DatahubPricelistRecords.class);
Map<Instant, BigDecimal> tariffMap = priceListParser
.toHourly(Arrays.stream(records.records()).filter(r -> r.chargeTypeCode().equals("CD R")).toList());
@@ -123,7 +123,7 @@ public class PriceListParserTest {
@Test
void toHourlyTariffAndDiscountIsSum() throws IOException {
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(Instant.parse("2022-11-30T15:00:00Z"), EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
InstantSource.fixed(Instant.parse("2022-11-30T15:00:00Z")));
DatahubPricelistRecords records = getObjectFromJson("DatahubPricelistN1.json", DatahubPricelistRecords.class);
Map<Instant, BigDecimal> tariffMap = priceListParser.toHourly(Arrays.stream(records.records()).toList());
@@ -135,7 +135,7 @@ public class PriceListParserTest {
@Test
void toHourlyTariffAndDiscountIsFree() throws IOException {
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(Instant.parse("2022-12-31T12:00:00Z"), EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
InstantSource.fixed(Instant.parse("2022-12-31T12:00:00Z")));
DatahubPricelistRecords records = getObjectFromJson("DatahubPricelistN1.json", DatahubPricelistRecords.class);
Map<Instant, BigDecimal> tariffMap = priceListParser.toHourly(Arrays.stream(records.records()).toList());
@@ -149,7 +149,7 @@ public class PriceListParserTest {
@Test
void toHourlyFixedTariff() throws IOException {
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(Instant.parse("2022-12-31T23:00:00Z"), EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
InstantSource.fixed(Instant.parse("2022-12-31T23:00:00Z")));
DatahubPricelistRecords records = getObjectFromJson("DatahubPricelistNordEnergi.json",
DatahubPricelistRecords.class);
Map<Instant, BigDecimal> tariffMap = priceListParser.toHourly(Arrays.stream(records.records()).toList());
@@ -164,7 +164,7 @@ public class PriceListParserTest {
@Test
void toHourlyDailyTariffs() throws IOException {
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(Instant.parse("2023-01-28T04:00:00Z"), EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
InstantSource.fixed(Instant.parse("2023-01-28T04:00:00Z")));
DatahubPricelistRecords records = getObjectFromJson("DatahubPricelistTrefor.json",
DatahubPricelistRecords.class);
Map<Instant, BigDecimal> tariffMap = priceListParser.toHourly(Arrays.stream(records.records()).toList());
@@ -183,7 +183,7 @@ public class PriceListParserTest {
@Test
void toHourlySystemTariff() throws IOException {
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(Instant.parse("2023-06-30T21:00:00Z"), EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
InstantSource.fixed(Instant.parse("2023-06-30T21:00:00Z")));
DatahubPricelistRecords records = getObjectFromJson("DatahubPricelistElectricityTax.json",
DatahubPricelistRecords.class);
Map<Instant, BigDecimal> tariffMap = priceListParser.toHourly(Arrays.stream(records.records()).toList());
@@ -20,9 +20,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.InstantSource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
@@ -419,8 +419,7 @@ public class EnergiDataServiceActionsTest {
Map<Instant, BigDecimal> spotPrices = Arrays.stream(spotPriceRecords)
.collect(Collectors.toMap(SpotPrice::start, SpotPrice::spotPrice));
PriceListParser priceListParser = new PriceListParser(
Clock.fixed(spotPriceRecords[0].start, EnergiDataServiceBindingConstants.DATAHUB_TIMEZONE));
PriceListParser priceListParser = new PriceListParser(InstantSource.fixed(spotPriceRecords[0].start));
DatahubPricelistRecords datahubRecords = getObjectFromJson("GridTariffs.json", DatahubPricelistRecords.class);
Map<Instant, BigDecimal> gridTariffs = priceListParser
.toHourly(Arrays.stream(datahubRecords.records()).toList());