Bug Fix: Count time from now if the end time is before now

This commit is contained in:
Campbell Alden 2024-12-27 09:36:49 +09:00
parent 2b55df907b
commit 51a200d1a3
3 changed files with 15 additions and 12 deletions

2
Cargo.lock generated
View file

@ -384,7 +384,7 @@ dependencies = [
[[package]] [[package]]
name = "time-track" name = "time-track"
version = "2.1.3" version = "2.1.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "time-track" name = "time-track"
version = "2.1.3" version = "2.1.4"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -70,17 +70,20 @@ pub fn get_charaterized_time_remaining(
} else { } else {
let diff = target_minutes - total_minutes; let diff = target_minutes - total_minutes;
let (hours, minutes) = to_hrs_minutes(diff); let (hours, minutes) = to_hrs_minutes(diff);
let end_at = (ended_at + Duration::minutes(diff)).time(); let now = Local::now();
let end_str = end_at.format("%-I:%M %p"); return if ended_at > now {
return if ended_at > Local::now() { let end_at = (ended_at + Duration::minutes(diff)).time();
format!( let end_str = end_at.format("%-I:%M %p");
"You have {} remaining (end at {} starting from {})", format!(
show_time(hours, minutes), "You have {} remaining (end at {} starting from {})",
end_str, show_time(hours, minutes),
ended_at.format("%-I:%M %p"), end_str,
) ended_at.format("%-I:%M %p"),
)
} else { } else {
format!("You have {} remaining (end at {} starting now)", show_time(hours, minutes), end_str) let end_at = (now + Duration::minutes(diff)).time();
let end_str = end_at.format("%-I:%M %p");
format!("You have {} remaining (end at {} starting now)", show_time(hours, minutes), end_str)
} }
} }
} }