Skip to content

Commit

Permalink
Fix scheduler date input and display (#1472)
Browse files Browse the repository at this point in the history
Fixes #1255

### Changes

- Fixes incorrect time zone conversion when generating UTC schedule in
workflow.
- Fixes minute input display not prefixing single digits with `0`

Co-authored-by: emma <hi@emma.cafe>
  • Loading branch information
SuaYoo and emma-sg authored Jan 19, 2024
1 parent bf38063 commit 896c3cc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
23 changes: 11 additions & 12 deletions frontend/src/components/ui/time-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,16 @@ export class TimeInput extends LitElement {
value=${this.hour}
?disabled=${this.disabled}
required
@keyup=${(e: KeyboardEvent) => {
@sl-change=${async (e: Event) => {
e.stopPropagation();
const input = e.target as SlInput;
if (input.value) {
const int = +input.value.replace(/[^0-9]/g, "");
input.value = `${Math.min(12, Math.max(1, int))}`;
} else {
input.value = "12";
}
}}
@sl-change=${async (e: Event) => {
e.stopPropagation();
const input = e.target as SlInput;
await input.updateComplete;
this.hour = +input.value;
this.dispatchChange();
Expand All @@ -114,18 +112,19 @@ export class TimeInput extends LitElement {
: this.minute}
?disabled=${this.disabled}
required
@keyup=${(e: KeyboardEvent) => {
@sl-change=${async (e: Event) => {
e.stopPropagation();
const input = e.target as SlInput;
if (input.value) {
const int = +input.value.replace(/[^0-9]/g, "");
input.value = `${Math.min(59, Math.max(0, int))}`;
const int = Math.min(
59,
Math.max(0, +input.value.replace(/[^0-9]/g, ""))
);
input.value = int < 10 ? `0${int}` : `${int}`;
} else {
input.value = "00";
}
}}
@sl-change=${async (e: Event) => {
e.stopPropagation();
const input = e.target as SlInput;
await input.updateComplete;
this.minute = +input.value;
this.dispatchChange();
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/org/workflow-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,7 @@ https://archiveweb.page/images/${"logo.svg"}`}
label=${msg("User Agent")}
autocomplete="off"
placeholder=${msg("Default")}
value=${this.formState.userAgent}
value=${this.formState.userAgent || ""}
>
</sl-input>
`)}
Expand Down
26 changes: 15 additions & 11 deletions frontend/src/utils/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ export type ScheduleInterval = "daily" | "weekly" | "monthly";
/**
* Parse interval from cron expression
**/
export function getScheduleInterval(
schedule: string
): "daily" | "weekly" | "monthly" {
const [_minute, _hour, dayofMonth, _month, dayOfWeek] = schedule.split(" ");
if (dayofMonth === "*") {
export function getScheduleInterval(schedule: string): ScheduleInterval {
const [_minute, _hour, dayOfMonth, _month, dayOfWeek] = schedule.split(" ");
if (dayOfMonth === "*") {
if (dayOfWeek === "*") {
return "daily";
}
Expand Down Expand Up @@ -63,7 +61,7 @@ export function humanizeNextDate(
export function humanizeSchedule(
schedule: string,
options: { length?: "short" } = {},
numberFormatter: any = numberUtils.numberFormatter
numberFormatter = numberUtils.numberFormatter
): string {
const interval = getScheduleInterval(schedule);
const parsed = parseCron(schedule);
Expand All @@ -77,7 +75,7 @@ export function humanizeSchedule(
weekday: "long",
});

let intervalMsg: any = "";
let intervalMsg = "";

if (options.length === "short") {
switch (interval) {
Expand Down Expand Up @@ -123,7 +121,7 @@ export function humanizeSchedule(
break;
case "monthly":
intervalMsg = msg(
str`On day ${days[0]} of the month at ${formattedTime}`
str`On day ${nextDate.getDate()} of the month at ${formattedTime}`
);
break;
default:
Expand Down Expand Up @@ -168,9 +166,15 @@ export function getUTCSchedule({

localDate.setHours(+hour + periodOffset);
localDate.setMinutes(+minute);
const date =
interval === "monthly" ? dayOfMonth || localDate.getUTCDate() : "*";
const day = interval === "weekly" ? dayOfWeek || localDate.getUTCDay() : "*";

if (interval === "monthly" && dayOfMonth) {
localDate.setDate(dayOfMonth);
} else if (interval == "weekly" && dayOfWeek) {
localDate.setDate(localDate.getDate() + dayOfWeek - localDate.getDay());
}

const date = interval === "monthly" ? localDate.getUTCDate() : "*";
const day = interval === "weekly" ? localDate.getUTCDay() : "*";
const month = "*";

const schedule = `${localDate.getUTCMinutes()} ${localDate.getUTCHours()} ${date} ${month} ${day}`;
Expand Down

0 comments on commit 896c3cc

Please sign in to comment.