style(sched): 💄 lint and format

This commit is contained in:
Lucas Colombo 2024-04-07 23:39:03 -03:00
parent 9b0faace04
commit da3dd37eed
Signed by: lucas
GPG Key ID: EF34786CFEFFAE35
12 changed files with 95 additions and 88 deletions

View File

@ -7,3 +7,4 @@ scopes:
- "cli" - "cli"
- "logger" - "logger"
- "macros" - "macros"
- "sched"

View File

@ -3,7 +3,7 @@ mod cron;
#[cfg(feature = "sched-rule-recurrent")] #[cfg(feature = "sched-rule-recurrent")]
mod recurrent; mod recurrent;
#[cfg(feature = "sched-rule-recurrent")] #[cfg(feature = "sched-rule-recurrent")]
pub use self::recurrent::{many, val, range, ranges, ruleset, RecurrenceRuleSet, Rule}; pub use self::recurrent::{many, range, ranges, ruleset, val, RecurrenceRuleSet, Rule};
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};

View File

@ -1,2 +1 @@
// TODO: cron based scheduling
// TODO: cron based scheduling

View File

@ -1,8 +1,10 @@
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
mod ruleset;
mod rule_unit; mod rule_unit;
mod ruleset;
pub use ruleset::{RecurrenceRuleSet, builder::ruleset}; pub use {
pub use rule_unit::{Rule, val, range, many, ranges}; rule_unit::{many, range, ranges, val, Rule},
ruleset::{builder::ruleset, RecurrenceRuleSet},
};

View File

