Skip to content

Commit

Permalink
Backport PR #744: Fix/report offsets in local time (#744)
Browse files Browse the repository at this point in the history
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 <felix@seita.nl>

* refactor: modernize type annotations

Signed-off-by: F.N. Claessen <felix@seita.nl>

* fix: default timezone is the sensor timezone where we save the report

Signed-off-by: F.N. Claessen <felix@seita.nl>

* refactor: flake8 helped me see this

Signed-off-by: F.N. Claessen <felix@seita.nl>

* docs: changelog entry

Signed-off-by: F.N. Claessen <felix@seita.nl>

* docs: CLI changelog entry

Signed-off-by: F.N. Claessen <felix@seita.nl>

---------

Signed-off-by: F.N. Claessen <felix@seita.nl>
Signed-off-by: Felix Claessen <30658763+Flix6x@users.noreply.github.com>
  • Loading branch information
Flix6x committed Jun 26, 2023
1 parent 38715bc commit 9c3a195
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -12,6 +12,7 @@ Bugfixes
* Relax constraint validation of `StorageScheduler` to accommodate violations caused by floating point precision [see `PR #731 <https://www.github.com/FlexMeasures/flexmeasures/pull/731>`_]
* Avoid saving any :abbr:`NaN (not a number)` values to the database, when calling ``flexmeasures add report`` [see `PR #735 <https://www.github.com/FlexMeasures/flexmeasures/pull/735>`_]
* Fix browser console error when loading asset or sensor page with only a single data point [see `PR #732 <https://www.github.com/FlexMeasures/flexmeasures/pull/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 <https://www.github.com/FlexMeasures/flexmeasures/pull/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 <https://www.github.com/FlexMeasures/flexmeasures/pull/743>`_]


Expand Down
1 change: 1 addition & 0 deletions documentation/cli/change_log.rst
Expand Up @@ -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
=================================
Expand Down
31 changes: 15 additions & 16 deletions flexmeasures/cli/data_add.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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:
Expand Down

0 comments on commit 9c3a195

Please sign in to comment.