Giter Club home page Giter Club logo

cron_calc's Introduction

CronCalc

CronCalc: A Ruby gem for calculating CRON job occurrences. With this gem, you can easily determine when a cron job will occur by providing a cron expression. Key features include the ability to calculate occurrences:

  • Within a specified period: Find out all the times your cron job will run during a particular timeframe.
  • After a given date: Determine the next set of occurrences after a specific starting point.
  • Before a given date: Discover when your cron job ran or would have run before a certain date.

This tool can be used for scheduling, forecasting, and analyzing tasks in systems that use cron for job scheduling. You can also read the article about CronCalc here.

Installation

Install the gem by executing:

$ gem install cron_calc

Usage

After installing cron_calc you can initialize CronCalc with the CRON string.

    require 'cron_calc'
    cron_calc = CronCalc.new('5 5 * * *')

Now, you can use one of three methods #in, #next, #last to determine cron job occurrencies within specified period.

Using #in

Calculates cron job occurrences within a given time period.
Parameters:

  • period - a Range object defining the start and end times for the calculation.\
    period = Time.new(2024, 1, 1, 0, 0)..Time.new(2024, 1, 4, 0, 0)
    cron_calc.in(period)

    # => [2024-01-01 05:05:00 +0100, 2024-01-02 05:05:00 +0100, 2024-01-03 05:05:00 +0100]

Using #next

Calculates the next 'n' occurrences of the cron job from a given start time.
Parameters:

  • count - (optional, Integer) The number of occurrences to calculate. Defaults to 1.
  • after: - (optional, Time, keyword argument) The start time from which to calculate occurrences. If not provided, defaults to the current time (Time.now).
  • max_years - (optional, Integer, keyword argument) The maximum number of years to search for future occurrences. Defaults to 5.
    cron_calc.next
    # => [2023-12-20 05:05:00 +0100]

    cron_calc.next(3)
    # => [2023-12-20 05:05:00 +0100, 2023-12-21 05:05:00 +0100, 2023-12-22 05:05:00 +0100]

    cron_calc.next(2, after: Time.new(2024, 1, 1, 0, 0))
    # => [2024-01-01 05:05:00 +0100, 2024-01-02 05:05:00 +0100]

Using #last

Calculates the last 'n' occurrences of the cron job until a given end time.
Parameters:

  • count - (optional, Integer) The number of occurrences to calculate. Defaults to 1.
  • before: - (optional, Time, keyword argument) The end time from which to calculate past occurrences. If not provided, defaults to the current time (Time.now).
  • max_years - (optional, Integer, keyword argument) The maximum number of years to search backward for past occurrences. Defaults to 5.
    cron_calc.last
    # => [2023-12-19 05:05:00 +0100]

    cron_calc.last(4, before: Time.new(2024, 1, 1, 0, 0))
    # => [2023-12-31 05:05:00 +0100, 2023-12-30 05:05:00 +0100, 2023-12-29 05:05:00 +0100, 2023-12-28 05:05:00 +0100]

Other examples

    # You can omit the count parameter
    CronCalc.new('5 5 */5 * SUN').last(before: Time.new(2020, 1, 1, 0, 0))
    # => [2019-12-01 05:05:00 +0100]

    # You can combine ',' and '-'
    CronCalc.new('5 5 5-7,10 FEB *').next(5)
    # => [2024-02-05 05:05:00 +0100, 2024-02-06 05:05:00 +0100, 2024-02-07 05:05:00 +0100, 2024-02-10 05:05:00 +0100, 2025-02-05 05:05:00 +0100]

    # You can use predefined definitions like @daily, @monthly, etc.
    CronCalc.new('@monthly').next(3, after: Time.new(2024, 1, 1, 0, 0))
    # => [2024-01-01 00:00:00 +0100, 2024-02-01 00:00:00 +0100, 2024-03-01 00:00:00 +0100]

    # I want to know when the next 10 Friday the 13ths will be!
    CronCalc.new('0 0 13 * FRI').next(10)
    # => 
    # [2024-09-13 00:00:00 +0200,
    #  2024-12-13 00:00:00 +0100,
    #  2025-06-13 00:00:00 +0200,
    #  2026-02-13 00:00:00 +0100,
    #  2026-03-13 00:00:00 +0100,
    #  2026-11-13 00:00:00 +0100,
    #  2027-08-13 00:00:00 +0200,
    #  2028-10-13 00:00:00 +0200]

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mizinsky/cron_calc.

License

The gem is available as open source under the terms of the MIT License.

cron_calc's People

Contributors

mizinsky avatar

Stargazers

Alexandr Stets avatar Ivan Kulagin avatar Alex Musayev avatar Thomas Klemm avatar Felipe Zavan avatar Bilal Budhani avatar Chris Edwards avatar Brandon Zylstra avatar Dmytro Piliugin avatar Samuel Olugbemi avatar Zbigniew Humeniuk avatar JButton avatar Igor Zubkov avatar John Mettraux avatar John Bachir avatar Trevor Turk avatar Julian Cheal avatar  avatar Pavlo Vavruk avatar Wojciech Drużkawiecki avatar Marcin Pomorski avatar Artur Luberda avatar  avatar Wojciech Rawicz-Kosiński avatar Jan Kaczorowski avatar Maciej Ochałek avatar Marcin Zgórecki avatar Szymon avatar Maciej Żabski avatar Łukasz Jachymczyk avatar Wojciech Zakrzewski avatar

Watchers

Łukasz Jachymczyk avatar  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.