diff --git a/dmapp/dmweb/get_period_times.py b/dmapp/dmweb/get_period_times.py index 868d94f..554de03 100644 --- a/dmapp/dmweb/get_period_times.py +++ b/dmapp/dmweb/get_period_times.py @@ -276,10 +276,19 @@ def get_period_totals(start, end, task=None): return [{"ws": "No Data", "total": ""}] bfirst = aux_results[0]["before_first"] + start_delta = 0 if bfirst: bfdate = bfirst["date"].replace(tzinfo=utctz) - start_delta = round((start - bfdate.astimezone(timezone)).total_seconds()) + time_since_bfirst = round((start - bfdate.astimezone(timezone)).total_seconds()) + + # Only apply start_delta if the before_first switch actually crosses into the period + # If time_since_bfirst > bfirst["delta"], the switch ended before the period started + if time_since_bfirst <= bfirst["delta"]: + start_delta = time_since_bfirst + logger.debug(f"start_delta: {start_delta}s (bfirst crosses into period)") + else: + logger.debug(f"start_delta: 0s (bfirst ended before period start: {time_since_bfirst}s > {bfirst['delta']}s)") ldoc = aux_results[0]["last_doc"] lastdate = ldoc["date"].replace(tzinfo=utctz)