Skip to Content
Octopus Energy Packages
Migration

Migrating to v2

Migrate timeframe and anchor date handling to explicit timezones and calendar dates

Version 2 uses the configured IANA timezone for calendar arithmetic. resolveTimeframe and useTimeframe already required this in v1. resolveAnchorDate and anchorDateQueryOptions now require it too.

Anchor date query cache data and SerializedTimeframe.date now use YYYY-MM-DD values. Direct resolution and client query results remain Date objects.

Update anchor date calls

Pass the property's measurement timezone to direct anchor date resolution:

const anchorDate = await resolveAnchorDate(client, {
  marketSupplyPoints,
  propertyId,
  timezone: propertyTimezone,
});

Do the same when creating query options:

const options = anchorDateQueryOptions(client, {
  marketSupplyPoints,
  propertyId,
  timezone: propertyTimezone,
});

A runtime Date is treated as a calendar selection, not an instant to convert. Its local year, month, and day fields are interpreted in the configured timezone. When starting from YYYY-MM-DD, use parseCalendarDate before resolving a timeframe.

Keep one timezone throughout

Use the same IANA timezone when fetching, transforming, and displaying measurements. Pass the resolved timeframe to createMeasurementsOverview, then pass its timezone to Coral:

const overview = createMeasurementsOverview(measurements, {
  preferences,
  timeframe,
});
const cardProps = getCardProps({
  overview,
  preferences,
  preferencesActions,
  utility,
});

<EnergyOverviewCard {...cardProps} timezone={timeframe.timezone}>
  {/* Card contents */}
</EnergyOverviewCard>;

Handle calendar anchor dates

anchorDateQueryOptions uses a YYYY-MM-DD value for its query function and cache data. On the server, parse the value before using it as a Date:

Server-side prefetching
import { QueryClient } from "@tanstack/react-query";
import { anchorDateQueryOptions } from "@octopus-energy/measurements/react-query";
import { parseCalendarDate } from "@octopus-energy/measurements/timeframe";

const queryClient = new QueryClient();
const anchorDateValue = await queryClient.fetchQuery(
  anchorDateQueryOptions(client, {
    marketSupplyPoints,
    propertyId,
    timezone: "Europe/London",
  }),
);
const anchorDate = parseCalendarDate(anchorDateValue);

if (!anchorDate) throw new Error("Invalid anchor-date calendar value");

Do not use new Date("YYYY-MM-DD"). JavaScript interprets that form as UTC, which can move the visible date backward in timezones west of UTC.

On the client, anchorDateQueryOptions applies its select transform. The useQuery result is already a Date.

Client-side fetching
import { useQuery } from "@tanstack/react-query";
import { anchorDateQueryOptions } from "@octopus-energy/measurements/react-query";

const { data: anchorDate } = useQuery(
  anchorDateQueryOptions(client, {
    marketSupplyPoints,
    propertyId,
    timezone: "Europe/London",
  }),
);

Update serialized timeframes

SerializedTimeframe.date now contains a YYYY-MM-DD calendar value. Continue to use deserializeTimeframe at serialization boundaries, it reads both the new calendar values and legacy ISO anchor timestamps.

const serialized = serializeTimeframe(timeframe);
const restored = deserializeTimeframe(serialized);

On this page