Skip to content

Commit

Permalink
Fix backwards iteration when inital time has milliseconds (#112)
Browse files Browse the repository at this point in the history
Iterating back from a date that is later than a candidate by less than 1s but more than 0s should return said candidate.

Ex.
Let's say the schedule is `0 * * * * *`.
 - Iterating the schedule before `12:00:05.000` should yield `12:00:04.000`
 - However, iterating the schedule before `12:00:05.100` (note the milliseconds) should start at `12:00:05.000`.

This commit fixes the second case which was yielding `12:00:04.000`.
  • Loading branch information
juancampa authored Oct 28, 2024
1 parent c491880 commit e2fb958
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@ where
Z: TimeZone,
{
pub fn from(before: &DateTime<Z>) -> PrevFromQuery<Z> {
let initial_datetime = if before.timestamp_subsec_millis() > 0 {
before.clone()
} else {
before.clone() - Duration::seconds(1)
};
PrevFromQuery {
initial_datetime: before.clone() - Duration::seconds(1),
initial_datetime,
first_month: true,
first_day_of_month: true,
first_hour: true,
Expand Down
7 changes: 7 additions & 0 deletions src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ fn days_in_month(month: Ordinal, year: Ordinal) -> u32 {

#[cfg(test)]
mod test {
use chrono::Duration;

use super::*;
use std::str::FromStr;

Expand All @@ -539,6 +541,11 @@ mod test {
println!("PREV FROM for {} {:?}", expression, prev);
assert!(prev.is_some());
assert_eq!(prev, next);

let prev2 = schedule.prev_from(&(next2.unwrap() + Duration::milliseconds(100)));
println!("PREV2 FROM for {} {:?}", expression, prev2);
assert!(prev2.is_some());
assert_eq!(prev2, next2);
}

#[test]
Expand Down

0 comments on commit e2fb958

Please sign in to comment.