Count end time from the last time if the last time is in the future
This commit is contained in:
parent
9087e69c79
commit
2b55df907b
4 changed files with 22 additions and 7 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -384,7 +384,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time-track"
|
name = "time-track"
|
||||||
version = "2.1.2"
|
version = "2.1.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "time-track"
|
name = "time-track"
|
||||||
version = "2.1.2"
|
version = "2.1.3"
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
let mut total_minutes: i64 = 0;
|
let mut total_minutes: i64 = 0;
|
||||||
let mut first: Option<DateTime<Local>> = None;
|
let mut first: Option<DateTime<Local>> = None;
|
||||||
|
let mut last: Option<DateTime<Local>> = None;
|
||||||
for time in times {
|
for time in times {
|
||||||
match first {
|
match first {
|
||||||
None => {
|
None => {
|
||||||
|
|
@ -46,7 +47,8 @@ fn main() -> Result<()> {
|
||||||
},
|
},
|
||||||
Some(prev) => {
|
Some(prev) => {
|
||||||
total_minutes += (time - prev).num_minutes();
|
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();
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
src/time.rs
19
src/time.rs
|
|
@ -54,7 +54,11 @@ pub fn to_hrs_minutes(total_minutes: i64) -> (i64, i64) {
|
||||||
(hours, minutes)
|
(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<Local>,
|
||||||
|
) -> String {
|
||||||
if total_minutes == target_minutes {
|
if total_minutes == target_minutes {
|
||||||
return "Exactly done".to_string();
|
return "Exactly done".to_string();
|
||||||
}
|
}
|
||||||
|
|
@ -66,8 +70,17 @@ pub fn get_charaterized_time_remaining(total_minutes: i64, target_minutes: i64)
|
||||||
} 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 = (Local::now() + Duration::minutes(diff)).time();
|
let end_at = (ended_at + Duration::minutes(diff)).time();
|
||||||
let end_str = end_at.format("%-I:%M %p");
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue