Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/snaps-controllers/src/cronjob/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ describe('getExecutionDate', () => {
expect(() => getExecutionDate('100 * * * * *')).toThrow(
'Unable to parse "100 * * * * *" as ISO 8601 date, ISO 8601 duration, or cron expression.',
);

expect(() => getExecutionDate('P1000000Y')).toThrow(
'Unable to parse "P1000000Y" as ISO 8601 date, ISO 8601 duration, or cron expression.',
);
});

it('throws an error for dates in the past', () => {
Expand Down
16 changes: 9 additions & 7 deletions packages/snaps-controllers/src/cronjob/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ export function getExecutionDate(schedule: string) {
});
}

const duration = Duration.fromISO(schedule);
if (duration.isValid) {
// This ensures the duration is at least 1 second.
const validatedDuration = getDuration(duration);
return DateTime.now().toUTC().plus(validatedDuration).toISO();
}

try {
const duration = Duration.fromISO(schedule);
if (duration.isValid) {
// This ensures the duration is at least 1 second.
const validatedDuration = getDuration(duration);
const offsetDate = DateTime.now().toUTC().plus(validatedDuration);

@MajorLift MajorLift Sep 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about triggering a type error as well with something like:

 import { DateTime, Duration } from 'luxon';
+import type { DateTimeMaybeValid } from 'luxon';

+/**
+ * Add a duration to the current time. The sum may fall outside the
+ * representable date range, so the result may be invalid.
+ *
+ * @param duration - The duration to add.
+ * @returns The resulting date, which may be invalid.
+ */
+function getOffsetDate(duration: Duration<true>): DateTimeMaybeValid {
+  return DateTime.now().toUTC().plus(duration);
+}
+
 /**
  * Get the next execution date from a schedule, which should be either:

-      const offsetDate = DateTime.now().toUTC().plus(validatedDuration);
+      const offsetDate = getOffsetDate(validatedDuration);

assert(offsetDate.isValid);
return offsetDate.toISO();
}

const parsed = parseExpression(schedule, { utc: true });
const next = parsed.next();
const nextDate = DateTime.fromJSDate(next.toDate());
Expand Down
Loading