pub struct Cron {
pub pattern: CronPattern,
}
Fields§
§pattern: CronPattern
Implementations§
source§impl Cron
impl Cron
pub fn new(cron_string: &str) -> Self
pub fn parse(&mut self) -> Result<Cron, CronError>
sourcepub fn is_time_matching<Tz: TimeZone>(
&self,
time: &DateTime<Tz>
) -> Result<bool, CronError>
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 theDateTime<Tz>
to be checked against the cron pattern.
Returns
Ok(bool)
:true
iftime
matches the cron pattern,false
otherwise.Err(CronError)
: An error if there is a problem checking any of the pattern fields against the providedDateTime
.
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
);
sourcepub fn find_next_occurrence<Tz>(
&self,
start_time: &DateTime<Tz>,
inclusive: bool
) -> Result<DateTime<Tz>, CronError>where
Tz: TimeZone,
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 aDateTime<Tz>
indicating the start time for the search.inclusive
: Abool
that specifies whether the search should includestart_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
);
sourcepub fn iter_from<Tz>(&self, start_from: DateTime<Tz>) -> CronIterator<Tz>where
Tz: TimeZone,
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
: ADateTime<Tz>
that represents the starting point for the iterator.
Returns
Returns a CronIterator<Tz>
that can be used to iterate over scheduled times.
sourcepub fn iter_after<Tz>(&self, start_after: DateTime<Tz>) -> CronIterator<Tz>where
Tz: TimeZone,
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
: ADateTime<Tz>
that represents the starting point for the iterator.
Returns
Returns a CronIterator<Tz>
that can be used to iterate over scheduled times.