Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make date period picker respect timezone settings #23996

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions src/components/ha-date-range-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ import {
endOfMonth,
endOfWeek,
endOfYear,
isThisYear,
startOfDay,
startOfMonth,
startOfWeek,
startOfYear,
isThisYear,
} from "date-fns";
import { fromZonedTime, toZonedTime } from "date-fns-tz";
import type { PropertyValues, TemplateResult } from "lit";
import { LitElement, css, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import { calcDate, shiftDateRange } from "../common/datetime/calc_date";
import { firstWeekdayIndex } from "../common/datetime/first_weekday";
import {
formatShortDateTimeWithYear,
formatShortDateTime,
formatShortDateTimeWithYear,
} from "../common/datetime/format_date_time";
import { useAmPm } from "../common/datetime/use_am_pm";
import { fireEvent } from "../common/dom/fire_event";
import { TimeZone } from "../data/translation";
import type { HomeAssistant } from "../types";
import "./date-range-picker";
import "./ha-icon-button";
import "./ha-textarea";
import "./ha-icon-button-next";
import "./ha-icon-button-prev";
import "./ha-textarea";

export type DateRangePickerRanges = Record<string, [Date, Date]>;

Expand Down Expand Up @@ -197,14 +200,15 @@ export class HaDateRangePicker extends LitElement {
?auto-apply=${this.autoApply}
time-picker=${this.timePicker}
twentyfour-hours=${this._hour24format}
start-date=${this.startDate.toISOString()}
end-date=${this.endDate.toISOString()}
start-date=${this._formatDate(this.startDate)}
end-date=${this._formatDate(this.endDate)}
?ranges=${this.ranges !== false}
opening-direction=${ifDefined(
this.openingDirection || this._calcedOpeningDirection
)}
first-day=${firstWeekdayIndex(this.hass.locale)}
language=${this.hass.locale.language}
@change=${this._handleChange}
>
<div slot="input" class="date-range-inputs" @click=${this._handleClick}>
${!this.minimal
Expand Down Expand Up @@ -325,9 +329,31 @@ export class HaDateRangePicker extends LitElement {
}

private _applyDateRange() {
if (this.hass.locale.time_zone === TimeZone.server) {
const dateRangePicker = this._dateRangePicker;

const startDate = fromZonedTime(
dateRangePicker.start,
this.hass.config.time_zone
);
const endDate = fromZonedTime(
dateRangePicker.end,
this.hass.config.time_zone
);

dateRangePicker.clickRange([startDate, endDate]);
}

this._dateRangePicker.clickedApply();
}

private _formatDate(date: Date): string {
if (this.hass.locale.time_zone === TimeZone.server) {
return toZonedTime(date, this.hass.config.time_zone).toISOString();
}
return date.toISOString();
}

private get _dateRangePicker() {
const dateRangePicker = this.shadowRoot!.querySelector(
"date-range-picker"
Expand Down Expand Up @@ -358,6 +384,16 @@ export class HaDateRangePicker extends LitElement {
}
}

private _handleChange(ev: CustomEvent) {
ev.stopPropagation();
const startDate = ev.detail.startDate;
const endDate = ev.detail.endDate;

fireEvent(this, "value-changed", {
value: { startDate, endDate },
});
}

static styles = css`

ha-icon-button {
Expand Down
6 changes: 3 additions & 3 deletions src/panels/history/ha-panel-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class HaPanelHistory extends LitElement {
.endDate=${this._endDate}
extended-presets
time-picker
@change=${this._dateRangeChanged}
@value-changed=${this._dateRangeChanged}
></ha-date-range-picker>
<ha-target-picker
.hass=${this.hass}
Expand Down Expand Up @@ -424,8 +424,8 @@ class HaPanelHistory extends LitElement {
);

private _dateRangeChanged(ev) {
this._startDate = ev.detail.startDate;
const endDate = ev.detail.endDate;
this._startDate = ev.detail.value.startDate;
const endDate = ev.detail.value.endDate;
if (endDate.getHours() === 0 && endDate.getMinutes() === 0) {
endDate.setDate(endDate.getDate() + 1);
endDate.setMilliseconds(endDate.getMilliseconds() - 1);
Expand Down
6 changes: 3 additions & 3 deletions src/panels/logbook/ha-panel-logbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class HaPanelLogbook extends LitElement {
.hass=${this.hass}
.startDate=${this._time.range[0]}
.endDate=${this._time.range[1]}
@change=${this._dateRangeChanged}
@value-changed=${this._dateRangeChanged}
time-picker
></ha-date-range-picker>

Expand Down Expand Up @@ -233,8 +233,8 @@ export class HaPanelLogbook extends LitElement {
}

private _dateRangeChanged(ev) {
const startDate = ev.detail.startDate;
const endDate = ev.detail.endDate;
const startDate = ev.detail.value.startDate;
const endDate = ev.detail.value.endDate;
if (endDate.getHours() === 0 && endDate.getMinutes() === 0) {
endDate.setDate(endDate.getDate() + 1);
endDate.setMilliseconds(endDate.getMilliseconds() - 1);
Expand Down
6 changes: 3 additions & 3 deletions src/panels/lovelace/components/hui-energy-period-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
.startDate=${this._startDate}
.endDate=${this._endDate || new Date()}
.ranges=${this._ranges}
@change=${this._dateRangeChanged}
@value-changed=${this._dateRangeChanged}
minimal
></ha-date-range-picker>
</div>
Expand Down Expand Up @@ -346,7 +346,7 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
private _dateRangeChanged(ev) {
const weekStartsOn = firstWeekdayIndex(this.hass.locale);
this._startDate = calcDate(
ev.detail.startDate,
ev.detail.value.startDate,
startOfDay,
this.hass.locale,
this.hass.config,
Expand All @@ -355,7 +355,7 @@ export class HuiEnergyPeriodSelector extends SubscribeMixin(LitElement) {
}
);
this._endDate = calcDate(
ev.detail.endDate,
ev.detail.value.endDate,
endOfDay,
this.hass.locale,
this.hass.config,
Expand Down
Loading