From 2b55df907bc556774d1bd7538fdd24270262d567 Mon Sep 17 00:00:00 2001 From: Campbell Alden Date: Thu, 26 Dec 2024 18:47:35 +0900 Subject: [PATCH 1/4] Count end time from the last time if the last time is in the future --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 6 ++++-- src/time.rs | 19 ++++++++++++++++--- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db20599..925ae63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -384,7 +384,7 @@ dependencies = [ [[package]] name = "time-track" -version = "2.1.2" +version = "2.1.3" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 76bb2be..e318688 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "time-track" -version = "2.1.2" +version = "2.1.3" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index 4ea78a7..19dc159 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,7 @@ fn main() -> Result<()> { let mut total_minutes: i64 = 0; let mut first: Option> = None; + let mut last: Option> = None; for time in times { match first { None => { @@ -46,7 +47,8 @@ fn main() -> Result<()> { }, Some(prev) => { total_minutes += (time - prev).num_minutes(); - first = None + first = None; + last = Some(time); } } } @@ -58,6 +60,6 @@ fn main() -> Result<()> { total_minutes += (now - remaining).num_minutes(); } - println!("{}", time::get_charaterized_time_remaining(total_minutes, target_minutes)); + println!("{}", time::get_charaterized_time_remaining(total_minutes, target_minutes, last.unwrap_or_else(|| Local::now()))); Ok(()) } diff --git a/src/time.rs b/src/time.rs index 6d49767..eb0e0cf 100644 --- a/src/time.rs +++ b/src/time.rs @@ -54,7 +54,11 @@ pub fn to_hrs_minutes(total_minutes: i64) -> (i64, i64) { (hours, minutes) } -pub fn get_charaterized_time_remaining(total_minutes: i64, target_minutes: i64) -> String { +pub fn get_charaterized_time_remaining( + total_minutes: i64, + target_minutes: i64, + ended_at: DateTime, +) -> String { if total_minutes == target_minutes { return "Exactly done".to_string(); } @@ -66,8 +70,17 @@ pub fn get_charaterized_time_remaining(total_minutes: i64, target_minutes: i64) } else { let diff = target_minutes - total_minutes; let (hours, minutes) = to_hrs_minutes(diff); - let end_at = (Local::now() + Duration::minutes(diff)).time(); + let end_at = (ended_at + Duration::minutes(diff)).time(); let end_str = end_at.format("%-I:%M %p"); - return format!("You have {} remaining (end at {} starting now)", show_time(hours, minutes), end_str) + 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"), + ) + } else { + format!("You have {} remaining (end at {} starting now)", show_time(hours, minutes), end_str) + } } } From 51a200d1a3097264d0985f03905058efc69f9a0e Mon Sep 17 00:00:00 2001 From: Campbell Alden Date: Fri, 27 Dec 2024 09:36:49 +0900 Subject: [PATCH 2/4] 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) } } } From e2c0b08c032ecec8f27236f4f7132519472a165f Mon Sep 17 00:00:00 2001 From: Campbell Alden Date: Sun, 20 Apr 2025 22:11:19 +0900 Subject: [PATCH 3/4] Create README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..21c3754 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# time-track + +A simple CLI tool for tracking how much time you have left to work. I find myself stressing about whether I'm hitting 8 real hours, so this little tool helps me avoid wasting +time calculating when my work day will end. + +Simply enter times, one per line and send an EOF character when you're done. The first lines opens a span of work and the next line closes it so that you can build up working +time be clocking in and out. Finally, you can send additional arguments to the program to configure how long you intend to work (the default is 8 hours). From 4a86e930f9120149a5cfcb9bace10d021cc888f6 Mon Sep 17 00:00:00 2001 From: Campbell Alden Date: Sun, 20 Apr 2025 22:18:34 +0900 Subject: [PATCH 4/4] Update README.md --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 21c3754..bbcf4bc 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,18 @@ time calculating when my work day will end. Simply enter times, one per line and send an EOF character when you're done. The first lines opens a span of work and the next line closes it so that you can build up working time be clocking in and out. Finally, you can send additional arguments to the program to configure how long you intend to work (the default is 8 hours). + +``` +❯ time-track +Working for 8 hours +Input times one per line. Send an EOF character to finish inputting... +8:30 +9:30 +11:15 +12:30 +13:16 +18:20 +19:30 +20:11 # Send an EOF +Exactly done +```