mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 00:37:13 +00:00
Small speed up to creating stats database rows (#124587)
* Small speed up to creating stats database rows Calling .timestamp() directly is faster on new cpython * more cleanup
This commit is contained in:
parent
902d76da36
commit
4bc19876ca
@ -693,13 +693,13 @@ class StatisticsBase:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_stats(cls, metadata_id: int, stats: StatisticData) -> Self:
|
def from_stats(cls, metadata_id: int, stats: StatisticData) -> Self:
|
||||||
"""Create object from a statistics with datatime objects."""
|
"""Create object from a statistics with datetime objects."""
|
||||||
return cls( # type: ignore[call-arg]
|
return cls( # type: ignore[call-arg]
|
||||||
metadata_id=metadata_id,
|
metadata_id=metadata_id,
|
||||||
created=None,
|
created=None,
|
||||||
created_ts=time.time(),
|
created_ts=time.time(),
|
||||||
start=None,
|
start=None,
|
||||||
start_ts=dt_util.utc_to_timestamp(stats["start"]),
|
start_ts=stats["start"].timestamp(),
|
||||||
mean=stats.get("mean"),
|
mean=stats.get("mean"),
|
||||||
min=stats.get("min"),
|
min=stats.get("min"),
|
||||||
max=stats.get("max"),
|
max=stats.get("max"),
|
||||||
|
@ -65,9 +65,7 @@ def process_datetime_to_timestamp(ts: datetime) -> float:
|
|||||||
|
|
||||||
def datetime_to_timestamp_or_none(dt: datetime | None) -> float | None:
|
def datetime_to_timestamp_or_none(dt: datetime | None) -> float | None:
|
||||||
"""Convert a datetime to a timestamp."""
|
"""Convert a datetime to a timestamp."""
|
||||||
if dt is None:
|
return None if dt is None else dt.timestamp()
|
||||||
return None
|
|
||||||
return dt_util.utc_to_timestamp(dt)
|
|
||||||
|
|
||||||
|
|
||||||
def timestamp_to_datetime_or_none(ts: float | None) -> datetime | None:
|
def timestamp_to_datetime_or_none(ts: float | None) -> datetime | None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user