forked from janek/mostr
feat(main): neatly interpret plain hour in date filter
This commit is contained in:
parent
9da41db427
commit
77ba311bab
|
@ -1,7 +1,7 @@
|
|||
use std::fmt::Display;
|
||||
use std::io::{stdin, stdout, Write};
|
||||
use std::ops::Sub;
|
||||
|
||||
use chrono::{DateTime, Local, NaiveDateTime, TimeZone, Utc};
|
||||
use chrono::{DateTime, Local, TimeDelta, TimeZone, Utc};
|
||||
use chrono::LocalResult::Single;
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use nostr_sdk::Timestamp;
|
||||
|
@ -19,6 +19,22 @@ pub fn prompt(prompt: &str) -> Option<String> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Parses the hour from a plain number in the String,
|
||||
/// with max of max_future hours into the future.
|
||||
pub fn parse_hour(str: &str, max_future: i64) -> Option<DateTime<Local>> {
|
||||
str.parse::<u32>().ok().and_then(|hour| {
|
||||
let now = Local::now();
|
||||
#[allow(deprecated)]
|
||||
now.date().and_hms_opt(hour, 0, 0).map(|time| {
|
||||
if time - now > TimeDelta::hours(max_future) {
|
||||
time.sub(TimeDelta::days(1))
|
||||
} else {
|
||||
time
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_date(str: &str) -> Option<DateTime<Utc>> {
|
||||
// Using two libraries for better exhaustiveness, see https://github.com/uutils/parse_datetime/issues/84
|
||||
match interim::parse_date_string(str, Local::now(), interim::Dialect::Us) {
|
||||
|
|
25
src/main.rs
25
src/main.rs
|
@ -10,6 +10,7 @@ use std::path::PathBuf;
|
|||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
|
||||
use chrono::Local;
|
||||
use colored::Colorize;
|
||||
use env_logger::{Builder, Target, WriteStyle};
|
||||
use itertools::Itertools;
|
||||
|
@ -27,7 +28,7 @@ use xdg::BaseDirectories;
|
|||
|
||||
use crate::helpers::*;
|
||||
use crate::kinds::{KINDS, PROP_KINDS, PROPERTY_COLUMNS, TRACKING_KIND};
|
||||
use crate::task::{MARKER_DEPENDS, MARKER_PARENT, State};
|
||||
use crate::task::{MARKER_DEPENDS, State};
|
||||
use crate::tasks::{PropertyCollection, StateFilter, Tasks};
|
||||
|
||||
mod helpers;
|
||||
|
@ -403,7 +404,7 @@ async fn main() {
|
|||
match arg {
|
||||
None => {
|
||||
let today = Timestamp::from(Timestamp::now() - 80_000);
|
||||
info!("Filtering for tasks from the last 22 hours");
|
||||
info!("Filtering for tasks created in the last 22 hours");
|
||||
tasks.set_filter(
|
||||
tasks.filtered_tasks(tasks.get_position_ref())
|
||||
.filter(|t| t.event.created_at > today)
|
||||
|
@ -431,15 +432,17 @@ async fn main() {
|
|||
.collect()
|
||||
)
|
||||
} else {
|
||||
parse_date(arg).map(|time| {
|
||||
info!("Filtering for tasks from {}", time); // TODO localize
|
||||
tasks.set_filter(
|
||||
tasks.filtered_tasks(tasks.get_position_ref())
|
||||
.filter(|t| t.event.created_at.as_u64() as i64 > time.timestamp())
|
||||
.map(|t| t.event.id)
|
||||
.collect()
|
||||
);
|
||||
});
|
||||
parse_hour(arg, 1)
|
||||
.or_else(|| parse_date(arg).map(|utc| utc.with_timezone(&Local)))
|
||||
.map(|time| {
|
||||
info!("Filtering for tasks created after {}", time);
|
||||
tasks.set_filter(
|
||||
tasks.filtered_tasks(tasks.get_position_ref())
|
||||
.filter(|t| t.event.created_at.as_u64() as i64 > time.to_utc().timestamp())
|
||||
.map(|t| t.event.id)
|
||||
.collect()
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue