summaryrefslogtreecommitdiff
path: root/src/dates.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/dates.rs')
-rw-r--r--src/dates.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/dates.rs b/src/dates.rs
new file mode 100644
index 0000000..fc3863c
--- /dev/null
+++ b/src/dates.rs
@@ -0,0 +1,60 @@
+use std::path::Path;
+// use std::str::FromStr;
+// use chrono::{Datelike, NaiveDate, NaiveTime, Weekday, Duration, Timelike};
+use chrono::{Datelike, NaiveDate, Duration};
+
+pub struct WTDPath<'a> {
+ pub path: &'a Path,
+ pub week_start: Option<NaiveDate>,
+ pub is_weekly_file: bool,
+}
+
+// both start, end are inclusive.
+pub fn relevant_files<'a>(start: &'a NaiveDate, end: &'a NaiveDate) -> Vec<WTDPath<'a>> {
+ let mut paths: Vec<WTDPath> = vec![];
+ if let Some(path) = maybe_path("wtd.md".to_string()) {
+ paths.push(WTDPath {
+ path: &path,
+ week_start: None,
+ is_weekly_file: false,
+ });
+ }
+
+ let weekly = maybe_path("weekly.md".to_string());
+
+ let start_week = week_start_of(start);
+ let end_week = week_start_of(end); // inclusive
+
+ let mut curr_week = start_week;
+ while curr_week <= end_week {
+ let maybe_week_path = week_file(curr_week);
+ let path = match [maybe_week_path, weekly] {
+ [Some(path), _] => path,
+ [_, Some(path)] => path,
+ [None, None] => continue,
+ };
+ paths.push(WTDPath {
+ path: path,
+ week_start: Some(curr_week),
+ is_weekly_file: maybe_week_path.is_none(),
+ });
+ curr_week = curr_week + Duration::weeks(1);
+ }
+
+ return paths;
+}
+
+fn week_file(week_start: NaiveDate) -> Option<&'static Path> {
+ let filename = week_start.format("%b_%d_%Y.md").to_string().to_lowercase();
+ return maybe_path(filename);
+}
+
+fn maybe_path(name: String) -> Option<&'static Path> {
+ let path = Path::new("wtd.md");
+ return if path.exists() { Some(&path) } else { None };
+}
+
+fn week_start_of(date: &NaiveDate) -> NaiveDate {
+ let days_from_monday = date.weekday().number_from_monday();
+ return *date - Duration::days(days_from_monday.into());
+}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback