Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/report offsets in local time #744

Merged
merged 8 commits into from Jun 23, 2023
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