Skip to content

Commit d229088

Browse files
committed
Remove unnecessary usage of pytz
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent c71ed5f commit d229088

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

src/frequenz/sdk/power_distribution/power_distributor.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
MIT
1010
"""
1111
import asyncio
12-
import datetime as dt
1312
import logging
1413
from asyncio.tasks import ALL_COMPLETED
14+
from datetime import datetime, timezone
1515
from typing import ( # pylint: disable=unused-import
1616
Any,
1717
Dict,
@@ -24,7 +24,6 @@
2424
)
2525

2626
import grpc
27-
import pytz
2827
from frequenz.channels import BidirectionalHandle, Peekable, Receiver
2928
from google.protobuf.empty_pb2 import Empty # pylint: disable=no-name-in-module
3029

@@ -528,7 +527,7 @@ def _is_component_data_valid(
528527
)
529528
return False
530529

531-
now = dt.datetime.now(tz=pytz.UTC)
530+
now = datetime.now(timezone.utc)
532531
time_delta = now - component_data.timestamp
533532
if time_delta.total_seconds() > self.component_data_timeout_sec:
534533
_logger.warning(

src/frequenz/sdk/timeseries/resampler.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010

1111
import logging
1212
from collections import deque
13-
from datetime import datetime, timedelta
13+
from datetime import datetime, timedelta, timezone
1414
from typing import Callable, Deque, Dict, Generator, Optional, Sequence, Tuple
1515

16-
import pytz
17-
1816
from ..data_pipeline import Sample
1917

2018
logger = logging.Logger(__name__)
@@ -88,7 +86,7 @@ def resample(self) -> Optional[float]:
8886
"""
8987
# It might be better to provide `now` from the outside so that all
9088
# individual resamplers use the same `now`
91-
now = datetime.now(tz=pytz.UTC)
89+
now = datetime.now(timezone.utc)
9290
threshold = now - timedelta(
9391
seconds=self._max_data_age_in_periods * self._resampling_period_s
9492
)
@@ -182,6 +180,6 @@ def resample(self) -> Generator[Tuple[str, Sample], None, None]:
182180
Yields:
183181
iterator of time series ids and their newly resampled samples
184182
"""
185-
now = datetime.now(tz=pytz.UTC)
183+
now = datetime.now(timezone.utc)
186184
for time_series_id, resampler in self._resamplers.items():
187185
yield time_series_id, Sample(timestamp=now, value=resampler.resample())

tests/timeseries/test_resampling.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
MIT
99
"""
1010

11-
from datetime import datetime, timedelta
11+
from datetime import datetime, timedelta, timezone
1212
from typing import Sequence
1313

14-
import pytz
1514
import time_machine
1615

1716
from frequenz.sdk.data_pipeline import Sample
@@ -51,7 +50,7 @@ def test_component_metric_resampler_remove_outdated_samples() -> None:
5150
resampling_function=resampling_function_sum,
5251
)
5352

54-
timestamp = datetime.now(tz=pytz.UTC)
53+
timestamp = datetime.now(timezone.utc)
5554
sample1 = Sample(timestamp, value=5.0)
5655
sample2 = Sample(timestamp + timedelta(seconds=1), value=12.0)
5756
resampler.add_sample(sample1)
@@ -73,7 +72,7 @@ def test_component_metric_resampler_resample() -> None:
7372
resampling_function=resampling_function_sum,
7473
)
7574

76-
timestamp = datetime.now(tz=pytz.UTC) - timedelta(seconds=0.5)
75+
timestamp = datetime.now(timezone.utc) - timedelta(seconds=0.5)
7776

7877
value1 = 5.0
7978
value2 = 15.0
@@ -98,7 +97,7 @@ def test_component_metric_resampler_resample_with_outdated_samples() -> None:
9897
resampling_function=resampling_function_sum,
9998
)
10099

101-
timestamp = datetime.now(tz=pytz.UTC)
100+
timestamp = datetime.now(timezone.utc)
102101

103102
value3 = 100.0
104103
value1 = 5.0
@@ -132,7 +131,7 @@ def test_component_metric_group_resampler() -> None:
132131
resampler.add_time_series(time_series_id=time_series_id_1)
133132
resampler.add_time_series(time_series_id=time_series_id_2)
134133

135-
timestamp = datetime.now(tz=pytz.UTC)
134+
timestamp = datetime.now(timezone.utc)
136135

137136
value1 = 5.0
138137
value2 = 15.0

0 commit comments

Comments
 (0)