Improve the performance of dt_util.utcnow() (#39145)
parent
3cc099af80
commit
42227c1c53
|
@ -52,7 +52,7 @@ def get_time_zone(time_zone_str: str) -> Optional[dt.tzinfo]:
|
|||
|
||||
def utcnow() -> dt.datetime:
|
||||
"""Get now in UTC time."""
|
||||
return dt.datetime.now(UTC)
|
||||
return dt.datetime.utcnow().replace(tzinfo=UTC)
|
||||
|
||||
|
||||
def now(time_zone: Optional[dt.tzinfo] = None) -> dt.datetime:
|
||||
|
|
|
@ -1 +1,12 @@
|
|||
"""Tests for the metoffice component."""
|
||||
|
||||
import datetime
|
||||
|
||||
|
||||
class NewDateTime(datetime.datetime):
|
||||
"""Patch time to a specific point."""
|
||||
|
||||
@classmethod
|
||||
def now(cls, *args, **kwargs): # pylint: disable=signature-differs
|
||||
"""Overload datetime.datetime.now."""
|
||||
return cls(2020, 4, 25, 12, tzinfo=datetime.timezone.utc)
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
"""The tests for the Met Office sensor component."""
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
|
||||
from homeassistant.components.metoffice.const import ATTRIBUTION, DOMAIN
|
||||
|
||||
from . import NewDateTime
|
||||
from .const import (
|
||||
DATETIME_FORMAT,
|
||||
KINGSLYNN_SENSOR_RESULTS,
|
||||
|
@ -15,13 +15,12 @@ from .const import (
|
|||
WAVERTREE_SENSOR_RESULTS,
|
||||
)
|
||||
|
||||
from tests.async_mock import Mock, patch
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
|
||||
@patch(
|
||||
"datapoint.Forecast.datetime.datetime",
|
||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
||||
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||
)
|
||||
async def test_one_sensor_site_running(hass, requests_mock, legacy_patchable_time):
|
||||
"""Test the Met Office sensor platform."""
|
||||
|
@ -58,8 +57,7 @@ async def test_one_sensor_site_running(hass, requests_mock, legacy_patchable_tim
|
|||
|
||||
|
||||
@patch(
|
||||
"datapoint.Forecast.datetime.datetime",
|
||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
||||
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||
)
|
||||
async def test_two_sensor_sites_running(hass, requests_mock, legacy_patchable_time):
|
||||
"""Test we handle two sets of sensors running for two different sites."""
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
"""The tests for the Met Office sensor component."""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from datetime import timedelta
|
||||
import json
|
||||
|
||||
from homeassistant.components.metoffice.const import DOMAIN
|
||||
from homeassistant.const import STATE_UNAVAILABLE
|
||||
from homeassistant.util import utcnow
|
||||
|
||||
from . import NewDateTime
|
||||
from .const import (
|
||||
METOFFICE_CONFIG_KINGSLYNN,
|
||||
METOFFICE_CONFIG_WAVERTREE,
|
||||
WAVERTREE_SENSOR_RESULTS,
|
||||
)
|
||||
|
||||
from tests.async_mock import Mock, patch
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
|
||||
|
||||
|
||||
@patch(
|
||||
"datapoint.Forecast.datetime.datetime",
|
||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
||||
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||
)
|
||||
async def test_site_cannot_connect(hass, requests_mock, legacy_patchable_time):
|
||||
"""Test we handle cannot connect error."""
|
||||
|
@ -39,8 +39,7 @@ async def test_site_cannot_connect(hass, requests_mock, legacy_patchable_time):
|
|||
|
||||
|
||||
@patch(
|
||||
"datapoint.Forecast.datetime.datetime",
|
||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
||||
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||
)
|
||||
async def test_site_cannot_update(hass, requests_mock, legacy_patchable_time):
|
||||
"""Test we handle cannot connect error."""
|
||||
|
@ -74,8 +73,7 @@ async def test_site_cannot_update(hass, requests_mock, legacy_patchable_time):
|
|||
|
||||
|
||||
@patch(
|
||||
"datapoint.Forecast.datetime.datetime",
|
||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
||||
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||
)
|
||||
async def test_one_weather_site_running(hass, requests_mock, legacy_patchable_time):
|
||||
"""Test the Met Office weather platform."""
|
||||
|
@ -108,8 +106,7 @@ async def test_one_weather_site_running(hass, requests_mock, legacy_patchable_ti
|
|||
|
||||
|
||||
@patch(
|
||||
"datapoint.Forecast.datetime.datetime",
|
||||
Mock(now=Mock(return_value=datetime(2020, 4, 25, 12, tzinfo=timezone.utc))),
|
||||
"datapoint.Forecast.datetime.datetime", NewDateTime,
|
||||
)
|
||||
async def test_two_weather_sites_running(hass, requests_mock, legacy_patchable_time):
|
||||
"""Test we handle two different weather sites both running."""
|
||||
|
|
Loading…
Reference in New Issue