cronparse 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 62

cronparse #

Parse and calculate different times related to Unix cron expressions

Parse Unix cron expressions and calculate things like:

  • whether the expression is valid, as a bool
  • the next time the expression will run, as a DateTime
  • the last time the expression would have run, as a DateTime
  • the duration until the next time the expression will run, as a Duration
  • the duration since the last time the expression would have run, as a negative Duration

Usage #

void main() {
    // validate a cron expression
    final valid = isValid("54 2-3,4-9 */3 FEB MON-FRI");
    print(valid); // true

    // calculate times based on a cron expression
    final time = DateTime.parse("2019-11-23 16:00:00");

    var cron = Cron("0 22 * * *");
    print(cron.nextRelativeTo(time)); // "2019-11-23 22:00:00.000"
    print(cron.untilNextRelativeTo(time) == Duration(hours: 6)); // true

    cron = Cron("*/15 * * * *");
    print(cron.previousRelativeTo(time)); // "2019-11-23 15:45:00.000"
    print(cron.sincePreviousRelativeTo(time) == Duration(minutes: -15)); // true
}

Cron expression matching strategy #

High-level explanation on matching #

  • if the expression is a predefined nickname, translate it to the relevant actual expression. note: this library does not parse the @reboot nickname.
  • tokenize the expression to each part (minute, hour, day of month, month, day of week)
  • test each of these tokens to match
    • token values can be in a few forms:
      • an asterisk (*): this always matches
      • an asterisk with skips (*/15): iterate through the valid range for the field, incrementing by the skip. if any of these match, the expression matches.
      • exact value (48, feb, WED): match these exactly
      • range of values without skips (5-9): iterate through the range, starting at the lower bound and ending at the higher bound, incrementing by 1. if any of these match, the value matches
      • range of values with skip (20-30/2): iterate through the range, starting at the lower bound and ending at the higher bound, incrementing by the skip value. if any of these match, the value matches.
      • set of values or ranges (1,2,3, 5-10,45-50): test each value in the set using the above strategy. if any of the member values match, the value matches.
  • iff all tokens match, the expression matches

@reboot #

This library is focused on temporal calculation for cron expressions. The @reboot nickname is shorthand for "exactly once after reboot." Since the @reboot nickname is not a temporal representation, it is not handled by this library.

[0.1.1] - 2020-06-12 #

  • Fix pub.dev health suggestions

[0.1.0] - 2020-06-04 #

  • Initial pub.dev release

[0.0.1] - 2020-05-22 #

  • GitHub repo initialized

example/main.dart

import 'package:cronparse/cronparse.dart';

void main() {
    // validate a cron expression
    final valid = isValid("54 2-3,4-9 */3 FEB MON-FRI");
    print(valid); // true

    // calculate times based on a cron expression
    final time = DateTime.parse("2019-11-23 16:00:00");

    var cron = Cron("0 22 * * *");
    print(cron.nextRelativeTo(time)); // "2019-11-23 22:00:00.000"
    print(cron.untilNextRelativeTo(time) == Duration(hours: 6)); // true

    cron = Cron("*/15 * * * *");
    print(cron.previousRelativeTo(time)); // "2019-11-23 15:45:00.000"
    print(cron.sincePreviousRelativeTo(time) == Duration(minutes: -15)); // true
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  cronparse: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:cronparse/cronparse.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
23
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
62
Learn more about scoring.

We analyzed this package on Jul 6, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Health suggestions

Format lib/src/cronparse.dart.

Run dartfmt to format lib/src/cronparse.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
Dev dependencies
test ^1.3.4