use crate::todo;
use crate::todotxt;
pub fn is_timer_on(task: &todotxt::Task) -> bool {
if let Some(state) = task.tags.get(todo::TIMER_TAG) {
return state != todo::TIMER_OFF;
}
false
}
pub fn spent_time(task: &todotxt::Task) -> chrono::Duration {
if is_timer_on(task) {
return match calc_time_spent(task) {
Some(n) => chrono::Duration::seconds(n),
None => chrono::Duration::seconds(0),
};
}
if let Some(sp) = task.tags.get(todo::SPENT_TAG) {
if let Ok(n) = sp.parse::<i64>() { chrono::Duration::seconds(n) } else { chrono::Duration::seconds(0) }
} else {
chrono::Duration::seconds(0)
}
}
pub fn start_timer(task: &mut todotxt::Task) -> bool {
if task.finished || is_timer_on(task) {
return false;
}
let utc = chrono::Utc::now();
let seconds = format!("{}", utc.timestamp());
task.update_tag_with_value(todo::TIMER_TAG, &seconds);
true
}
fn calc_time_spent(task: &todotxt::Task) -> Option<i64> {
if let Some(started) = task.tags.get(todo::TIMER_TAG)
&& let Ok(n) = started.parse::<i64>()
{
let dt_start = chrono::DateTime::from_timestamp(n, 0)?;
let diff = chrono::Utc::now() - dt_start;
let mut spent: i64 =
if let Some(sp) = task.tags.get(todo::SPENT_TAG) { sp.parse::<i64>().unwrap_or(0) } else { 0 };
if diff.num_seconds() > 0 {
spent += diff.num_seconds();
}
return Some(spent);
}
None
}
pub fn stop_timer(task: &mut todotxt::Task) -> bool {
if !is_timer_on(task) {
return false;
}
if let Some(spent) = calc_time_spent(task) {
let new_spent = format!("{spent}");
task.update_tag_with_value(todo::SPENT_TAG, &new_spent);
task.update_tag_with_value(todo::TIMER_TAG, todo::TIMER_OFF);
return true;
}
false
}