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 initOverTime's calculation of last slot timestamp #1312

Merged
merged 1 commit into from
Apr 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/overTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,22 @@ void initOverTime(void)
// Get current timestamp
time_t now = time(NULL);

// The last timestamp (overTime[149]) should be the last interval of this hour
// If the current time is 09:35, the last interval is 09:50 - 10:00 (centered at 09:55)
time_t timestamp = now - now % 3600 + 3600 - (OVERTIME_INTERVAL / 2);
// Get the centered timestamp of the current timestamp
time_t first_slot_ts = now - now % OVERTIME_INTERVAL + (OVERTIME_INTERVAL / 2);
time_t last_slot_ts = first_slot_ts + (OVERTIME_SLOTS-1) * OVERTIME_INTERVAL;
Comment on lines +74 to +75
Copy link
Member

@DL6ER DL6ER Apr 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic in this PR is broken, unfortunately, I didn't see this before.

The issue here is, basically, that first_slot_ts should be the one 24 hours in the past, however, it is a timestamp centered at the current timestamp (as the comment correctly says). The last_slot_ts is then 24 hours in the future which is wrong.

This wasn't an issue in the code before the change as the old code was going backwards from now. The new code tries to go forward but start at now causing the history to incorrectly live in the future rather than in the past.


if(config.debug & DEBUG_OVERTIME)
logg("initOverTime(): Initializing %i slots from %llu to %llu",
OVERTIME_SLOTS,
(long long)timestamp-OVERTIME_SLOTS*OVERTIME_INTERVAL,
(long long)timestamp);
(long long)first_slot_ts,
(long long)last_slot_ts);

// Iterate over overTime
for(int i = OVERTIME_SLOTS-1; i >= 0 ; i--)
for(int i = 0; i < OVERTIME_SLOTS; i++)
{
// Initialize onerTime slot
initSlot(i, timestamp);
// Prepare for next iteration
timestamp -= OVERTIME_INTERVAL;
time_t this_slot_ts = first_slot_ts + OVERTIME_INTERVAL * i;
// Initialize overTime slot
initSlot(i, this_slot_ts);
}
}

Expand Down