34 lines
1.6 KiB
JavaScript
34 lines
1.6 KiB
JavaScript
const adjustForTimezone = (utcMinutes) => {
|
|
const offsetInMinutes = -120; // Simulate UTC+2
|
|
return utcMinutes - offsetInMinutes;
|
|
};
|
|
|
|
const formatMinutesAfter = (minutes, reference = "04:00") => {
|
|
const [refHours, refMinutes] = reference.split(":").map(Number);
|
|
const totalRefMinutes = refHours * 60 + refMinutes;
|
|
const targetMinutes = totalRefMinutes + minutes;
|
|
const normalizedMinutes = ((targetMinutes % 1440) + 1440) % 1440;
|
|
const hours = Math.floor(normalizedMinutes / 60);
|
|
const mins = normalizedMinutes % 60;
|
|
return `${String(hours).padStart(2, "0")}:${String(mins).padStart(2, "0")}`;
|
|
};
|
|
|
|
// Case 1: Backend sends offset from 04:00 UTC (-5 minutes)
|
|
const backendOffset = -5;
|
|
const adjusted = adjustForTimezone(backendOffset);
|
|
console.log("Adjusted offset:", adjusted); // Should be 115
|
|
console.log("Formatted time:", formatMinutesAfter(adjusted, "04:00")); // Should be 05:55
|
|
|
|
// Case 2: Backend sends minutes from UTC midnight (235 minutes)
|
|
const backendMidnightMins = 235;
|
|
const adjusted2 = adjustForTimezone(backendMidnightMins);
|
|
console.log("Adjusted midnight mins:", adjusted2); // Should be 355
|
|
// If we use it as an offset from reference:
|
|
console.log("Formatted time (as offset):", formatMinutesAfter(adjusted2, "04:00")); // Should be 09:55 (WRONG if it was supposed to be 05:55)
|
|
|
|
// If we want the offset from reference for Case 2:
|
|
const refMins = 4 * 60;
|
|
const correctOffset = adjusted2 - refMins; // 355 - 240 = 115
|
|
console.log("Corrected offset (from midnight):", correctOffset); // 115
|
|
console.log("Formatted time (corrected):", formatMinutesAfter(correctOffset, "04:00")); // 05:55
|