From 9c3a1953fcf1091d6f9550c6bcee70ac558c39e8 Mon Sep 17 00:00:00 2001 From: Felix Claessen <30658763+Flix6x@users.noreply.github.com> Date: Fri, 23 Jun 2023 12:33:50 +0200 Subject: [PATCH] Backport PR #744: Fix/report offsets in local time (#744) Switch to interpreting the report offsets in the sensor's timezone. * fix: do not overwrite default timezone with UTC Signed-off-by: F.N. Claessen * refactor: modernize type annotations Signed-off-by: F.N. Claessen * fix: default timezone is the sensor timezone where we save the report Signed-off-by: F.N. Claessen * refactor: flake8 helped me see this Signed-off-by: F.N. Claessen * docs: changelog entry Signed-off-by: F.N. Claessen * docs: CLI changelog entry Signed-off-by: F.N. Claessen --------- Signed-off-by: F.N. Claessen Signed-off-by: Felix Claessen <30658763+Flix6x@users.noreply.github.com> --- documentation/changelog.rst | 1 + documentation/cli/change_log.rst | 1 + flexmeasures/cli/data_add.py | 31 +++++++++++++++---------------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/documentation/changelog.rst b/documentation/changelog.rst index f56f388d4..ec462bc8e 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -12,6 +12,7 @@ Bugfixes * Relax constraint validation of `StorageScheduler` to accommodate violations caused by floating point precision [see `PR #731 `_] * Avoid saving any :abbr:`NaN (not a number)` values to the database, when calling ``flexmeasures add report`` [see `PR #735 `_] * Fix browser console error when loading asset or sensor page with only a single data point [see `PR #732 `_] +* Fix defaults for the ``--start-offset`` and ``--end-offset` options to ``flexmeasures add report``, which weren't being interpreted in the local timezone of the reporting sensor [see `PR #744 `_] * Relax constraint for overlaying plot traces for sensors with various resolutions, making it possible to show e.g. two price sensors in one chart, where one of them records hourly prices and the other records quarter-hourly prices [see `PR #743 `_] diff --git a/documentation/cli/change_log.rst b/documentation/cli/change_log.rst index 3a8b9f2da..d85767457 100644 --- a/documentation/cli/change_log.rst +++ b/documentation/cli/change_log.rst @@ -8,6 +8,7 @@ since v0.14.1 | June XX, 2023 ================================= * Avoid saving any :abbr:`NaN (not a number)` values to the database, when calling ``flexmeasures add report``. +* Fix defaults for the ``--start-offset`` and ``--end-offset` options to ``flexmeasures add report``, which weren't being interpreted in the local timezone of the reporting sensor. since v0.14.0 | June 15, 2023 ================================= diff --git a/flexmeasures/cli/data_add.py b/flexmeasures/cli/data_add.py index d51374ae7..1cad0504d 100755 --- a/flexmeasures/cli/data_add.py +++ b/flexmeasures/cli/data_add.py @@ -5,7 +5,7 @@ from __future__ import annotations from datetime import datetime, timedelta -from typing import Optional, Type +from typing import Type import json from pathlib import Path from io import TextIOBase @@ -66,7 +66,7 @@ ) from flexmeasures.data.services.utils import get_or_create_model from flexmeasures.utils import flexmeasures_inflection -from flexmeasures.utils.time_utils import server_now, get_timezone, apply_offset_chain +from flexmeasures.utils.time_utils import server_now, apply_offset_chain from flexmeasures.utils.unit_utils import convert_units, ur from flexmeasures.data.utils import save_to_db from flexmeasures.data.models.reporting import Reporter @@ -1230,8 +1230,7 @@ def add_schedule_for_storage( "--timezone", "timezone", required=False, - default="UTC", - help="Timezone as string, e.g. 'UTC' or 'Europe/Amsterdam' (defaults to FLEXMEASURES_TIMEZONE config setting)", + help="Timezone as string, e.g. 'UTC' or 'Europe/Amsterdam' (defaults to the timezone of the sensor used to save the report).", ) @click.option( "--dry-run", @@ -1243,26 +1242,26 @@ def add_report( # noqa: C901 reporter_class: str, sensor: Sensor, reporter_config: TextIOBase, - start: Optional[datetime] = None, - end: Optional[datetime] = None, - start_offset: Optional[str] = None, - end_offset: Optional[str] = None, - resolution: Optional[timedelta] = None, - output_file: Optional[Path] = None, + start: datetime | None = None, + end: datetime | None = None, + start_offset: str | None = None, + end_offset: str | None = None, + resolution: timedelta | None = None, + output_file: Path | None = None, dry_run: bool = False, - timezone: str | pytz.BaseTzInfo = get_timezone(), + timezone: str | None = None, ): """ Create a new report using the Reporter class and save the results to the database or export them as CSV or Excel file. """ - # parse timezone into a BaseTzInfo object - if isinstance(timezone, str): + # compute now in the timezone local to the output sensor + if timezone is not None: check_timezone(timezone) - timezone = pytz.timezone(zone=timezone) - - now = timezone.localize(datetime.now()) + now = pytz.timezone( + zone=timezone if timezone is not None else sensor.timezone + ).localize(datetime.now()) # apply offsets, if provided if start_offset is not None: