diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b13ffe..586928d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- `Get-ElapsedBusinessTime` did not return correct result if `-StartDate` and `-EndDate` were on the same day, but it was a working day, and its time for both dates were outside of working hours ## [0.1.5] - 2022-04-14 ### Fixed diff --git a/src/Public/Get-ElapsedBusinessTime.ps1 b/src/Public/Get-ElapsedBusinessTime.ps1 index 96856a8..370df11 100644 --- a/src/Public/Get-ElapsedBusinessTime.ps1 +++ b/src/Public/Get-ElapsedBusinessTime.ps1 @@ -93,7 +93,7 @@ function Get-ElapsedBusinessTime { NonWorkingDates = $NonWorkingDates } - $WorkingHours = New-TimeSpan -Start $StartHour -End $FinishHour + $WorkingHours = New-TimeSpan -Start $StartHour -End $FinishHour $WorkingDays = Get-WorkingDates -StartDate $StartDate -EndDate $EndDate @CommonParams if ($null -eq $WorkingDays) { @@ -107,6 +107,7 @@ function Get-ElapsedBusinessTime { $StartHour.Hour, $StartHour.Minute, $StartHour.Second) + $j++ } if (-not (Test-WorkingDay -Date $EndDate -StartHour $StartHour -FinishHour $FinishHour @CommonParams)) { @@ -116,17 +117,23 @@ function Get-ElapsedBusinessTime { $FinishHour.Hour, $FinishHour.Minute, $FinishHour.Second) - + $j++ } - $Params = @{ - StartDate = $StartDate - EndDate = $EndDate - StartHour = $StartHour - FinishHour = $FinishHour + if ($j -eq 2) { + # This is if both start and end datetimes are outside of working hours + New-TimeSpan + } + else { + $Params = @{ + StartDate = $StartDate + EndDate = $EndDate + StartHour = $StartHour + FinishHour = $FinishHour + } + + GetElapsedTime @Params } - - GetElapsedTime @Params } else { $NumberOfWorkingDays = $WorkingDays.Count diff --git a/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 b/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 index 7d066f1..db51ef7 100644 --- a/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 +++ b/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 @@ -164,6 +164,12 @@ Describe "Get-ElapsedBusinessTime" { (Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate).Hours | Should -Be 0 } + It "should be 0 hours, across 1 consecutive day, where 1 is a working day" { + $StartDate = Get-Date '2022-04-07 01:00:00' + $EndDate = Get-Date '2022-04-07 03:00:00' + (Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate).Hours | Should -Be 0 + } + It "should be 0 hours, across 4 consecutive days, where 2 are working days" { $StartDate = Get-Date '2022-04-08 18:00:00' $EndDate = Get-Date '2022-04-11 03:00:00'