#cron #format-string #utilities #utility #minutes #return #remaining

rusty-cron

Lightweight and performant library to parse cron format strings and return the remaining miliseconds to execute. Accepts both 5 and 6 parameters (5 for minutes and up and 6 for seconds)

1 unstable release

0.8.0 Jul 30, 2024

#3 in #minutes

28 downloads per month
Used in rusty-cron-scheduler

GPL-3.0-only

17KB
227 lines

rusty-cron

Lightweight and performant library to parse cron format strings and return the remaining miliseconds to execute.

  1. About
  2. Features
  3. Installation
  4. Usage
  5. Contributing

About

rusty-cron is a simple library made to parse cron formatted strings and return the remaining milliseconds to the next execution, either using current time or a given one, it's made to be very quick at the cost of being a bit more heavy on memory, but it shouldn't be a problem for most applications.

Features

Currently allows for either a 5 or 6 tokens cron formated string depending on if you need the precision to go up to minutes or seconds respectively. It also allows all normal tokens to be used in the string, those are:

  • *: Defines all possible values
  • x/y: Defines values starting at x and repeating every y, for example: 0/5, every 5 minutes starting at 0
  • x-y: Values starting at x until y, for example: 5-15, every minute between 5 and 15
  • x,y,z: Specific values defined, for example: 1,5,50, at minutes 1, 5 and 50

You can also mix and match those in some ways, checkout https://crontab.guru/ for an amazing explanation on what does your cron do.

Installation

Should be as easy as cargo install rusty_cron

Usage

Simply add the use rusty_cron::Cron and use Cron::parse_time() with your cron string (I recommend https://crontab.guru/ to validate your strings and create new ones), and, optionally, a second parameter DateTime to give the algorithm the starting date, by default it'll be "chrono::Utc::now()".

That'll return a result, with a string with the error message or the milliseconds remaining to the next theoretical execution. If what you want is for a task to be triggered periodically, have a look at rusty-scheduler, it uses this same cron string format to automatically execute functions.

Contributing

If you feel like something is missing, could be improved or needs to be changed let me know on a ticket or just start a pull request and I'll try to have a look at it asap.

Dependencies

~1MB
~18K SLoC