Skip to content

Commit

Permalink
Fix energy dates when using server TZ (#20191)
Browse files Browse the repository at this point in the history
* Fix energy dates when using server TZ

* update
  • Loading branch information
karwosts authored Mar 27, 2024
1 parent 7ca5467 commit 141c8c5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
17 changes: 17 additions & 0 deletions src/common/datetime/calc_date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,20 @@ export const calcDateProperty = (
locale.time_zone === TimeZone.server
? (calcZonedDate(date, config.time_zone, fn, options) as number | boolean)
: fn(date, options);

export const calcDateDifferenceProperty = (
endDate: Date,
startDate: Date,
fn: (date: Date, options?: any) => boolean | number,
locale: FrontendLocaleData,
config: HassConfig
) =>
calcDateProperty(
endDate,
fn,
locale,
config,
locale.time_zone === TimeZone.server
? utcToZonedTime(startDate, config.time_zone)
: startDate
);
12 changes: 8 additions & 4 deletions src/data/energy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
isLastDayOfMonth,
} from "date-fns/esm";
import { Collection, getCollection } from "home-assistant-js-websocket";
import { calcDate, calcDateProperty } from "../common/datetime/calc_date";
import {
calcDate,
calcDateProperty,
calcDateDifferenceProperty,
} from "../common/datetime/calc_date";
import { formatTime24h } from "../common/datetime/format_time";
import { groupBy } from "../common/util/group-by";
import { HomeAssistant } from "../types";
Expand Down Expand Up @@ -443,12 +447,12 @@ const getEnergyData = async (
addMonths,
hass.locale,
hass.config,
-(calcDateProperty(
-(calcDateDifferenceProperty(
end || new Date(),
start,
differenceInMonths,
hass.locale,
hass.config,
start
hass.config
) as number) - 1
);
} else {
Expand Down
42 changes: 23 additions & 19 deletions src/panels/lovelace/components/hui-energy-period-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import {
} from "lit";
import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one";
import { calcDate, calcDateProperty } from "../../../common/datetime/calc_date";
import {
calcDate,
calcDateProperty,
calcDateDifferenceProperty,
} from "../../../common/datetime/calc_date";
import { firstWeekdayIndex } from "../../../common/datetime/first_weekday";
import {
formatDate,
Expand Down Expand Up @@ -300,23 +304,23 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
(calcDateProperty(endDate, isLastDayOfMonth, locale, config) as boolean)
) {
if (
(calcDateProperty(
(calcDateDifferenceProperty(
endDate,
startDate,
differenceInMonths,
locale,
config,
startDate
config
) as number) === 0
) {
return "month";
}
if (
(calcDateProperty(
(calcDateDifferenceProperty(
endDate,
startDate,
differenceInMonths,
locale,
config,
startDate
config
) as number) === 2 &&
startDate.getMonth() % 3 === 0
) {
Expand All @@ -326,12 +330,12 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
if (
calcDateProperty(startDate, isFirstDayOfMonth, locale, config) &&
calcDateProperty(endDate, isLastDayOfMonth, locale, config) &&
calcDateProperty(
calcDateDifferenceProperty(
endDate,
startDate,
differenceInMonths,
locale,
config,
startDate
config
) === 11
) {
return "year";
Expand Down Expand Up @@ -468,12 +472,12 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
);
} else {
// Custom date range
const difference = calcDateProperty(
const difference = calcDateDifferenceProperty(
this._endDate!,
this._startDate,
differenceInDays,
this.hass.locale,
this.hass.config,
this._startDate
this.hass.config
) as number;
this._startDate = calcDate(
calcDate(
Expand Down Expand Up @@ -534,12 +538,12 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
) {
// Shift date range with respect to month/year selection
const difference =
((calcDateProperty(
((calcDateDifferenceProperty(
this._endDate!,
this._startDate,
differenceInMonths,
this.hass.locale,
this.hass.config,
this._startDate
this.hass.config
) as number) +
1) *
(forward ? 1 : -1);
Expand All @@ -565,12 +569,12 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
} else {
// Shift date range by period length
const difference =
((calcDateProperty(
((calcDateDifferenceProperty(
this._endDate!,
this._startDate,
differenceInDays,
this.hass.locale,
this.hass.config,
this._startDate
this.hass.config
) as number) +
1) *
(forward ? 1 : -1);
Expand Down

0 comments on commit 141c8c5

Please sign in to comment.