From 12c9e4428e2d801d513133ad6cec6cb8b763f06d Mon Sep 17 00:00:00 2001 From: Dominique Lasserre Date: Mon, 7 Oct 2024 22:38:14 +0200 Subject: [PATCH] Replace pytz with stdlib zoneinfo/timezone, Bump python to 3.10 * Timezone support comes with the standard lib since 3.9 * Bump to 3.10 because typing.TypeGuard --- NOTICE | 1 - pyproject.toml | 2 +- src/akkudoktoreos/class_sommerzeit.py | 15 ++++++--------- src/akkudoktoreos/class_strompreis.py | 20 ++++++-------------- 4 files changed, 13 insertions(+), 25 deletions(-) diff --git a/NOTICE b/NOTICE index d95e2f1..eb9c1f4 100644 --- a/NOTICE +++ b/NOTICE @@ -20,7 +20,6 @@ The following is a list of licensors and other acknowledgements for third-party - Flask, licensed under the BSD License, see https://flask.palletsprojects.com/ - NumPy, licensed under the BSD License, see https://numpy.org/ - Requests, licensed under the Apache License 2.0, see https://requests.readthedocs.io/ -- pytz, licensed under the MIT License, see https://pythonhosted.org/pytz/ - matplotlib, licensed under the matplotlib License (a variant of the Python Software Foundation License), see https://matplotlib.org/ - DEAP, licensed under the GNU Lesser General Public License v3.0, see https://deap.readthedocs.io/ - SciPy, licensed under the BSD License, see https://scipy.org/ diff --git a/pyproject.toml b/pyproject.toml index 3183cb4..ed916ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ authors = [ description = "This project provides a comprehensive solution for simulating and optimizing an energy system based on renewable energy sources. With a focus on photovoltaic (PV) systems, battery storage (batteries), load management (consumer requirements), heat pumps, electric vehicles, and consideration of electricity price data, this system enables forecasting and optimization of energy flow and costs over a specified period." readme = "README.md" license = {file = "LICENSE"} -requires-python = ">=3.8" +requires-python = ">=3.10" classifiers = [ "Development Status :: 3 - Alpha", "Programming Language :: Python :: 3", diff --git a/src/akkudoktoreos/class_sommerzeit.py b/src/akkudoktoreos/class_sommerzeit.py index 4e2a9e8..5ea17d8 100644 --- a/src/akkudoktoreos/class_sommerzeit.py +++ b/src/akkudoktoreos/class_sommerzeit.py @@ -1,21 +1,18 @@ import datetime - -import pytz +import zoneinfo -def ist_dst_wechsel(tag, timezone="Europe/Berlin"): +def ist_dst_wechsel(tag: datetime.datetime, timezone="Europe/Berlin") -> bool: """Checks if Daylight Saving Time (DST) starts or ends on a given day.""" - tz = pytz.timezone(timezone) + tz = zoneinfo.ZoneInfo(timezone) # Get the current day and the next day current_day = datetime.datetime(tag.year, tag.month, tag.day) next_day = current_day + datetime.timedelta(days=1) - # Localize the days in the given timezone - current_day_localized = tz.localize(current_day, is_dst=None) - next_day_localized = tz.localize(next_day, is_dst=None) - # Check if the UTC offsets are different (indicating a DST change) - dst_change = current_day_localized.dst() != next_day_localized.dst() + dst_change = ( + current_day.replace(tzinfo=tz).dst() != next_day.replace(tzinfo=tz).dst() + ) return dst_change diff --git a/src/akkudoktoreos/class_strompreis.py b/src/akkudoktoreos/class_strompreis.py index bd540dc..fad13b3 100644 --- a/src/akkudoktoreos/class_strompreis.py +++ b/src/akkudoktoreos/class_strompreis.py @@ -1,20 +1,12 @@ import hashlib import json import os -from datetime import datetime, timedelta +import zoneinfo +from datetime import datetime, timedelta, timezone import numpy as np -import pytz import requests -# Example: Converting a UTC timestamp to local time -utc_time = datetime.strptime("2024-03-28T01:00:00.000Z", "%Y-%m-%dT%H:%M:%S.%fZ") -utc_time = utc_time.replace(tzinfo=pytz.utc) - -# Replace 'Europe/Berlin' with your own timezone -local_time = utc_time.astimezone(pytz.timezone("Europe/Berlin")) -print(local_time) - def repeat_to_shape(array, target_shape): # Check if the array fits the target shape @@ -116,13 +108,13 @@ class HourlyElectricityPriceForecast: print(start_date_str) print(end_date_str) start_date_utc = datetime.strptime(start_date_str, "%Y-%m-%d").replace( - tzinfo=pytz.utc + tzinfo=timezone.utc ) end_date_utc = datetime.strptime(end_date_str, "%Y-%m-%d").replace( - tzinfo=pytz.utc + tzinfo=timezone.utc ) - start_date = start_date_utc.astimezone(pytz.timezone("Europe/Berlin")) - end_date = end_date_utc.astimezone(pytz.timezone("Europe/Berlin")) + start_date = start_date_utc.astimezone(zoneinfo.ZoneInfo("Europe/Berlin")) + end_date = end_date_utc.astimezone(zoneinfo.ZoneInfo("Europe/Berlin")) price_list = []