https://github.com/maennchen/crontab
:alarm_clock: Parse Cron Expressions, Compose Cron Expression Strings and Caluclate Execution Dates.
https://github.com/maennchen/crontab
cron crontab elixir schedule
Last synced: 12 days ago
JSON representation
:alarm_clock: Parse Cron Expressions, Compose Cron Expression Strings and Caluclate Execution Dates.
- Host: GitHub
- URL: https://github.com/maennchen/crontab
- Owner: maennchen
- License: mit
- Created: 2016-12-21T23:43:07.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2024-10-23T14:28:00.000Z (6 months ago)
- Last Synced: 2025-03-29T05:05:30.608Z (19 days ago)
- Topics: cron, crontab, elixir, schedule
- Language: Elixir
- Homepage: https://hex.pm/packages/crontab
- Size: 365 KB
- Stars: 94
- Watchers: 4
- Forks: 33
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - A Cron Expressions Parser, Composer & Date Candidate Finder. (Date and Time)
README
# Crontab
[](https://github.com/maennchen/crontab/actions?query=workflow%3A.github%2Fworkflows%2Felixir.yml)
[](https://coveralls.io/github/maennchen/crontab?branch=main)
[](https://hex.pm/packages/crontab)
[](https://hexdocs.pm/crontab/)
[](https://hex.pm/packages/crontab)
[](https://github.com/maennchen/crontab/blob/main/LICENSE)
[](https://github.com/maennchen/crontab/commits/main)Elixir library for parsing, writing, and calculating Cron format strings.
## Installation
Add `:crontab` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:crontab, "~> 1.1"}
]
end
```## Usage
### Import Cron expression sigil
Everywhere you want to use the Cron expression sigil (`e[cron expression]`).
```elixir
import Crontab.CronExpression
```### Extended Cron expressions
An extended Cron expression has more precision than a normal Cron expression.
It also specifies the second.If you want to use extended Cron expressions with the sigil, just append an `e`.
### Checking if a Cron Expression Matches a date
```elixir
iex> import Crontab.CronExpression
iex> Crontab.DateChecker.matches_date?(~e[*/2], ~N[2017-01-01 01:01:00])
false
iex> Crontab.DateChecker.matches_date?(~e[*], ~N[2017-01-01 01:01:00])
true
```### Find Next / Previous Execution Date candidates
All the date parameters default to now.
For previous, just replace `next` in the code below.
```elixir
iex> import Crontab.CronExpression
iex> Crontab.Scheduler.get_next_run_date(~e[*/2], ~N[2017-01-01 01:01:00])
{:ok, ~N[2017-01-01 01:02:00]}
iex> Crontab.Scheduler.get_next_run_date!(~e[*/2], ~N[2017-01-01 01:01:00])
~N[2017-01-01 01:02:00]
``````elixir
iex> Enum.take(Crontab.Scheduler.get_next_run_dates(~e[*/2], ~N[2017-01-01 01:01:00]), 3)
[~N[2017-01-01 01:02:00], ~N[2017-01-01 01:04:00], ~N[2017-01-01 01:06:00]]
```### Parse Cron Expressions
If you statically define cron expressions, use the `~e[cron expression]` sigil.
For dynamic cron expressions, there is a Parser module.
The parser module takes an optional `extended` flag. This is to mark if the
expression contains seconds. This defaults to `false`.```elixir
iex> Crontab.CronExpression.Parser.parse "* * * * *"
{:ok,
%Crontab.CronExpression{day: [:*], hour: [:*], minute: [:*],
month: [:*], weekday: [:*], year: [:*]}}
iex> Crontab.CronExpression.Parser.parse! "* * * * *"
%Crontab.CronExpression{day: [:*], hour: [:*], minute: [:*],
month: [:*], weekday: [:*], year: [:*]}
```### Compose Cron expressions
```elixir
iex> Crontab.CronExpression.Composer.compose %Crontab.CronExpression{}
"* * * * * *"
iex> Crontab.CronExpression.Composer.compose %Crontab.CronExpression{minute: [9, {:-, 4, 6}, {:/, :*, 9}]}
"9,4-6,*/9 * * * * *"
```## Copyright and License
Copyright (c) 2016, SK & T AG, JOSHMARTIN GmbH, Jonatan Männchen
This library is MIT licensed. See the [LICENSE](https://github.com/maennchen/crontab/blob/main/LICENSE) for details.