Fix energy dates when using server TZ (#20191)

* Fix energy dates when using server TZ

* update
This commit is contained in:
karwosts 2024-03-27 07:24:25 -07:00 committed by GitHub
parent 7ca5467f4c
commit 141c8c5192
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 48 additions and 23 deletions

View File

@ -37,3 +37,20 @@ export const calcDateProperty = (
locale.time_zone === TimeZone.server locale.time_zone === TimeZone.server
? (calcZonedDate(date, config.time_zone, fn, options) as number | boolean) ? (calcZonedDate(date, config.time_zone, fn, options) as number | boolean)
: fn(date, options); : 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
);

View File

@ -11,7 +11,11 @@ import {
isLastDayOfMonth, isLastDayOfMonth,
} from "date-fns/esm"; } from "date-fns/esm";
import { Collection, getCollection } from "home-assistant-js-websocket"; 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 { formatTime24h } from "../common/datetime/format_time";
import { groupBy } from "../common/util/group-by"; import { groupBy } from "../common/util/group-by";
import { HomeAssistant } from "../types"; import { HomeAssistant } from "../types";
@ -443,12 +447,12 @@ const getEnergyData = async (
addMonths, addMonths,
hass.locale, hass.locale,
hass.config, hass.config,
-(calcDateProperty( -(calcDateDifferenceProperty(
end || new Date(), end || new Date(),
start,
differenceInMonths, differenceInMonths,
hass.locale, hass.locale,
hass.config, hass.config
start
) as number) - 1 ) as number) - 1
); );
} else { } else {

View File

@ -32,7 +32,11 @@ import {
} from "lit"; } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import memoizeOne from "memoize-one"; 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 { firstWeekdayIndex } from "../../../common/datetime/first_weekday";
import { import {
formatDate, formatDate,
@ -300,23 +304,23 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
(calcDateProperty(endDate, isLastDayOfMonth, locale, config) as boolean) (calcDateProperty(endDate, isLastDayOfMonth, locale, config) as boolean)
) { ) {
if ( if (
(calcDateProperty( (calcDateDifferenceProperty(
endDate, endDate,
startDate,
differenceInMonths, differenceInMonths,
locale, locale,
config, config
startDate
) as number) === 0 ) as number) === 0
) { ) {
return "month"; return "month";
} }
if ( if (
(calcDateProperty( (calcDateDifferenceProperty(
endDate, endDate,
startDate,
differenceInMonths, differenceInMonths,
locale, locale,
config, config
startDate
) as number) === 2 && ) as number) === 2 &&
startDate.getMonth() % 3 === 0 startDate.getMonth() % 3 === 0
) { ) {
@ -326,12 +330,12 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
if ( if (
calcDateProperty(startDate, isFirstDayOfMonth, locale, config) && calcDateProperty(startDate, isFirstDayOfMonth, locale, config) &&
calcDateProperty(endDate, isLastDayOfMonth, locale, config) && calcDateProperty(endDate, isLastDayOfMonth, locale, config) &&
calcDateProperty( calcDateDifferenceProperty(
endDate, endDate,
startDate,
differenceInMonths, differenceInMonths,
locale, locale,
config, config
startDate
) === 11 ) === 11
) { ) {
return "year"; return "year";
@ -468,12 +472,12 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
); );
} else { } else {
// Custom date range // Custom date range
const difference = calcDateProperty( const difference = calcDateDifferenceProperty(
this._endDate!, this._endDate!,
this._startDate,
differenceInDays, differenceInDays,
this.hass.locale, this.hass.locale,
this.hass.config, this.hass.config
this._startDate
) as number; ) as number;
this._startDate = calcDate( this._startDate = calcDate(
calcDate( calcDate(
@ -534,12 +538,12 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
) { ) {
// Shift date range with respect to month/year selection // Shift date range with respect to month/year selection
const difference = const difference =
((calcDateProperty( ((calcDateDifferenceProperty(
this._endDate!, this._endDate!,
this._startDate,
differenceInMonths, differenceInMonths,
this.hass.locale, this.hass.locale,
this.hass.config, this.hass.config
this._startDate
) as number) + ) as number) +
1) * 1) *
(forward ? 1 : -1); (forward ? 1 : -1);
@ -565,12 +569,12 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
} else { } else {
// Shift date range by period length // Shift date range by period length
const difference = const difference =
((calcDateProperty( ((calcDateDifferenceProperty(
this._endDate!, this._endDate!,
this._startDate,
differenceInDays, differenceInDays,
this.hass.locale, this.hass.locale,
this.hass.config, this.hass.config
this._startDate
) as number) + ) as number) +
1) * 1) *
(forward ? 1 : -1); (forward ? 1 : -1);