fix time shift

This commit is contained in:
lelo 2025-04-01 21:44:12 +00:00
parent 7c19735e24
commit fa8796a413

View File

@ -219,25 +219,21 @@
// For 'last24hours', the bucket is an hour in UTC (e.g., "14") // For 'last24hours', the bucket is an hour in UTC (e.g., "14")
const utcHour = parseInt(item.bucket, 10); const utcHour = parseInt(item.bucket, 10);
const now = new Date(); const now = new Date();
// Create Date objects for the start and end of the hour in UTC // Use UTC components to get the correct UTC date
const utcStart = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), utcHour)); const utcStart = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), utcHour));
const utcEnd = new Date(Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), utcHour + 1)); const utcEnd = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), utcHour + 1));
// Convert to local time strings, e.g., "16:00" // Convert to local time strings
const localStart = utcStart.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); const localStart = utcStart.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const localEnd = utcEnd.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); const localEnd = utcEnd.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
return `${localStart} - ${localEnd}`; return `${localStart} - ${localEnd}`;
} else if (timeframe === '7days' || timeframe === '30days') { } else if (timeframe === '7days' || timeframe === '30days') {
// For these timeframes, the bucket is a date in the format "YYYY-MM-DD"
const utcDate = new Date(item.bucket + 'T00:00:00Z'); const utcDate = new Date(item.bucket + 'T00:00:00Z');
return utcDate.toLocaleDateString(); // Adjust formatting as needed return utcDate.toLocaleDateString();
} else if (timeframe === '365days') { } else if (timeframe === '365days') {
// For this timeframe, the bucket is a month in the format "YYYY-MM"
const [year, month] = item.bucket.split('-'); const [year, month] = item.bucket.split('-');
const dateObj = new Date(year, month - 1, 1); const dateObj = new Date(year, month - 1, 1);
// Format to something like "Mar 2025"
return dateObj.toLocaleString([], { month: 'short', year: 'numeric' }); return dateObj.toLocaleString([], { month: 'short', year: 'numeric' });
} else { } else {
// Fallback: use the bucket value as-is
return item.bucket; return item.bucket;
} }
}); });