From 51a200d1a3097264d0985f03905058efc69f9a0e Mon Sep 17 00:00:00 2001 From: Campbell Alden Date: Fri, 27 Dec 2024 09:36:49 +0900 Subject: [PATCH] Bug Fix: Count time from now if the end time is before now --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/time.rs | 23 +++++++++++++---------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 925ae63..e66d302 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -384,7 +384,7 @@ dependencies = [ [[package]] name = "time-track" -version = "2.1.3" +version = "2.1.4" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index e318688..74de412 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "time-track" -version = "2.1.3" +version = "2.1.4" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/time.rs b/src/time.rs index eb0e0cf..2b31d50 100644 --- a/src/time.rs +++ b/src/time.rs @@ -70,17 +70,20 @@ pub fn get_charaterized_time_remaining( } else { let diff = target_minutes - total_minutes; let (hours, minutes) = to_hrs_minutes(diff); - let end_at = (ended_at + Duration::minutes(diff)).time(); - let end_str = end_at.format("%-I:%M %p"); - return if ended_at > Local::now() { - format!( - "You have {} remaining (end at {} starting from {})", - show_time(hours, minutes), - end_str, - ended_at.format("%-I:%M %p"), - ) + let now = Local::now(); + return if ended_at > now { + let end_at = (ended_at + Duration::minutes(diff)).time(); + let end_str = end_at.format("%-I:%M %p"); + format!( + "You have {} remaining (end at {} starting from {})", + show_time(hours, minutes), + end_str, + ended_at.format("%-I:%M %p"), + ) } 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) } } }