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

Fixed weekly report week number calculation to match ISO 8601 week definition #80

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 17 additions & 9 deletions birdcage_frontend/templates/weekly_report.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ <h3 id="report-summary">Total Detections: 0 | Unique Species: 0</h3>
const [year, week] = weekString.split('-W');

// Calculate the start of the week (Monday)
const start = new Date(year, 0, (week) * 7 + 1);
start.setDate(start.getDate() - (start.getDay() + 6) % 7);
const start = getMondayOfWeek(weekString);

// Calculate the end of the week (Sunday)
const end = new Date(start);
Expand Down Expand Up @@ -157,13 +156,19 @@ <h3 id="report-summary">Total Detections: 0 | Unique Species: 0</h3>
});
}


function getWeekNumber(date) {
const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
const dayOfYear = (date - firstDayOfYear + (86400000 /* ms per day */ * (firstDayOfYear.getDay() + 1))) / 86400000;
return Math.ceil(dayOfYear / 7);
function getWeekYear(date) {
const thursday = new Date(date);
thursday.setDate(thursday.getDate() + 3 - (thursday.getDay() + 6) % 7);
return thursday.getFullYear();
}

function getWeekNumber(date) {
const thursday = new Date(date);
thursday.setDate(thursday.getDate() + 3 - (thursday.getDay() + 6) % 7);
const firstWeek = new Date(thursday.getFullYear(), 0, 4);
return 1 + Math.round(((thursday.getTime() - firstWeek.getTime()) / 86400000 - 3 + (firstWeek.getDay() + 6) % 7) / 7);
}

document.addEventListener('DOMContentLoaded', function () {
flatpickr('#week-picker', {
weekNumbers: true,
Expand All @@ -172,7 +177,7 @@ <h3 id="report-summary">Total Detections: 0 | Unique Species: 0</h3>
onChange: function (selectedDates, dateStr, instance) {
if (selectedDates.length > 0) {
const selectedDate = selectedDates[0];
const selectedWeek = selectedDate.getFullYear() + "-W" + getWeekNumber(selectedDate);
const selectedWeek = getWeekYear(selectedDate) + "-W" + getWeekNumber(selectedDate);
instance.input.value = selectedWeek;
// Redirect to the new URL for the selected week
const newUrl = `{{ script_name }}/weekly_report/${selectedWeek}`;
Expand All @@ -185,7 +190,10 @@ <h3 id="report-summary">Total Detections: 0 | Unique Species: 0</h3>
function getMondayOfWeek(weekString) {
const [year, week] = weekString.split('-W');
const date = new Date(year, 0, (week) * 7 + 1);
date.setDate(date.getDate() - (date.getDay() + 6) % 7);
if (date.getDay() <= 4) {
date.setDate(date.getDate() - 7);
}
date.setDate(date.getDate() - date.getDay() + 1);
return date;
}

Expand Down