Skip to main content Link Search Menu Expand Document (external link)

Cron.ts overview

Since v2.0.0


Exports Grouped by Category


constructors

make

Creates a Cron instance.

Signature

declare const make: (values: {
  readonly seconds?: Iterable<number> | undefined
  readonly minutes: Iterable<number>
  readonly hours: Iterable<number>
  readonly days: Iterable<number>
  readonly months: Iterable<number>
  readonly weekdays: Iterable<number>
  readonly tz?: DateTime.TimeZone | undefined
}) => Cron

Source

Since v2.0.0

parse

Parses a cron expression into a Cron instance.

Example

import * as assert from "node:assert"
import { Cron, Either } from "effect"

// At 04:00 on every day-of-month from 8 through 14.
assert.deepStrictEqual(
  Cron.parse("0 0 4 8-14 * *"),
  Either.right(
    Cron.make({
      seconds: [0],
      minutes: [0],
      hours: [4],
      days: [8, 9, 10, 11, 12, 13, 14],
      months: [],
      weekdays: []
    })
  )
)

Signature

declare const parse: (cron: string, tz?: DateTime.TimeZone | string) => Either.Either<Cron, ParseError>

Source

Since v2.0.0

unsafeParse

Parses a cron expression into a Cron instance.

Details

This function takes a cron expression as a string and attempts to parse it into a Cron instance. If the expression is valid, the resulting Cron instance will represent the schedule defined by the cron expression.

If the expression is invalid, the function throws a ParseError.

You can optionally provide a time zone (tz) to interpret the cron expression in a specific time zone. If no time zone is provided, the cron expression will use the default time zone.

Example

import { Cron } from "effect"

// At 04:00 on every day-of-month from 8 through 14.
console.log(Cron.unsafeParse("0 4 8-14 * *"))
// Output:
// {
//   _id: 'Cron',
//   tz: { _id: 'Option', _tag: 'None' },
//   seconds: [ 0 ],
//   minutes: [ 0 ],
//   hours: [ 4 ],
//   days: [
//      8,  9, 10, 11,
//     12, 13, 14
//   ],
//   months: [],
//   weekdays: []
// }

Signature

declare const unsafeParse: (cron: string, tz?: DateTime.TimeZone | string) => Cron

Source

Since v2.0.0

guards

isCron

Checks if a given value is a Cron instance.

Signature

declare const isCron: (u: unknown) => u is Cron

Source

Since v2.0.0

isParseError

Returns true if the specified value is an ParseError, false otherwise.

Signature

declare const isParseError: (u: unknown) => u is ParseError

Source

Since v2.0.0

instances

Equivalence

Signature

declare const Equivalence: equivalence.Equivalence<Cron>

Source

Since v2.0.0

models

Cron (interface)

Signature

export interface Cron extends Pipeable, Equal.Equal, Inspectable {
  readonly [TypeId]: TypeId
  readonly tz: Option.Option<DateTime.TimeZone>
  readonly seconds: ReadonlySet<number>
  readonly minutes: ReadonlySet<number>
  readonly hours: ReadonlySet<number>
  readonly days: ReadonlySet<number>
  readonly months: ReadonlySet<number>
  readonly weekdays: ReadonlySet<number>
  /** @internal */
  readonly first: {
    readonly second: number
    readonly minute: number
    readonly hour: number
    readonly day: number
    readonly month: number
    readonly weekday: number
  }
  /** @internal */
  readonly next: {
    readonly second: ReadonlyArray<number | undefined>
    readonly minute: ReadonlyArray<number | undefined>
    readonly hour: ReadonlyArray<number | undefined>
    readonly day: ReadonlyArray<number | undefined>
    readonly month: ReadonlyArray<number | undefined>
    readonly weekday: ReadonlyArray<number | undefined>
  }
}

Source

Since v2.0.0

ParseError (class)

Represents a checked exception which occurs when decoding fails.

Signature

declare class ParseError

Source

Since v2.0.0

[ParseErrorTypeId] (property)

Signature

readonly [ParseErrorTypeId]: symbol

Source

Since v2.0.0

predicates

equals

Checks if two Crons are equal.

Signature

declare const equals: { (that: Cron): (self: Cron) => boolean; (self: Cron, that: Cron): boolean }

Source

Since v2.0.0

symbol

ParseErrorTypeId

Signature

declare const ParseErrorTypeId: unique symbol

Source

Since v2.0.0

TypeId (type alias)

Signature

type TypeId = typeof TypeId

Source

Since v2.0.0

symbols

ParseErrorTypeId (type alias)

Signature

type ParseErrorTypeId = typeof ParseErrorTypeId

Source

Since v2.0.0

TypeId

Signature

declare const TypeId: unique symbol

Source

Since v2.0.0

utils

match

Checks if a given Date falls within an active Cron time window.

Throws

IllegalArgumentException if the given DateTime.Input is invalid.

Example

import * as assert from "node:assert"
import { Cron, Either } from "effect"

const cron = Either.getOrThrow(Cron.parse("0 4 8-14 * *"))
assert.deepStrictEqual(Cron.match(cron, new Date("2021-01-08 04:00:00")), true)
assert.deepStrictEqual(Cron.match(cron, new Date("2021-01-08 05:00:00")), false)

Signature

declare const match: (cron: Cron, date: DateTime.DateTime.Input) => boolean

Source

Since v2.0.0

next

Returns the next run Date for the given Cron instance.

Uses the current time as a starting point if no value is provided for now.

Throws

IllegalArgumentException if the given DateTime.Input is invalid. Error if the next run date cannot be found within 10,000 iterations.

Example

import * as assert from "node:assert"
import { Cron, Either } from "effect"

const after = new Date("2021-01-01 00:00:00")
const cron = Either.getOrThrow(Cron.parse("0 4 8-14 * *"))
assert.deepStrictEqual(Cron.next(cron, after), new Date("2021-01-08 04:00:00"))

Signature

declare const next: (cron: Cron, startFrom?: DateTime.DateTime.Input) => Date

Source

Since v2.0.0

sequence

Returns an IterableIterator which yields the sequence of Dates that match the Cron instance.

Signature

declare const sequence: (cron: Cron, startFrom?: DateTime.DateTime.Input) => IterableIterator<Date>

Source

Since v2.0.0