Cron

Struct Cron 

Source
pub struct Cron {
    pub pattern: CronPattern,
}

Fields§

§pattern: CronPattern

Implementations§

Source§

impl Cron

Source

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

Evaluates if a given DateTime matches the cron pattern.

The function checks each cron field (seconds, minutes, hours, day of month, month and year) 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 std::str::FromStr as _;

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

// Parse cron expression
let cron: Cron = Cron::from_str("* * * * *").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: TimeZone>( &self, start_time: &DateTime<Tz>, inclusive: bool, ) -> Result<DateTime<Tz>, CronError>

Finds the next occurrence of a scheduled 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, parser::{Seconds, CronParser}};

// Parse cron expression
let cron: Cron = CronParser::builder().seconds(Seconds::Required).build().parse("0 18 * * * 5").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 find_previous_occurrence<Tz: TimeZone>( &self, start_time: &DateTime<Tz>, inclusive: bool, ) -> Result<DateTime<Tz>, CronError>

Finds the previous occurrence of a scheduled time that matches the cron pattern.

Source

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

Creates a CronIterator starting from the specified time.

The search can be performed forwards or backwards in time.

§Arguments
  • start_from - A DateTime<Tz> that represents the starting point for the iterator.
  • direction - A Direction to specify the search direction.
§Returns

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

Source

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

Creates a CronIterator starting after the specified time, in forward direction.

§Arguments
  • 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 iter_before<Tz: TimeZone>( &self, start_before: DateTime<Tz>, ) -> CronIterator<Tz>

Creates a CronIterator starting before the specified time, in backwards direction.

§Arguments
  • start_before - 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 describe(&self) -> String

Returns a human-readable description of the cron pattern.

This method provides a best-effort English description of the cron schedule. Note: The cron instance must be parsed successfully before calling this method.

§Example
use croner::Cron;
use std::str::FromStr as _;

let cron = Cron::from_str("0 12 * * MON-FRI").unwrap();
println!("{}", cron.describe());
// Output: At on minute 0, at hour 12, on Monday,Tuesday,Wednesday,Thursday,Friday.
Source

pub fn describe_lang<L: Language>(&self, lang: L) -> String

Returns a human-readable description using a provided language provider.

§Arguments
  • lang - An object that implements the Language trait.
Source

pub fn determine_job_type(&self) -> JobType

Determines if the cron pattern represents a Fixed-Time Job or an Interval/Wildcard Job. A Fixed-Time Job has fixed (non-wildcard, non-stepped, single-value) Seconds, Minute, and Hour fields. Otherwise, it’s an Interval/Wildcard Job.

Source

pub fn as_str(&self) -> &str

Trait Implementations§

Source§

impl Clone for Cron

Source§

fn clone(&self) -> Cron

Returns a duplicate 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 Debug for Cron

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Cron

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for Cron

Source§

type Err = CronError

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

fn from_str(s: &str) -> Result<Self, Self::Err>

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

impl Hash for Cron

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Cron

Source§

fn eq(&self, other: &Cron) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Cron

Source§

fn partial_cmp(&self, other: &Cron) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Cron

Auto Trait Implementations§

§

impl Freeze for Cron

§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.