Skip to content

Commit

Permalink
[fix] durations can be <= 0 when using '--end'
Browse files Browse the repository at this point in the history
  • Loading branch information
icepuma committed Dec 1, 2021
1 parent 0469e2c commit bb8dc10
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/commands/time_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ pub(super) fn calculate_duration(
let duration = end - start;

if time_entry.lunch_break {
Ok(duration - launch_break())
calculate_duration_with_lunch_break(duration)
} else {
Ok(duration)
}
Expand All @@ -208,6 +208,18 @@ pub(super) fn calculate_duration(
}
}

fn calculate_duration_with_lunch_break(
duration: Duration,
) -> anyhow::Result<Duration> {
let duration_with_lunch_break = duration - launch_break();

if duration_with_lunch_break <= Duration::zero() {
Err(anyhow!("Duration minus lunch break is <= 0"))
} else {
Ok(duration_with_lunch_break)
}
}

pub fn start(
format: &Format,
time_entry: &StartTimeEntry,
Expand Down
30 changes: 23 additions & 7 deletions src/commands/time_entries_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn test_calculate_duration() -> anyhow::Result<()> {
project: "fkbr".to_string(),
start: DateTime::<Local>::from_str("2021-11-21T22:58:09Z")?,
end: Some(DateTime::<Local>::from_str("2021-11-21T22:58:09Z")?),
duration: Some(Duration::hours(2)),
duration: None,
non_billable: false,
lunch_break: false,
description: None,
Expand All @@ -138,22 +138,38 @@ fn test_calculate_duration() -> anyhow::Result<()> {
project: "fkbr".to_string(),
start: DateTime::<Local>::from_str("2021-11-21T23:58:09Z")?,
end: Some(DateTime::<Local>::from_str("2021-11-21T22:58:09Z")?),
duration: Some(Duration::hours(2)),
duration: None,
non_billable: false,
lunch_break: false,
description: None,
tags: None,
};

assert_eq!(
calculate_duration(
&time_entry_with_start_is_after_end
)
.unwrap_err()
.to_string(),
calculate_duration(&time_entry_with_start_is_after_end)
.unwrap_err()
.to_string(),
"start='2021-11-22 00:58:09 +01:00' is greater or equal than end='2021-11-21 23:58:09 +01:00'".to_string()
);

let time_entry_where_lunch_break_is_longer_than_duration = CreateTimeEntry {
project: "fkbr".to_string(),
start: DateTime::<Local>::from_str("2021-11-21T10:58:09Z")?,
end: Some(DateTime::<Local>::from_str("2021-11-21T11:58:09Z")?),
duration: None,
non_billable: false,
lunch_break: true,
description: None,
tags: None,
};

assert_eq!(
calculate_duration(&time_entry_where_lunch_break_is_longer_than_duration)
.unwrap_err()
.to_string(),
"Duration minus lunch break is <= 0".to_string()
);

Ok(())
}

Expand Down

0 comments on commit bb8dc10

Please sign in to comment.