@ -1,5 +1,5 @@
pub mod builder; pub mod builder;
use { use {
super::Rule, super::Rule,
crate::sched::utils::cron_date::LoolDate, crate::sched::utils::cron_date::LoolDate,
@ -9,7 +9,7 @@ use {
/// 🧉 » a recurrence rule-set /// 🧉 » a recurrence rule-set
/// ///
/// sets rules that define a certain recurrence behavior /// sets rules that define a certain recurrence behavior
/// ///
/// use the builder pattern to create a new `RecurrenceRuleSet` /// use the builder pattern to create a new `RecurrenceRuleSet`
pub struct RecurrenceRuleSet { pub struct RecurrenceRuleSet {
/// second of the minute (0..59) /// second of the minute (0..59)
@ -37,10 +37,7 @@ impl RecurrenceRuleSet {
/// 🧉 » returns the next match of the rule set from a given `DateTime` /// 🧉 » returns the next match of the rule set from a given `DateTime`
pub fn next_match_from(&self, from: DateTime<Local>) -> Option<DateTime<Local>> { pub fn next_match_from(&self, from: DateTime<Local>) -> Option<DateTime<Local>> {
let next = self._next_match(from); let next = self._next_match(from);
match next { next.map(|date| date.date())
Some(date) => Some(date.date()),
None => None,
}
} }
/// 🚧 internal /// 🚧 internal
@ -51,12 +48,12 @@ impl RecurrenceRuleSet {
// check year // check year
if let Some(Rule::Val(year)) = self.year { if let Some(Rule::Val(year)) = self.year {
if year < from.year().into() { if year < from.year() {
return None; return None;
} }
} }
let mut next = LoolDate::new(from.clone()); let mut next = LoolDate::new(from);
next.add_second(); next.add_second();
loop { loop {
@ -125,4 +122,3 @@ impl RecurrenceRuleSet {
Some(next) Some(next)
} }
} }

View File

@ -1,11 +1,10 @@
use chrono::Weekday; use chrono::Weekday;
use super::RecurrenceRuleSet; use {super::RecurrenceRuleSet, crate::sched::rules::Rule};
use crate::sched::rules::Rule;
pub fn ruleset() -> RecurrenceRuleSet { pub fn ruleset() -> RecurrenceRuleSet {
let ruleset = RecurrenceRuleSet::recurring();
ruleset RecurrenceRuleSet::recurring()
} }
impl RecurrenceRuleSet { impl RecurrenceRuleSet {

View File

@ -1,3 +1,3 @@
mod recurrence_rules_by_val; mod recurrence_rules_by_many;
mod recurrence_rules_by_range; mod recurrence_rules_by_range;
mod recurrence_rules_by_many; mod recurrence_rules_by_val;

View File

@ -1,10 +1,10 @@
use chrono::{Datelike, Local, TimeZone, Utc}; use chrono::{Datelike, Local, TimeZone};
use crate::sched::rules::{many, range, ruleset}; use crate::sched::rules::{many, range, ruleset};
#[test] #[test]
fn every_day_at_12_and_15() { fn every_day_at_12_and_15() {
let date = Local.with_ymd_and_hms(2024, 4, 7, 13, 15, 05).unwrap(); let date = Local.with_ymd_and_hms(2024, 4, 7, 13, 15, 5).unwrap();
let mut rules = ruleset(); let mut rules = ruleset();
rules.hours_rule(many(vec![12, 15])).at_minute(0).at_second(0); rules.hours_rule(many(vec![12, 15])).at_minute(0).at_second(0);
@ -28,12 +28,12 @@ fn every_day_at_12_and_15() {
#[test] #[test]
fn each_day_between_9_and_17_at_hour_start() { fn each_day_between_9_and_17_at_hour_start() {
// will start next day because it's already > 17:00:00 // will start next day because it's already > 17:00:00
let date = Local.with_ymd_and_hms(2024, 4, 25, 19, 15, 05).unwrap(); let date = Local.with_ymd_and_hms(2024, 4, 25, 19, 15, 5).unwrap();
let mut rules = ruleset(); let mut rules = ruleset();
rules.hours_rule(range(9, 17, 1)).at_minute(0).at_second(0); rules.hours_rule(range(9, 17, 1)).at_minute(0).at_second(0);
let mut next = date.clone(); let mut next = date;
let initial_day = date.day() + 1; let initial_day = date.day() + 1;
for i in 0..18 { for i in 0..18 {

View File

@ -4,12 +4,12 @@ use crate::sched::rules::{range, ruleset};
#[test] #[test]
fn between_10_and_20_seconds() { fn between_10_and_20_seconds() {
let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 05).unwrap(); let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 5).unwrap();
let mut rules = ruleset(); let mut rules = ruleset();
rules.seconds_rule(range(10, 20, 1)); rules.seconds_rule(range(10, 20, 1));
let mut next = date.clone(); let mut next = date;
let initial_minute = date.minute(); let initial_minute = date.minute();
for i in 0..20 { for i in 0..20 {
@ -33,12 +33,12 @@ fn between_10_and_20_seconds() {
#[test] #[test]
fn each_day_between_9_and_17_at_hour_start() { fn each_day_between_9_and_17_at_hour_start() {
// will start next day because it's already > 17:00:00 // will start next day because it's already > 17:00:00
let date = Local.with_ymd_and_hms(2024, 4, 25, 19, 15, 05).unwrap(); let date = Local.with_ymd_and_hms(2024, 4, 25, 19, 15, 5).unwrap();
let mut rules = ruleset(); let mut rules = ruleset();
rules.hours_rule(range(9, 17, 1)).at_minute(0).at_second(0); rules.hours_rule(range(9, 17, 1)).at_minute(0).at_second(0);
let mut next = date.clone(); let mut next = date;
let initial_day = date.day() + 1; let initial_day = date.day() + 1;
for i in 0..18 { for i in 0..18 {

View File

@ -6,12 +6,12 @@ use crate::sched::rules::ruleset;
fn at_second_1_of_each_minute() { fn at_second_1_of_each_minute() {
// we have passed the second 1 of the minute // we have passed the second 1 of the minute
// so it should go to the next minute // so it should go to the next minute
let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 05).unwrap(); let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 5).unwrap();
let mut rules = ruleset(); let mut rules = ruleset();
rules.at_second(1); rules.at_second(1);
let mut next = date.clone(); let mut next = date;
let initial_minute = date.minute(); let initial_minute = date.minute();
for i in 0..10 { for i in 0..10 {
@ -20,7 +20,7 @@ fn at_second_1_of_each_minute() {
// should match 16:16:01, 16:17:01, 16:18:01, ... // should match 16:16:01, 16:17:01, 16:18:01, ...
assert_eq!( assert_eq!(
next, next,
Local.with_ymd_and_hms(2024, 4, 7, 16, initial_minute + i + 1, 01).unwrap() Local.with_ymd_and_hms(2024, 4, 7, 16, initial_minute + i + 1, 1).unwrap()
); );
} }
} }
@ -29,12 +29,12 @@ fn at_second_1_of_each_minute() {
fn at_hour_1_of_each_day() { fn at_hour_1_of_each_day() {
// we have passed the hour 1 of the day // we have passed the hour 1 of the day
// so it should go to the next day // so it should go to the next day
let date = Local.with_ymd_and_hms(2024, 4, 25, 16, 15, 05).unwrap(); let date = Local.with_ymd_and_hms(2024, 4, 25, 16, 15, 5).unwrap();
let mut rules = ruleset(); let mut rules = ruleset();
rules.at_time(1, 0, 0); rules.at_time(1, 0, 0);
let mut next = date.clone(); let mut next = date;
let mut initial_day = date.day() as i32; let mut initial_day = date.day() as i32;
let mut initial_month = date.month(); let mut initial_month = date.month();
@ -55,9 +55,9 @@ fn at_hour_1_of_each_day() {
2024, 2024,
initial_month, initial_month,
(initial_day + i + 1) as u32, (initial_day + i + 1) as u32,
01, 1,
00, 0,
00 0
) )
.unwrap() .unwrap()
); );
@ -68,12 +68,12 @@ fn at_hour_1_of_each_day() {
fn each_1st_of_month() { fn each_1st_of_month() {
// we have passed the 1st of the month // we have passed the 1st of the month
// so it should go to the next month // so it should go to the next month
let date = Local.with_ymd_and_hms(2024, 5, 7, 16, 15, 05).unwrap(); let date = Local.with_ymd_and_hms(2024, 5, 7, 16, 15, 5).unwrap();
let mut rules = ruleset(); let mut rules = ruleset();
rules.on_day(1).at_time(0, 0, 0); rules.on_day(1).at_time(0, 0, 0);
let mut next = date.clone(); let mut next = date;
let mut initial_month = date.month() as i32; let mut initial_month = date.month() as i32;
let mut initial_year = date.year(); let mut initial_year = date.year();
@ -90,7 +90,7 @@ fn each_1st_of_month() {
assert_eq!( assert_eq!(
next, next,
Local Local
.with_ymd_and_hms(initial_year, (initial_month + i + 1) as u32, 01, 00, 00, 00) .with_ymd_and_hms(initial_year, (initial_month + i + 1) as u32, 1, 0, 0, 0)
.unwrap() .unwrap()
); );
} }
@ -100,20 +100,20 @@ fn each_1st_of_month() {
fn each_wednesday() { fn each_wednesday() {
// we have passed the Wednesday // we have passed the Wednesday
// so it should go to the next Wednesday // so it should go to the next Wednesday
let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 05).unwrap(); let date = Local.with_ymd_and_hms(2024, 4, 7, 16, 15, 5).unwrap();
let mut next_wednesday = Local.with_ymd_and_hms(2024, 4, 10, 0, 0, 0).unwrap(); let mut next_wednesday = Local.with_ymd_and_hms(2024, 4, 10, 0, 0, 0).unwrap();
let mut rules = ruleset(); let mut rules = ruleset();
rules.on_dow(Weekday::Wed).at_time(0, 0, 0); rules.on_dow(Weekday::Wed).at_time(0, 0, 0);
let mut next = date.clone(); let mut next = date;
for _ in 0..10 { for _ in 0..10 {
next = rules.next_match_from(next).unwrap(); next = rules.next_match_from(next).unwrap();
println!("next: {:?}", next); println!("next: {:?}", next);
assert_eq!(next, next_wednesday); assert_eq!(next, next_wednesday);
next_wednesday = next_wednesday + chrono::Duration::days(7); next_wednesday += chrono::Duration::days(7);
} }
} }
@ -121,12 +121,12 @@ fn each_wednesday() {
fn from_31th_may_schedule_first_of_each_june() { fn from_31th_may_schedule_first_of_each_june() {
// we have passed the 31th of May // we have passed the 31th of May
// so it should go to the 1st of June // so it should go to the 1st of June
let date = Local.with_ymd_and_hms(2024, 5, 31, 16, 15, 05).unwrap(); let date = Local.with_ymd_and_hms(2024, 5, 31, 16, 15, 5).unwrap();
let mut rules = ruleset(); let mut rules = ruleset();
rules.in_month(6).on_day(1).at_time(0, 0, 0); rules.in_month(6).on_day(1).at_time(0, 0, 0);
let mut next = date.clone(); let mut next = date;
let initial_year = date.year(); let initial_year = date.year();
let initial_month = date.month(); let initial_month = date.month();
@ -138,7 +138,7 @@ fn from_31th_may_schedule_first_of_each_june() {
// should match 2024-06-01 00:00:00, 2024-07-01 00:00:00, 2024-08-01 00:00:00, ... // should match 2024-06-01 00:00:00, 2024-07-01 00:00:00, 2024-08-01 00:00:00, ...
assert_eq!( assert_eq!(
next, next,
Local.with_ymd_and_hms(initial_year + i, initial_month + 1, 01, 00, 00, 00).unwrap() Local.with_ymd_and_hms(initial_year + i, initial_month + 1, 1, 0, 0, 0).unwrap()
); );
} }
} }
@ -150,7 +150,7 @@ fn from_1st_may_schedule_first_of_each_june() {
let mut rules = ruleset(); let mut rules = ruleset();
rules.in_month(6).on_day(1).at_time(0, 0, 0); rules.in_month(6).on_day(1).at_time(0, 0, 0);
let mut next = date.clone(); let mut next = date;
let initial_year = date.year(); let initial_year = date.year();
for i in 0..10 { for i in 0..10 {
@ -159,7 +159,7 @@ fn from_1st_may_schedule_first_of_each_june() {
assert_eq!( assert_eq!(
next, next,
Local.with_ymd_and_hms(initial_year + i + 1, 06, 01, 00, 00, 00).unwrap() Local.with_ymd_and_hms(initial_year + i + 1, 6, 1, 0, 0, 0).unwrap()
); );
} }
} }

View File

@ -54,25 +54,25 @@ impl<Tz: TimeZone> LoolDate<Tz> {
/// adds `1` day to the current date /// adds `1` day to the current date
pub fn add_day(&mut self) { pub fn add_day(&mut self) {
self.date = self.date.clone() + Duration::days(1); self.date += Duration::days(1);
self.set_start_of(TimeUnit::Day); self.set_start_of(TimeUnit::Day);
} }
/// adds `1` hour to the current date /// adds `1` hour to the current date
pub fn add_hour(&mut self) { pub fn add_hour(&mut self) {
self.date = self.date.clone() + Duration::hours(1); self.date += Duration::hours(1);
self.set_start_of(TimeUnit::Hour); self.set_start_of(TimeUnit::Hour);
} }
/// adds `1` minute to the current date /// adds `1` minute to the current date
pub fn add_minute(&mut self) { pub fn add_minute(&mut self) {
self.date = self.date.clone() + Duration::minutes(1); self.date += Duration::minutes(1);
self.set_start_of(TimeUnit::Minute); self.set_start_of(TimeUnit::Minute);
} }
/// adds `1` second to the current date /// adds `1` second to the current date
pub fn add_second(&mut self) { pub fn add_second(&mut self) {
self.date = self.date.clone() + Duration::seconds(1); self.date += Duration::seconds(1);
self.set_start_of(TimeUnit::Second); self.set_start_of(TimeUnit::Second);
} }
@ -88,22 +88,22 @@ impl<Tz: TimeZone> LoolDate<Tz> {
/// subtracts `1` day from the current date /// subtracts `1` day from the current date
pub fn subs_day(&mut self) { pub fn subs_day(&mut self) {
self.date = self.date.clone() - Duration::days(1); self.date -= Duration::days(1);
} }
/// subtracts `1` hour from the current date /// subtracts `1` hour from the current date
pub fn subs_hour(&mut self) { pub fn subs_hour(&mut self) {
self.date = self.date.clone() - Duration::hours(1); self.date -= Duration::hours(1);
} }
/// subtracts `1` minute from the current date /// subtracts `1` minute from the current date
pub fn subs_minute(&mut self) { pub fn subs_minute(&mut self) {
self.date = self.date.clone() - Duration::minutes(1); self.date -= Duration::minutes(1);
} }
/// subtracts `1` second from the current date /// subtracts `1` second from the current date
pub fn subs_second(&mut self) { pub fn subs_second(&mut self) {
self.date = self.date.clone() - Duration::seconds(1); self.date -= Duration::seconds(1);
} }
/// returns the day of the month starting from `1` /// returns the day of the month starting from `1`
@ -152,7 +152,7 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// returns the number of milliseconds since the last second boundary /// returns the number of milliseconds since the last second boundary
/// ///
/// In event of a [leap second](https://en.wikipedia.org/wiki/Leap_second) this may exceed /// In event of a [leap second](https://en.wikipedia.org/wiki/Leap_second) this may exceed
/// `999`. /// `999`.
pub fn millis(&self) -> u64 { pub fn millis(&self) -> u64 {
@ -160,7 +160,7 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// Returns the number of microseconds since the last second boundary. /// Returns the number of microseconds since the last second boundary.
/// ///
/// In event of a [leap second](https://en.wikipedia.org/wiki/Leap_second) this may exceed /// In event of a [leap second](https://en.wikipedia.org/wiki/Leap_second) this may exceed
/// `999999`. /// `999999`.
pub fn micros(&self) -> u32 { pub fn micros(&self) -> u32 {
@ -168,7 +168,7 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// Returns the number of nanoseconds since the last second boundary. /// Returns the number of nanoseconds since the last second boundary.
/// ///
/// In event of a [leap second](https://en.wikipedia.org/wiki/Leap_second) this may exceed /// In event of a [leap second](https://en.wikipedia.org/wiki/Leap_second) this may exceed
/// `999999999`. /// `999999999`.
pub fn nanos(&self) -> u32 { pub fn nanos(&self) -> u32 {
@ -176,25 +176,33 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// sets the nanoseconds since the last second change /// sets the nanoseconds since the last second change
/// ///
/// values greater than `2000,000,000` will be clamped to `1999,999,999` /// values greater than `2000,000,000` will be clamped to `1999,999,999`
pub fn set_nanos(&mut self, nanos: u32) { pub fn set_nanos(&mut self, nanos: u32) {
// avoid `whith_nanosecond` returning None for > 2_000_000_000 values // avoid `whith_nanosecond` returning None for > 2_000_000_000 values
// so we can safely unwrap the result // so we can safely unwrap the result
let nanos = if nanos > 2_000_000_000 { 1_999_999_999 } else { nanos }; let nanos = if nanos > 2_000_000_000 {
1_999_999_999
} else {
nanos
};
self.date = self.date.with_nanosecond(nanos).unwrap(); self.date = self.date.with_nanosecond(nanos).unwrap();
} }
/// sets the microseconds since the last second change /// sets the microseconds since the last second change
/// ///
/// values greater than `2,000,000` will be clamped to `1,999,999` /// values greater than `2,000,000` will be clamped to `1,999,999`
pub fn set_micros(&mut self, micros: u32) { pub fn set_micros(&mut self, micros: u32) {
let micros = if micros > 2_000_000 { 1_999_999 } else { micros }; let micros = if micros > 2_000_000 {
self.date = self.date.with_nanosecond(micros as u32 * 1_000).unwrap(); 1_999_999
} else {
micros
};
self.date = self.date.with_nanosecond(micros * 1_000).unwrap();
} }
/// sets the milliseconds since the last second change /// sets the milliseconds since the last second change
/// ///
/// values greater than `2,000` will be clamped to `1,999` /// values greater than `2,000` will be clamped to `1,999`
pub fn set_millis(&mut self, millis: u64) { pub fn set_millis(&mut self, millis: u64) {
let millis = if millis > 2_000 { 1_999 } else { millis }; let millis = if millis > 2_000 { 1_999 } else { millis };
@ -202,7 +210,7 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// sets the second number of the date /// sets the second number of the date
/// ///
/// values >= `60` will be clamped to `0` /// values >= `60` will be clamped to `0`
pub fn set_second(&mut self, second: u32) { pub fn set_second(&mut self, second: u32) {
let second = if second >= 60 { 0 } else { second }; let second = if second >= 60 { 0 } else { second };
@ -210,7 +218,7 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// sets the minute number of the date /// sets the minute number of the date
/// ///
/// values >= `60` will be clamped to `0` /// values >= `60` will be clamped to `0`
pub fn set_minute(&mut self, minute: u32) { pub fn set_minute(&mut self, minute: u32) {
let minute = if minute >= 60 { 0 } else { minute }; let minute = if minute >= 60 { 0 } else { minute };
@ -218,7 +226,7 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// sets the hour number of the date `(0..23)` /// sets the hour number of the date `(0..23)`
/// ///
/// values >= `24` will be clamped to `0` /// values >= `24` will be clamped to `0`
pub fn set_hour(&mut self, hour: u32) { pub fn set_hour(&mut self, hour: u32) {
let hour = if hour >= 24 { 0 } else { hour }; let hour = if hour >= 24 { 0 } else { hour };
@ -226,11 +234,11 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// sets the hour, minute and second of the date at once /// sets the hour, minute and second of the date at once
/// ///
/// - `hour` must be in the range `(0..23)` /// - `hour` must be in the range `(0..23)`
/// - `minute` must be in the range `(0..59)` /// - `minute` must be in the range `(0..59)`
/// - `second` must be in the range `(0..59)` /// - `second` must be in the range `(0..59)`
/// ///
/// values greater than the maximum will be clamped (see `set_hour`, `set_minute`, `set_second`) /// values greater than the maximum will be clamped (see `set_hour`, `set_minute`, `set_second`)
/// documentation for more information /// documentation for more information
pub fn set_hms(&mut self, hour: u32, minute: u32, second: u32) { pub fn set_hms(&mut self, hour: u32, minute: u32, second: u32) {
@ -240,7 +248,7 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// sets the day of the month `(1..31)` /// sets the day of the month `(1..31)`
/// ///
/// values greater than the maximum day of the month will be clamped to the last day of the /// values greater than the maximum day of the month will be clamped to the last day of the
/// month, depending on the current year and month and taking leap years into account. /// month, depending on the current year and month and taking leap years into account.
pub fn set_day(&mut self, day: u32) { pub fn set_day(&mut self, day: u32) {
@ -255,10 +263,10 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// sets the month of the year `(1..12)` /// sets the month of the year `(1..12)`
/// ///
/// - values greater than the maximum month will be clamped to `12` /// - values greater than the maximum month will be clamped to `12`
/// - values less than `1` will be clamped to `1` /// - values less than `1` will be clamped to `1`
/// ///
/// if the current day is greater than the last day of the new month, the day will be clamped /// if the current day is greater than the last day of the new month, the day will be clamped
/// to the last day of the month, depending on the current year and month and taking leap years /// to the last day of the month, depending on the current year and month and taking leap years
/// into account. So, be aware that changing the month may imply a change in the day. /// into account. So, be aware that changing the month may imply a change in the day.
@ -285,7 +293,7 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// sets the month and day of the date at once /// sets the month and day of the date at once
/// ///
/// check the `set_month` and `set_day` documentation for more information about the clamping /// check the `set_month` and `set_day` documentation for more information about the clamping
/// behavior. /// behavior.
pub fn set_md(&mut self, month: u32, day: u32) { pub fn set_md(&mut self, month: u32, day: u32) {
@ -294,12 +302,12 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// sets the year of the date /// sets the year of the date
/// ///
/// **warning**: changing the year may imply a change in the day. /// **warning**: changing the year may imply a change in the day.
/// For example, if the current date is `2024-02-29` and you set the year to `2023`, the date /// For example, if the current date is `2024-02-29` and you set the year to `2023`, the date
/// date `2023-02-29` will be invalid, because `2023` is not a leap year. In this case, the day /// date `2023-02-29` will be invalid, because `2023` is not a leap year. In this case, the day
/// will be clamped to `2023-02-28`. /// will be clamped to `2023-02-28`.
/// ///
/// check the `set_day` documentation for more information about the clamping behavior. /// check the `set_day` documentation for more information about the clamping behavior.
pub fn set_year(&mut self, year: i32) { pub fn set_year(&mut self, year: i32) {
let month = self.date.month(); let month = self.date.month();
@ -315,7 +323,7 @@ impl<Tz: TimeZone> LoolDate<Tz> {
} }
/// sets the year, month and day of the date at once /// sets the year, month and day of the date at once
/// ///
/// check the `set_year`, `set_month` and `set_day` documentation for more information about the /// check the `set_year`, `set_month` and `set_day` documentation for more information about the
/// clamping behavior. /// clamping behavior.
pub fn set_ymd(&mut self, year: i32, month: u32, day: u32) { pub fn set_ymd(&mut self, year: i32, month: u32, day: u32) {
@ -347,32 +355,32 @@ impl<Tz: TimeZone> LoolDate<Tz> {
self.set_minute(0); self.set_minute(0);
self.set_second(0); self.set_second(0);
self.set_nanos(0); self.set_nanos(0);
}, }
TimeUnit::Month => { TimeUnit::Month => {
self.set_day(1); self.set_day(1);
self.set_hour(0); self.set_hour(0);
self.set_minute(0); self.set_minute(0);
self.set_second(0); self.set_second(0);
self.set_nanos(0); self.set_nanos(0);
}, }
TimeUnit::Day => { TimeUnit::Day => {
self.set_hour(0); self.set_hour(0);
self.set_minute(0); self.set_minute(0);
self.set_second(0); self.set_second(0);
self.set_nanos(0); self.set_nanos(0);
}, }
TimeUnit::Hour => { TimeUnit::Hour => {
self.set_minute(0); self.set_minute(0);
self.set_second(0); self.set_second(0);
self.set_nanos(0); self.set_nanos(0);
}, }
TimeUnit::Minute => { TimeUnit::Minute => {
self.set_second(0); self.set_second(0);
self.set_nanos(0); self.set_nanos(0);
}, }
TimeUnit::Second => { TimeUnit::Second => {
self.set_nanos(0); self.set_nanos(0);
}, }
}; };
} }
} }

View File

@ -10,7 +10,7 @@ const INVALID_OFFSET_MIN_ERR: &str = "invalid timezone offset (minute should be
/// 🧉 » converts `hours` and `minutes` durations to total seconds /// 🧉 » converts `hours` and `minutes` durations to total seconds
/// ///
/// e.g. `h=1, m=30` should return `5400` /// e.g. `h=1, m=30` should return `5400`
/// ///
/// meaning `1 hour and 30 minutes` is `5400` seconds /// meaning `1 hour and 30 minutes` is `5400` seconds
pub fn hm_to_s(h: i32, m: i32) -> i32 { pub fn hm_to_s(h: i32, m: i32) -> i32 {
h * 3600 + m * 60 h * 3600 + m * 60
@ -48,7 +48,7 @@ pub fn tz_to_s(offset: &str) -> Result<i32> {
0 0
}; };
// offset hours cannot be greater than 14, minutes cannot be greater than 59 and // offset hours cannot be greater than 14, minutes cannot be greater than 59 and
// seconds cannot be greater than 59 // seconds cannot be greater than 59
ensure!(hours <= 14, INVALID_OFFSET_HS_ERR); ensure!(hours <= 14, INVALID_OFFSET_HS_ERR);
ensure!(minutes <= 59, INVALID_OFFSET_MIN_ERR); ensure!(minutes <= 59, INVALID_OFFSET_MIN_ERR);
@ -58,8 +58,10 @@ pub fn tz_to_s(offset: &str) -> Result<i32> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use eyre::{set_hook, DefaultHandler}; use {
use super::*; super::*,
eyre::{set_hook, DefaultHandler},
};
fn setup_eyre() { fn setup_eyre() {
let _ = set_hook(Box::new(DefaultHandler::default_with)); let _ = set_hook(Box::new(DefaultHandler::default_with));