Struct croner::Cron

source ·
pub struct Cron {
    pub pattern: CronPattern,
}

Fields§

§pattern: CronPattern

Implementations§

source§

impl Cron

source

pub fn new(cron_string: &str) -> Self

source

pub fn parse(&mut self) -> Result<Cron, CronError>

source

pub fn is_time_matching<Tz: TimeZone>( &self, time: &DateTime<Tz> ) -> Result<bool, CronError>

Evaluates if a given DateTime matches the cron pattern associated with this instance.

The function checks each cron field (seconds, minutes, hours, day of month, month) against the provided DateTime to determine if it aligns with the cron pattern. Each field is checked for a match, and all fields must match for the entire pattern to be considered a match.

Parameters
  • time: A reference to the DateTime<Tz> to be checked against the cron pattern.
Returns
  • Ok(bool): true if time matches the cron pattern, false otherwise.
  • Err(CronError): An error if there is a problem checking any of the pattern fields against the provided DateTime.
Errors

This method may return CronError if an error occurs during the evaluation of the cron pattern fields. Errors can occur due to invalid bit operations or invalid dates.

Examples
use croner::Cron;
use chrono::Utc;

// Parse cron expression
let cron: Cron = Cron::new("* * * * *").parse().expect("Couldn't parse cron string");

// Compare to time now
let time = Utc::now();
let matches_all = cron.is_time_matching(&time).unwrap();

// Output results
println!("Time is: {}", time);
println!(
    "Pattern \"{}\" does {} time {}",
    cron.pattern.to_string(),
    if matches_all { "match" } else { "not match" },
    time
);
source

pub fn find_next_occurrence<Tz>( &self, start_time: &DateTime<Tz>, inclusive: bool ) -> Result<DateTime<Tz>, CronError>
where Tz: TimeZone,

Finds the next occurrence of a scheduled date and time that matches the cron pattern, starting from a given start_time. If inclusive is true, the search includes the start_time; otherwise, it starts from the next second.

This method performs a search through time, beginning at start_time, to find the next date and time that aligns with the cron pattern defined within the Cron instance. The search respects cron fields (seconds, minutes, hours, day of month, month, day of week) and iterates through time until a match is found or an error occurs.

Parameters
  • start_time: A reference to a DateTime<Tz> indicating the start time for the search.
  • inclusive: A bool that specifies whether the search should include start_time itself.
Returns
  • Ok(DateTime<Tz>): The next occurrence that matches the cron pattern.
  • Err(CronError): An error if the next occurrence cannot be found within a reasonable limit, if any of the date/time manipulations result in an invalid date, or if the cron pattern match fails.
Errors
  • CronError::InvalidTime: If the start time provided is invalid or adjustments to the time result in an invalid date/time.
  • CronError::TimeSearchLimitExceeded: If the search exceeds a reasonable time limit. This prevents infinite loops in case of patterns that cannot be matched.
  • Other errors as defined by the CronError enum may occur if the pattern match fails at any stage of the search.
Examples
use chrono::Utc;
use croner::Cron;

// Parse cron expression
let cron: Cron = Cron::new("0 18 * * * 5").with_seconds_required().parse().expect("Success");

// Get next match
let time = Utc::now();
let next = cron.find_next_occurrence(&time, false).unwrap();

println!(
    "Pattern \"{}\" will match next time at {}",
    cron.pattern.to_string(),
    next
);
source

pub fn iter_from<Tz>(&self, start_from: DateTime<Tz>) -> CronIterator<Tz>
where Tz: TimeZone,

Creates a CronIterator starting from the specified time.

This function will create an iterator that yields dates and times that match a cron schedule, beginning at start_from. The iterator will begin at the specified start time if it matches.

Examples
use chrono::Utc;
use croner::Cron;

// Parse cron expression
let cron = Cron::new("* * * * *").parse().expect("Couldn't parse cron string");

// Compare to time now
let time = Utc::now();

// Get next 5 matches using iter_from
println!("Finding matches of pattern '{}' starting from {}:", cron.pattern.to_string(), time);

for time in cron.clone().iter_from(time).take(5) {
    println!("{}", time);
}
Parameters
  • start_from: A DateTime<Tz> that represents the starting point for the iterator.
Returns

Returns a CronIterator<Tz> that can be used to iterate over scheduled times.

source

pub fn iter_after<Tz>(&self, start_after: DateTime<Tz>) -> CronIterator<Tz>
where Tz: TimeZone,

Creates a CronIterator starting after the specified time.

This function will create an iterator that yields dates and times that match a cron schedule, beginning after start_after. The iterator will not yield the specified start time; it will yield times that come after it according to the cron schedule.

Examples
use chrono::Utc;
use croner::Cron;

// Parse cron expression
let cron = Cron::new("* * * * *").parse().expect("Couldn't parse cron string");

// Compare to time now
let time = Utc::now();

// Get next 5 matches using iter_from
println!("Finding matches of pattern '{}' starting from {}:", cron.pattern.to_string(), time);

for time in cron.clone().iter_after(time).take(5) {
    println!("{}", time);
}
  
Parameters
  • start_after: A DateTime<Tz> that represents the starting point for the iterator.
Returns

Returns a CronIterator<Tz> that can be used to iterate over scheduled times.

source

pub fn with_dom_and_dow(&mut self) -> &mut Self

source

pub fn with_seconds_optional(&mut self) -> &mut Self

source

pub fn with_seconds_required(&mut self) -> &mut Self

source

pub fn with_alternative_weekdays(&mut self) -> &mut Self

Trait Implementations§

source§

impl Clone for Cron

source§

fn clone(&self) -> Cron

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl FromStr for Cron

§

type Err = CronError

The associated error which can be returned from parsing.
source§

fn from_str(cron_string: &str) -> Result<Cron, CronError>

Parses a string s to return a value of this type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Cron

§

impl Send for Cron

§

impl Sync for Cron

§

impl Unpin for Cron

§

impl UnwindSafe for Cron

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.