Consolidate 12 hr and 24 hr span adjustments into a helper

This commit is contained in:
Campbell Alden 2024-09-10 21:53:45 +09:00
parent 283876ebda
commit 0884006809

View file

@ -30,6 +30,24 @@ fn parse_time(time_str: &str) -> Result<NaiveDateTime> {
Ok(naive_date) Ok(naive_date)
} }
fn adjust_last(first: &NaiveTime, second: NaiveTime) -> NaiveTime {
// The following logic attempts to handle wrapping from AM -> PM and also from PM ->
// AM but cannot handle multiple days in a single span
if (second < *first) && first.signed_duration_since(epoch()).num_hours() < 12 {
// Assuming wrapped to PM
// first = 8:00
// second: 1:00
second + Duration::hours(12)
} else if second < *first {
// Wrapped to a new day because first was after noon.
// first = 14:40
// second = 1:30
second + Duration::hours(24)
} else {
second
}
}
/// The default way of calculating time. Time values are given one per line and subsequent pairs of /// The default way of calculating time. Time values are given one per line and subsequent pairs of
/// lines are considered time spans. If an odd number of spans is given, then the final time value /// lines are considered time spans. If an odd number of spans is given, then the final time value
/// is ignored. /// is ignored.
@ -44,22 +62,9 @@ fn all_at_once(is_terminal: bool) -> Result<Vec<Duration>> {
let cleaned = line?.trim().to_string(); let cleaned = line?.trim().to_string();
if let Some(previous) = &seen { if let Some(previous) = &seen {
let first = parse_time(previous)?; let first = parse_time(previous)?;
let mut second = parse_time(&cleaned)?; let second = adjust_last(&first.time(), parse_time(&cleaned)?.time());
// The following logic attempts to handle wrapping from AM -> PM and also from PM -> durations.push(second - first.time());
// AM but cannot handle multiple days in a single span
if (second < first) && first.time().signed_duration_since(epoch()).num_hours() < 12 {
// Assuming wrapped to PM
// first = 8:00
// second: 1:00
second = second + Duration::hours(12);
} else if second < first {
// Wrapped to a new day because first was after noon.
// first = 14:40
// second = 1:30
second = second + Duration::hours(24)
}
durations.push(second - first);
seen = None; seen = None;
} else { } else {
seen = Some(cleaned.to_string()); seen = Some(cleaned.to_string());
@ -80,9 +85,7 @@ fn live_spans() -> Result<Vec<Duration>> {
for _line in stdin().lock().lines() { for _line in stdin().lock().lines() {
let mut now = Local::now().naive_local().time(); let mut now = Local::now().naive_local().time();
if let Some(previous) = &seen { if let Some(previous) = &seen {
if now < *previous { now = adjust_last(previous, now);
now = now + Duration::hours(12);
}
let prev_time = previous.format("%H:%M"); let prev_time = previous.format("%H:%M");
let now_time = now.format("%H:%M"); let now_time = now.format("%H:%M");
println!("Closing span from {prev_time} - {now_time}"); println!("Closing span from {prev_time} - {now_time}");
@ -99,9 +102,7 @@ fn live_spans() -> Result<Vec<Duration>> {
if let Some(unpaired) = seen { if let Some(unpaired) = seen {
println!("Closing unpaired span now"); println!("Closing unpaired span now");
let mut now = Local::now().naive_local().time(); let mut now = Local::now().naive_local().time();
if now < unpaired { now = adjust_last(&unpaired, now);
now = now + Duration::hours(12);
}
durations.push(now - unpaired); durations.push(now - unpaired);
} }
return Ok(durations); return Ok(durations);