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 from.

Signature

export declare const make: ({
  days,
  hours,
  minutes,
  months,
  weekdays
}: {
  readonly minutes: Iterable<number>
  readonly hours: Iterable<number>
  readonly days: Iterable<number>
  readonly months: Iterable<number>
  readonly weekdays: Iterable<number>
}) => Cron

Added in v2.0.0

parse

Parses a cron expression into a Cron instance.

Signature

export declare const parse: (cron: 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 4 8-14 * *"),
  Either.right(
    Cron.make({
      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 minutes: ReadonlySet<number>
  readonly hours: ReadonlySet<number>
  readonly days: ReadonlySet<number>
  readonly months: ReadonlySet<number>
  readonly weekdays: ReadonlySet<number>
}

Added in v2.0.0

ParseError (interface)

Represents a checked exception which occurs when decoding fails.

Signature

export interface ParseError {
  readonly _tag: "ParseError"
  readonly [ParseErrorTypeId]: ParseErrorTypeId
  readonly message: string
  readonly input?: string
}

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: Date) => 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?: Date) => 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?: Date) => IterableIterator<Date>

Added in v2.0.0