fix: cron per local time for utc.

This commit is contained in:
ItzNotABug
2024-09-11 14:49:01 +05:30
parent 5c4727243d
commit bf3e945d75
2 changed files with 19 additions and 6 deletions
@@ -5,7 +5,6 @@
FormList,
InputRadio,
InputSelect,
InputSelectCheckbox,
InputText,
InputTime,
Label
@@ -59,7 +58,14 @@
let backupFrequency: string = 'end';
const generateCronExpression = () => {
const [hour, minute] = selectedTime.split(':');
const [localHour, localMinute] = selectedTime.split(':');
// Convert local time to UTC
const now = new Date();
now.setHours(parseInt(localHour), parseInt(localMinute), 0);
const utcHour = now.getUTCHours();
const utcMinute = now.getUTCMinutes();
let dayOfWeek = '*';
let dayOfMonth = '*';
@@ -77,7 +83,7 @@
return `${new Date().getMinutes()} * * * *`;
}
return `${minute} ${hour} ${dayOfMonth} * ${dayOfWeek}`;
return `${utcMinute} ${utcHour} ${dayOfMonth} * ${dayOfWeek}`;
};
// TODO: reset values on modal close.
@@ -55,7 +55,7 @@
return `Retained for ${retention} day${retention > 1 ? 's' : ''}`;
};
function getPolicyDescription(cron: string): string {
const getPolicyDescription = (cron: string): string => {
const cronParts = cron.split(' ');
// A standard cron has 5 parts: minute, hour, day of month, month, day of week
@@ -88,7 +88,14 @@
) {
return 'Runs daily';
}
}
};
// Switches to UTC for non-hourly,
// keeps local time for hourly schedules.
const isHourlySchedule = (cron: string): boolean => {
const [_, hour, dayOfMonth, month, dayOfWeek] = cron.split(' ');
return hour === '*' && dayOfMonth === '*' && month === '*' && dayOfWeek === '*';
};
</script>
<Card
@@ -167,7 +174,7 @@
<div
class="u-flex u-gap-4 u-cross-center policy-item-subtitles darker-neutral-color">
{toLocaleDateTime(
parseExpression(policy.schedule, { utc: false }).next().toString()
parseExpression(policy.schedule, { utc: !isHourlySchedule(policy.schedule) }).next().toString()
)}
</div>
</div>