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

Cron overview

Added in v2.0.0


Table of contents


constructors

make

Creates a Cron instance.

Signature

export 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

Added in v2.0.0

parse

Parses a cron expression into a Cron instance.

Signature

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

Example

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: []
    })
  )
)

Added in 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.

Signature

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

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: []
// }

Added in v2.0.0

guards

isCron

Checks if a given value is a Cron instance.

Signature

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

Added in v2.0.0

isParseError

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

Signature

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

Added in v2.0.0

instances

Equivalence

Signature

export declare const Equivalence: equivalence.Equivalence<Cron>

Added in 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>
  }
}

Added in v2.0.0

ParseError (class)

Represents a checked exception which occurs when decoding fails.

Signature

export declare class ParseError

Added in v2.0.0

[ParseErrorTypeId] (property)

Signature

readonly [ParseErrorTypeId]: symbol

Added in v2.0.0

predicates

equals

Checks if two Crons are equal.

Signature

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

Added in v2.0.0

symbol

ParseErrorTypeId

Signature

export declare const ParseErrorTypeId: typeof ParseErrorTypeId

Added in v2.0.0

TypeId (type alias)

Signature

export type TypeId = typeof TypeId

Added in v2.0.0

symbols

ParseErrorTypeId (type alias)

Signature

export type ParseErrorTypeId = typeof ParseErrorTypeId

Added in v2.0.0

TypeId

Signature

export declare const TypeId: typeof TypeId

Added in v2.0.0

utils

match

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

Signature

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

Example

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)

Added in 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.

Signature

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

Example

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"))

Added in v2.0.0

sequence

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

Signature

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

Added in v2.0.0