mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 11:05:23 +02:00
95 lines
3.2 KiB
TypeScript
95 lines
3.2 KiB
TypeScript
// Verificar fechas ISO para el experimento BODYNESS
|
|
|
|
function getISOWeek(date: Date): { year: number; week: number } {
|
|
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
|
|
const dayNum = d.getUTCDay() || 7;
|
|
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
|
|
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
|
|
const weekNum = Math.ceil((((d.getTime() - yearStart.getTime()) / 86400000) + 1) / 7);
|
|
return { year: d.getUTCFullYear(), week: weekNum };
|
|
}
|
|
|
|
function getISOWeekMonday(year: number, week: number): number {
|
|
const jan4 = new Date(Date.UTC(year, 0, 4));
|
|
const dayOfWeek = jan4.getUTCDay() || 7;
|
|
const week1Monday = new Date(Date.UTC(year, 0, 4 - (dayOfWeek - 1)));
|
|
return week1Monday.getTime() + (week - 1) * 7 * 86400000;
|
|
}
|
|
|
|
function getWeeksInYear(year: number): number {
|
|
const dec28 = new Date(Date.UTC(year, 11, 28));
|
|
const iso = getISOWeek(dec28);
|
|
return iso.week;
|
|
}
|
|
|
|
function calculateISOWeekRange(startDate: Date, endDate: Date): {
|
|
numWeeks: number;
|
|
expandedStartTs: number;
|
|
expandedEndTs: number;
|
|
weekKeys: string[];
|
|
} {
|
|
const startISO = getISOWeek(startDate);
|
|
const endISO = getISOWeek(endDate);
|
|
|
|
const weekKeys: string[] = [];
|
|
let currentYear = startISO.year;
|
|
let currentWeek = startISO.week;
|
|
|
|
while (true) {
|
|
const weekKey = `${currentYear}-W${String(currentWeek).padStart(2, '0')}`;
|
|
weekKeys.push(weekKey);
|
|
|
|
if (currentYear === endISO.year && currentWeek === endISO.week) {
|
|
break;
|
|
}
|
|
|
|
currentWeek++;
|
|
const weeksInYear = getWeeksInYear(currentYear);
|
|
if (currentWeek > weeksInYear) {
|
|
currentWeek = 1;
|
|
currentYear++;
|
|
}
|
|
}
|
|
|
|
const expandedStartTs = getISOWeekMonday(startISO.year, startISO.week);
|
|
const expandedEndTs = getISOWeekMonday(endISO.year, endISO.week) + 7 * 86400000;
|
|
|
|
return {
|
|
numWeeks: weekKeys.length,
|
|
expandedStartTs,
|
|
expandedEndTs,
|
|
weekKeys,
|
|
};
|
|
}
|
|
|
|
// Experimento: 22 enero - 4 febrero 2026
|
|
const startDate = new Date('2026-01-22');
|
|
const endDate = new Date('2026-02-04');
|
|
|
|
console.log('=== Experimento BODYNESS ES ===');
|
|
console.log('Start:', startDate.toISOString().split('T')[0], '(day', startDate.getDay(), ')');
|
|
console.log('End:', endDate.toISOString().split('T')[0], '(day', endDate.getDay(), ')');
|
|
console.log('');
|
|
|
|
const startISO = getISOWeek(startDate);
|
|
const endISO = getISOWeek(endDate);
|
|
console.log('Start ISO:', `W${startISO.week}-${startISO.year}`);
|
|
console.log('End ISO:', `W${endISO.week}-${endISO.year}`);
|
|
console.log('');
|
|
|
|
const isoRange = calculateISOWeekRange(startDate, endDate);
|
|
console.log('ISO Week Range:');
|
|
console.log(' numWeeks:', isoRange.numWeeks);
|
|
console.log(' weekKeys:', isoRange.weekKeys);
|
|
console.log(' expandedStart:', new Date(isoRange.expandedStartTs).toISOString().split('T')[0]);
|
|
console.log(' expandedEnd:', new Date(isoRange.expandedEndTs).toISOString().split('T')[0]);
|
|
console.log('');
|
|
|
|
// Verificar cada semana
|
|
isoRange.weekKeys.forEach(week => {
|
|
const [year, weekNum] = week.split('-W');
|
|
const monday = getISOWeekMonday(Number(year), Number(weekNum));
|
|
const sunday = monday + 6 * 86400000 + 86399999;
|
|
console.log(` ${week}: ${new Date(monday).toISOString().split('T')[0]} (Mon) → ${new Date(sunday).toISOString().split('T')[0]} (Sun)`);
|
|
});
|