Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rebase upstream cron onto jiff-cron #131

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
[package]
name = "cron"
version = "0.12.1"
authors = ["Zack Slayton <zack.slayton@gmail.com>"]
repository = "https://github.com/zslayton/cron"
documentation = "https://docs.rs/cron"
name = "jiff-cron"
version = "0.1.0"
authors = [
"Max Countryman <hello@maxcountryman.com>",
"Zack Slayton <zack.slayton@gmail.com>",
]
repository = "https://github.com/maxcountryman/jiff-cron"
documentation = "https://docs.rs/jiff-cron"
readme = "README.md"
description = "A cron expression parser and schedule explorer."
keywords = [ "cron", "schedule", "repeat", "periodic", "time" ]
description = "A cron expression parser and schedule explorer built with jiff."
keywords = ["cron", "schedule", "repeat", "periodic", "time"]
license = "MIT OR Apache-2.0"
edition = "2021"

[lib]
name = "cron"

[dependencies]
chrono = { version = "~0.4", default-features = false, features = ["clock"] }
jiff = "0.1"
nom = "~7"
once_cell = "1.10"
serde = {version = "1.0.164", optional = true }

[dev-dependencies]
chrono-tz = "~0.6"
serde_test = "1.0.164"

# Dev-dependency for feature "serde".
Expand All @@ -30,5 +29,3 @@ postcard = { version = "1.0.10", default-features = false, features = ["use-std"

[features]
serde = ["dep:serde"]


18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# cron [![Rust](https://github.com/zslayton/cron/workflows/Rust/badge.svg)](https://github.com/zslayton/cron/actions) [![](https://img.shields.io/crates/v/cron.svg)](https://crates.io/crates/cron) [![](https://docs.rs/cron/badge.svg)](https://docs.rs/cron)
A cron expression parser. Works with stable Rust v1.28.0.
# jiff-cron [![Rust](https://github.com/maxcountryman/jiff-cron/workflows/Rust/badge.svg)](https://github.com/maxcountryman/jiff-cron/actions) [![](https://img.shields.io/crates/v/jiff-cron.svg)](https://crates.io/crates/jiff-cron) [![](https://docs.rs/jiff-cron/badge.svg)](https://docs.rs/jiff-cron)

A cron expression parser built with `jiff`.

```rust
use cron::Schedule;
use chrono::Utc;
use jiff_cron::Schedule;
use jiff::tz::TimeZone;
use std::str::FromStr;

fn main() {
// sec min hour day of month month day of week year
let expression = "0 30 9,12,15 1,15 May-Aug Mon,Wed,Fri 2018/2";
let schedule = Schedule::from_str(expression).unwrap();
println!("Upcoming fire times:");
for datetime in schedule.upcoming(Utc).take(10) {
for datetime in schedule.upcoming(TimeZone::UTC).take(10) {
println!("-> {}", datetime);
}
}
Expand All @@ -34,9 +35,10 @@ Upcoming fire times:
## License

Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)
at your option.

- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)
at your option.

### Contribution

Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#![deny(rust_2018_idioms)]
#![deny(rustdoc::broken_intra_doc_links)]
#![allow(clippy::needless_doctest_main)]
//! A cron expression parser and schedule explorer
//! A cron expression parser and schedule explorer built with jiff.
//!
//! # Example
//!
//! ```
//! use cron::Schedule;
//! use chrono::Utc;
//! use jiff_cron::Schedule;
//! use jiff::tz::TimeZone;
//! use std::str::FromStr;
//!
//! fn main() {
//! // sec min hour day of month month day of week year
//! let expression = "0 30 9,12,15 1,15 May-Aug Mon,Wed,Fri 2018/2";
//! let schedule = Schedule::from_str(expression).unwrap();
//! println!("Upcoming fire times:");
//! for datetime in schedule.upcoming(Utc).take(10) {
//! for datetime in schedule.upcoming(TimeZone::UTC).take(10) {
//! println!("-> {}", datetime);
//! }
//! }
Expand Down
59 changes: 24 additions & 35 deletions src/queries.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
use chrono::offset::TimeZone;
use chrono::{DateTime, Datelike, Duration, Timelike};
use jiff::{SignedDuration, Zoned};

use crate::ordinal::Ordinal;
use crate::time_unit::{DaysOfMonth, Hours, Minutes, Months, Seconds, TimeUnitField};

const ONE_SECOND: SignedDuration = SignedDuration::from_secs(1);

// TODO: Possibility of one query struct?

pub struct NextAfterQuery<Z>
where
Z: TimeZone,
{
initial_datetime: DateTime<Z>,
pub struct NextAfterQuery {
initial_datetime: Zoned,
first_month: bool,
first_day_of_month: bool,
first_hour: bool,
first_minute: bool,
first_second: bool,
}

impl<Z> NextAfterQuery<Z>
where
Z: TimeZone,
{
pub fn from(after: &DateTime<Z>) -> NextAfterQuery<Z> {
impl NextAfterQuery {
pub fn from(after: &Zoned) -> NextAfterQuery {
NextAfterQuery {
initial_datetime: after.clone() + Duration::seconds(1),
initial_datetime: after.clone().saturating_add(ONE_SECOND),
first_month: true,
first_day_of_month: true,
first_hour: true,
Expand All @@ -41,7 +36,7 @@ where
pub fn month_lower_bound(&mut self) -> Ordinal {
if self.first_month {
self.first_month = false;
return self.initial_datetime.month();
return self.initial_datetime.month() as u32;
}
Months::inclusive_min()
}
Expand All @@ -54,7 +49,7 @@ where
pub fn day_of_month_lower_bound(&mut self) -> Ordinal {
if self.first_day_of_month {
self.first_day_of_month = false;
return self.initial_datetime.day();
return self.initial_datetime.day() as u32;
}
DaysOfMonth::inclusive_min()
}
Expand All @@ -67,7 +62,7 @@ where
pub fn hour_lower_bound(&mut self) -> Ordinal {
if self.first_hour {
self.first_hour = false;
return self.initial_datetime.hour();
return self.initial_datetime.hour() as u32;
}
Hours::inclusive_min()
}
Expand All @@ -80,7 +75,7 @@ where
pub fn minute_lower_bound(&mut self) -> Ordinal {
if self.first_minute {
self.first_minute = false;
return self.initial_datetime.minute();
return self.initial_datetime.minute() as u32;
}
Minutes::inclusive_min()
}
Expand All @@ -93,7 +88,7 @@ where
pub fn second_lower_bound(&mut self) -> Ordinal {
if self.first_second {
self.first_second = false;
return self.initial_datetime.second();
return self.initial_datetime.second() as u32;
}
Seconds::inclusive_min()
}
Expand All @@ -103,27 +98,21 @@ where
}
} // End of impl

pub struct PrevFromQuery<Z>
where
Z: TimeZone,
{
initial_datetime: DateTime<Z>,
pub struct PrevFromQuery {
initial_datetime: Zoned,
first_month: bool,
first_day_of_month: bool,
first_hour: bool,
first_minute: bool,
first_second: bool,
}

impl<Z> PrevFromQuery<Z>
where
Z: TimeZone,
{
pub fn from(before: &DateTime<Z>) -> PrevFromQuery<Z> {
let initial_datetime = if before.timestamp_subsec_millis() > 0 {
impl PrevFromQuery {
pub fn from(before: &Zoned) -> PrevFromQuery {
let initial_datetime = if before.subsec_nanosecond() > 0 {
before.clone()
} else {
before.clone() - Duration::seconds(1)
before.saturating_sub(ONE_SECOND)
};
PrevFromQuery {
initial_datetime,
Expand All @@ -143,7 +132,7 @@ where
pub fn month_upper_bound(&mut self) -> Ordinal {
if self.first_month {
self.first_month = false;
return self.initial_datetime.month();
return self.initial_datetime.month() as u32;
}
Months::inclusive_max()
}
Expand All @@ -156,7 +145,7 @@ where
pub fn day_of_month_upper_bound(&mut self) -> Ordinal {
if self.first_day_of_month {
self.first_day_of_month = false;
return self.initial_datetime.day();
return self.initial_datetime.day() as u32;
}
DaysOfMonth::inclusive_max()
}
Expand All @@ -169,7 +158,7 @@ where
pub fn hour_upper_bound(&mut self) -> Ordinal {
if self.first_hour {
self.first_hour = false;
return self.initial_datetime.hour();
return self.initial_datetime.hour() as u32;
}
Hours::inclusive_max()
}
Expand All @@ -182,7 +171,7 @@ where
pub fn minute_upper_bound(&mut self) -> Ordinal {
if self.first_minute {
self.first_minute = false;
return self.initial_datetime.minute();
return self.initial_datetime.minute() as u32;
}
Minutes::inclusive_max()
}
Expand All @@ -195,7 +184,7 @@ where
pub fn second_upper_bound(&mut self) -> Ordinal {
if self.first_second {
self.first_second = false;
return self.initial_datetime.second();
return self.initial_datetime.second() as u32;
}
Seconds::inclusive_max()
}
Expand Down
Loading
Loading