MCPcopy Create free account
hub / github.com/Effect-TS/effect / stepCron

Function stepCron

packages/effect/src/Cron.ts:497–667  ·  view source on GitHub ↗
(cron: Cron, startFrom: DateTime.DateTime.Input | undefined, direction: "next" | "prev")

Source from the content-addressed store, hash-verified

495
496/** @internal */
497const stepCron = (cron: Cron, startFrom: DateTime.DateTime.Input | undefined, direction: "next" | "prev"): Date => {
498 const tz = Option.getOrUndefined(cron.tz)
499 const zoned = dateTime.unsafeMakeZoned(startFrom ?? new Date(), {
500 timeZone: tz
501 })
502
503 const prev = direction === "prev"
504 const tick = prev ? -1 : 1
505 const table = cron[direction]
506 const boundary = prev ? cron.last : cron.first
507
508 const needsStep = prev
509 ? (next: number, current: number) => next < current
510 : (next: number, current: number) => next > current
511
512 const utc = tz !== undefined && dateTime.isTimeZoneNamed(tz) && tz.id === "UTC"
513 const adjustDst = utc ? constVoid : (current: Date) => {
514 const adjusted = dateTime.unsafeMakeZoned(current, {
515 timeZone: zoned.zone,
516 adjustForTimeZone: true,
517 disambiguation: prev ? "later" : undefined
518 }).pipe(dateTime.toDate)
519
520 const drift = current.getTime() - adjusted.getTime()
521 if (prev ? drift !== 0 : drift > 0) {
522 current.setTime(adjusted.getTime())
523 }
524 }
525
526 const result = dateTime.mutate(zoned, (current) => {
527 current.setUTCSeconds(current.getUTCSeconds() + tick, 0)
528
529 for (let i = 0; i < 10_000; i++) {
530 if (cron.seconds.size !== 0) {
531 const currentSecond = current.getUTCSeconds()
532 const nextSecond = table.second[currentSecond]
533 if (nextSecond === undefined) {
534 current.setUTCMinutes(current.getUTCMinutes() + tick, boundary.second)
535 adjustDst(current)
536 continue
537 }
538 if (needsStep(nextSecond, currentSecond)) {
539 current.setUTCSeconds(nextSecond)
540 adjustDst(current)
541 continue
542 }
543 }
544
545 if (cron.minutes.size !== 0) {
546 const currentMinute = current.getUTCMinutes()
547 const nextMinute = table.minute[currentMinute]
548 if (nextMinute === undefined) {
549 current.setUTCHours(current.getUTCHours() + tick, boundary.minute, boundary.second)
550 adjustDst(current)
551 continue
552 }
553 if (needsStep(nextMinute, currentMinute)) {
554 current.setUTCMinutes(nextMinute, boundary.second)

Callers 2

nextFunction · 0.85
prevFunction · 0.85

Calls 4

daysInMonthFunction · 0.85
clampBoundaryDayFunction · 0.85
pipeMethod · 0.65
setTimeMethod · 0.65

Tested by

no test coverage detected