Giter VIP home page Giter VIP logo

crontab's Introduction

Crontab

CI Coverage Status Module Version Hex Docs Total Download License Last Updated

Elixir library for parsing, writing, and calculating Cron format strings.

Installation

Add :crontab to your list of dependencies in mix.exs:

def deps do
  [
    {:crontab, "~> 1.1"}
  ]
end

For Elixir version before 1.4, ensure :crontab is started before your application:

def application do
  [
    applications: [:crontab]
  ]
end

Usage

Import Cron expression sigil

Everywhere you want to use the Cron expression sigil (e[cron expression]).

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

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.

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]
iex> Crontab.Scheduler.get_next_run_dates(3, ~e[*/2], ~N[2017-01-01 01:01:00])
{:ok,
 [~N[2017-01-01 01:02:00], ~N[2017-01-01 01:04:00], ~N[2017-01-01 01:06:00]]}
iex> Crontab.Scheduler.get_next_run_dates!(3, ~e[*/2], ~N[2017-01-01 01:01:00])
[~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.

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

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 for details.

crontab's People

Contributors

maennchen avatar dependabot[bot] avatar thomas9911 avatar h4cc avatar kianmeng avatar ftuyama avatar grantjamespowell avatar kabie avatar theoretick avatar mtrudel avatar mveytsman avatar vishal-h avatar dependabot-preview[bot] avatar jjl avatar pnezis avatar techgaun avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.