MCPcopy Index your code
hub / github.com/simstudioai/sim / calculateNextRunTime

Function calculateNextRunTime

apps/sim/lib/workflows/schedules/utils.ts:371–477  ·  view source on GitHub ↗
(
  scheduleType: string,
  scheduleValues: ReturnType<typeof getScheduleTimeValues>
)

Source from the content-addressed store, hash-verified

369 * @returns Date object for next execution time
370 */
371export function calculateNextRunTime(
372 scheduleType: string,
373 scheduleValues: ReturnType<typeof getScheduleTimeValues>
374): Date {
375 // Get timezone (default to UTC)
376 const timezone = scheduleValues.timezone || 'UTC'
377
378 // Get the current time
379 const baseDate = new Date()
380
381 // If we have both a start date and time, use them together with timezone awareness
382 if (scheduleValues.scheduleStartAt && scheduleValues.scheduleTime) {
383 try {
384 logger.debug(
385 `Creating date with: startAt=${scheduleValues.scheduleStartAt}, time=${scheduleValues.scheduleTime}, timezone=${timezone}`
386 )
387
388 const combinedDate = createDateWithTimezone(
389 scheduleValues.scheduleStartAt,
390 scheduleValues.scheduleTime,
391 timezone
392 )
393
394 logger.debug(`Combined date result: ${combinedDate.toISOString()}`)
395
396 // If the combined date is in the future, use it as our next run time
397 if (combinedDate > baseDate) {
398 return combinedDate
399 }
400 } catch (e) {
401 logger.error('Error combining scheduled date and time:', e)
402 }
403 }
404 // If only scheduleStartAt is set (without scheduleTime), parse it directly
405 else if (scheduleValues.scheduleStartAt) {
406 try {
407 // Check if the date string already includes time information
408 const startAtStr = scheduleValues.scheduleStartAt
409 const hasTimeComponent =
410 startAtStr.includes('T') && (startAtStr.includes(':') || startAtStr.includes('.'))
411
412 if (hasTimeComponent) {
413 // If the string already has time info, parse it directly but with timezone awareness
414 const startDate = new Date(startAtStr)
415
416 // If it's a UTC ISO string (ends with Z), use it directly
417 if (startAtStr.endsWith('Z') && timezone === 'UTC') {
418 if (startDate > baseDate) {
419 return startDate
420 }
421 } else {
422 // For non-UTC dates or when timezone isn't UTC, we need to interpret it in the specified timezone
423 // Extract time from the date string (crude but effective for ISO format)
424 const timeMatch = startAtStr.match(/T(\d{2}:\d{2})/)
425 const timeStr = timeMatch ? timeMatch[1] : '00:00'
426
427 // Use our timezone-aware function with the extracted time
428 const tzAwareDate = createDateWithTimezone(

Callers 4

utils.test.tsFile · 0.90
validateScheduleBlockFunction · 0.90
executeScheduleJobFunction · 0.90

Calls 5

toErrorFunction · 0.90
createDateWithTimezoneFunction · 0.85
generateCronExpressionFunction · 0.85
debugMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected