diff --git a/CHANGELOG.md b/CHANGELOG.md index 554c5cd..57d1643 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.2.0] - 2022-04-14 ### Added diff --git a/src/Public/Get-ElapsedBusinessTime.ps1 b/src/Public/Get-ElapsedBusinessTime.ps1 index b7adcca..fa7d561 100644 --- a/src/Public/Get-ElapsedBusinessTime.ps1 +++ b/src/Public/Get-ElapsedBusinessTime.ps1 @@ -105,39 +105,46 @@ function Get-ElapsedBusinessTime { New-TimeSpan } elseif ($WorkingDays.Count -eq 1) { - if (-not (Test-WorkingDay -Date $StartDate -StartHour $StartHour -FinishHour $FinishHour @CommonParams)) { - $StartDate = Get-Date ('{0}/{1}/{2} {3}:{4}:{5}' -f $WorkingDays.Year, + $Params = @{ + StartHour = $StartHour + FinishHour = $FinishHour + } + + $_StartDate = Get-Date ('{0}/{1}/{2} {3}:{4}:{5}' -f $WorkingDays.Year, $WorkingDays.Month, $WorkingDays.Day, $StartHour.Hour, $StartHour.Minute, $StartHour.Second) - $j++ + + if ($StartDate -le $_StartDate) { + $Params["StartDate"] = $_StartDate + } + else { + $Params["StartDate"] = $StartDate } - if (-not (Test-WorkingDay -Date $EndDate -StartHour $StartHour -FinishHour $FinishHour @CommonParams)) { - $EndDate = Get-Date ('{0}/{1}/{2} {3}:{4}:{5}' -f $WorkingDays.Year, + $_EndDate = Get-Date ('{0}/{1}/{2} {3}:{4}:{5}' -f $WorkingDays.Year, $WorkingDays.Month, $WorkingDays.Day, $FinishHour.Hour, $FinishHour.Minute, $FinishHour.Second) - $j++ + + if ($EndDate -gt $_EndDate) { + $Params["EndDate"] = $_EndDate + } + else { + $Params["EndDate"] = $EndDate } - if ($j -eq 2) { - # This is if both start and end datetimes are outside of working hours + $Result = GetElapsedTime @Params + + if ($Result.Ticks -le 0) { New-TimeSpan } else { - $Params = @{ - StartDate = $StartDate - EndDate = $EndDate - StartHour = $StartHour - FinishHour = $FinishHour - } - - GetElapsedTime @Params + $Result } } else { diff --git a/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 b/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 index db51ef7..500d913 100644 --- a/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 +++ b/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 @@ -30,6 +30,12 @@ Describe "Get-ElapsedBusinessTime" { (Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate).Hours | Should -Be 9 } + It "should be 9 hours on the same day" { + $StartDate = Get-Date '2022-04-07 07:00:00' + $EndDate = Get-Date '2022-04-07 18:00:00' + (Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate).Hours | Should -Be 9 + } + It "should be 1 hour, across 2 consecutive days, where both are working days" { $StartDate = Get-Date '2022-04-07 16:00:00' $EndDate = Get-Date '2022-04-08 03:00:00' @@ -170,6 +176,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 18:00:00' + $EndDate = Get-Date '2022-04-07 19: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'