Renaming project again
This commit is contained in:
1740
Cargo.lock
generated
1740
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "cronkin"
|
name = "flexcal"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
@@ -7,8 +7,5 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = { version = "0.4", features = ["serde"] }
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
actix-web = "3"
|
|
||||||
crossbeam = "0.8"
|
|
||||||
pam = "0.7"
|
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ use std::collections::HashSet;
|
|||||||
/*
|
/*
|
||||||
There are a few gaping holes here, and some functional deficiencies:
|
There are a few gaping holes here, and some functional deficiencies:
|
||||||
|
|
||||||
- Holidays are only calculated within a year. If a holiday in a prior
|
- workdays are only calculated within a year. If a workday in a prior
|
||||||
year is bumped to the next year, it won't be considered.
|
year is bumped to the next year, it won't be considered.
|
||||||
- Holiday impact is searched forward. If there is a mix of AdjustmentPolicy's
|
- workday impact is searched forward. If there is a mix of AdjustmentPolicy's
|
||||||
then some weird stuff can happen (Holidays occur A, B, but end up getting
|
then some weird stuff can happen (workdays occur A, B, but end up getting
|
||||||
observed B, A)
|
observed B, A)
|
||||||
- No support for holiday ranges (e.g. Golden Week)
|
- No support for workday ranges (e.g. Golden Week)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
|
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
|
||||||
@@ -157,13 +157,13 @@ pub struct Calendar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Calendar {
|
impl Calendar {
|
||||||
fn adjust_holidays(&self, holidays: &Vec<(NaiveDate, AdjustmentPolicy)>) -> HashSet<NaiveDate> {
|
fn adjust_workdays(&self, workdays: &Vec<(NaiveDate, AdjustmentPolicy)>) -> HashSet<NaiveDate> {
|
||||||
let mut observed = HashSet::new();
|
let mut observed = HashSet::new();
|
||||||
|
|
||||||
for (date, policy) in holidays.iter() {
|
for (date, policy) in workdays.iter() {
|
||||||
match self.adjust_holiday(*date, policy, &observed) {
|
match self.adjust_workday(*date, policy, &observed) {
|
||||||
Some(holiday) => {
|
Some(workday) => {
|
||||||
observed.insert(holiday);
|
observed.insert(workday);
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
@@ -172,19 +172,19 @@ impl Calendar {
|
|||||||
observed
|
observed
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adjust a date given existing holidays and adjustment policy
|
/// Adjust a date given existing workdays and adjustment policy
|
||||||
fn adjust_holiday(
|
fn adjust_workday(
|
||||||
&self,
|
&self,
|
||||||
date: NaiveDate,
|
date: NaiveDate,
|
||||||
policy: &AdjustmentPolicy,
|
policy: &AdjustmentPolicy,
|
||||||
holidays: &HashSet<NaiveDate>,
|
workdays: &HashSet<NaiveDate>,
|
||||||
) -> Option<NaiveDate> {
|
) -> Option<NaiveDate> {
|
||||||
let mut actual = date.clone();
|
let mut actual = date.clone();
|
||||||
|
|
||||||
println!("Adjusting {:?}", date);
|
println!("Adjusting {:?}", date);
|
||||||
|
|
||||||
let is_blocked =
|
let is_blocked =
|
||||||
|x: NaiveDate| -> bool { (!self.dow.contains(&x.weekday())) || holidays.contains(&x) };
|
|x: NaiveDate| -> bool { (!self.dow.contains(&x.weekday())) || workdays.contains(&x) };
|
||||||
|
|
||||||
use AdjustmentPolicy::*;
|
use AdjustmentPolicy::*;
|
||||||
match policy {
|
match policy {
|
||||||
@@ -202,10 +202,10 @@ impl Calendar {
|
|||||||
}
|
}
|
||||||
Closest => {
|
Closest => {
|
||||||
let prev = self
|
let prev = self
|
||||||
.adjust_holiday(date, &AdjustmentPolicy::Prev, holidays)
|
.adjust_workday(date, &AdjustmentPolicy::Prev, workdays)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let next = self
|
let next = self
|
||||||
.adjust_holiday(date, &AdjustmentPolicy::Next, holidays)
|
.adjust_workday(date, &AdjustmentPolicy::Next, workdays)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
if (date - prev) < (next - date) {
|
if (date - prev) < (next - date) {
|
||||||
Some(prev)
|
Some(prev)
|
||||||
@@ -223,21 +223,21 @@ impl Calendar {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the set of all holidays in a given year
|
/// Get the set of all workdays in a given year
|
||||||
pub fn get_holidays(&self, date: NaiveDate) -> HashSet<NaiveDate> {
|
pub fn get_workdays(&self, date: NaiveDate) -> HashSet<NaiveDate> {
|
||||||
let holidays: Vec<(NaiveDate, AdjustmentPolicy)> = self
|
let workdays: Vec<(NaiveDate, AdjustmentPolicy)> = self
|
||||||
.exclude
|
.exclude
|
||||||
.iter()
|
.iter()
|
||||||
.map(|x| x.resolve(date.year()))
|
.map(|x| x.resolve(date.year()))
|
||||||
.filter(|x| x.is_some())
|
.filter(|x| x.is_some())
|
||||||
.map(|x| x.unwrap())
|
.map(|x| x.unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
self.adjust_holidays(&holidays)
|
self.adjust_workdays(&workdays)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the given date is a holiday / non-business day
|
/// Returns true if the given date is a workday / non-business day
|
||||||
fn is_holiday(&self, date: NaiveDate) -> bool {
|
fn is_workday(&self, date: NaiveDate) -> bool {
|
||||||
self.get_holidays(date).contains(&date)
|
self.get_workdays(date).contains(&date)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the set of valid calendar dates within the specified range
|
/// Returns the set of valid calendar dates within the specified range
|
||||||
@@ -245,14 +245,14 @@ impl Calendar {
|
|||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
let mut cur = from.pred();
|
let mut cur = from.pred();
|
||||||
let year = from.year();
|
let year = from.year();
|
||||||
let mut holidays = self.get_holidays(cur);
|
let mut workdays = self.get_workdays(cur);
|
||||||
|
|
||||||
while cur <= to {
|
while cur <= to {
|
||||||
cur = cur.succ();
|
cur = cur.succ();
|
||||||
if cur.year() != year {
|
if cur.year() != year {
|
||||||
holidays = self.get_holidays(cur);
|
workdays = self.get_workdays(cur);
|
||||||
}
|
}
|
||||||
if !self.dow.contains(&cur.weekday()) || holidays.contains(&cur) {
|
if !self.dow.contains(&cur.weekday()) || workdays.contains(&cur) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
result.push(cur);
|
result.push(cur);
|
||||||
@@ -296,12 +296,12 @@ mod tests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Christmas falls on a Saturday, observed was Monday
|
// Christmas falls on a Saturday, observed was Monday
|
||||||
assert!(cal.is_holiday(NaiveDate::from_ymd(2021, 12, 27)));
|
assert!(cal.is_workday(NaiveDate::from_ymd(2021, 12, 27)));
|
||||||
|
|
||||||
// Boxing Day falls on a Sunday, observed is a Tuesday
|
// Boxing Day falls on a Sunday, observed is a Tuesday
|
||||||
assert!(cal.is_holiday(NaiveDate::from_ymd(2021, 12, 28)));
|
assert!(cal.is_workday(NaiveDate::from_ymd(2021, 12, 28)));
|
||||||
|
|
||||||
assert!(!cal.is_holiday(NaiveDate::from_ymd(2021, 12, 24)));
|
assert!(!cal.is_workday(NaiveDate::from_ymd(2021, 12, 24)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -379,6 +379,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
}"#;
|
}"#;
|
||||||
let cal: Calendar = serde_json::from_str(data).unwrap();
|
let res: serde_json::Result<Calendar> = serde_json::from_str(data);
|
||||||
|
assert!(res.is_ok());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user