diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-08-04 21:55:51 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-08-04 21:55:51 +0200 |
| commit | 75c238343b8868bc7ff060bee6c99f2d30faee3b (patch) | |
| tree | f99ebe51f79dab63ab4c99b60b62dc3b5ee52af7 | |
| parent | 8dcdd9d9ddb624dbce98ee9e1b53a2988e7c7fce (diff) | |
| download | money-75c238343b8868bc7ff060bee6c99f2d30faee3b.tar.gz | |
bucket month
| -rw-r--r-- | cli/src/search.rs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/cli/src/search.rs b/cli/src/search.rs index fb7f5dc..972913f 100644 --- a/cli/src/search.rs +++ b/cli/src/search.rs @@ -145,6 +145,10 @@ impl Constraint { |_| Constraint::Filters(this_week()), ), map( + tag("month"), + |_| Constraint::Filters(this_month()), + ), + map( delimited(char('('), separated_list0(space1, Filter::parse), char(')')), Constraint::Filters, ), @@ -152,15 +156,29 @@ impl Constraint { } } +fn filter_between(start: NaiveDate, end: NaiveDate) -> Vec<Filter> { + vec![ + Filter::Intersect(Constraint::After(DateIsh::Absolute(start))), + Filter::Intersect(Constraint::Before(DateIsh::Absolute(end))), + ] +} + fn this_week() -> Vec<Filter> { let today = chrono::Local::today().naive_utc(); let to_last_monday = Duration::days(today.weekday().num_days_from_monday() as i64); let last_monday = today - to_last_monday; let next_monday = last_monday + Duration::weeks(1); - vec![ - Filter::Intersect(Constraint::After(DateIsh::Absolute(last_monday))), - Filter::Intersect(Constraint::Before(DateIsh::Absolute(next_monday))), - ] + filter_between(last_monday, next_monday) +} + +fn this_month() -> Vec<Filter> { + let today = chrono::Local::today().naive_utc(); + let start_of_month = today.with_day(1).unwrap(); + let start_of_next_month = match start_of_month.month() { + 12 => start_of_month.with_year(start_of_month.year() + 1).unwrap().with_month(1).unwrap(), + _ => start_of_month.with_month(start_of_month.month() + 1).unwrap(), + }; + filter_between(start_of_month, start_of_next_month) } fn string(i: &str) -> nom::IResult<&str, &str> { |
