From d70b681a86c0f182ccad207ac656cc547878704a Mon Sep 17 00:00:00 2001 From: codaamok Date: Wed, 13 Apr 2022 22:27:18 +0100 Subject: [PATCH] Updated Get-ElapsedBusinessTime --- .vscode/tasks.json | 2 +- CHANGELOG.md | 2 ++ src/Public/Get-ElapsedBusinessTime.ps1 | 19 +++++++++++++++++++ .../Public/Get-ElapsedBusinessTime.Tests.ps1 | 6 ++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 721746d..b90945e 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -41,7 +41,7 @@ { "label": "Tests", "type": "process", - "command": "powershell", + "command": "pwsh", "args": ["-noprofile", "-file", "./tests/invoke.tests.ps1"], "group": { "kind": "test", diff --git a/CHANGELOG.md b/CHANGELOG.md index 9349174..9ec3e62 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 where the number of working days between `-StartDate` and `-EndDate` was 1 but the date range spanned more than 1 calendar day, e.g. Sunday through to Monday. ## [0.1.1] - 2022-04-12 ### Fixed diff --git a/src/Public/Get-ElapsedBusinessTime.ps1 b/src/Public/Get-ElapsedBusinessTime.ps1 index 12c9428..141c28d 100644 --- a/src/Public/Get-ElapsedBusinessTime.ps1 +++ b/src/Public/Get-ElapsedBusinessTime.ps1 @@ -91,6 +91,25 @@ 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, + $WorkingDays.Month, + $WorkingDays.Day, + $StartHour.Hour, + $StartHour.Minute, + $StartHour.Second) + } + + if (-not (Test-WorkingDay -Date $EndDate -StartHour $StartHour -FinishHour $FinishHour @CommonParams)) { + $EndDate = Get-Date ('{0}/{1}/{2} {3}:{4}:{5}' -f $WorkingDays.Year, + $WorkingDays.Month, + $WorkingDays.Day, + $FinishHour.Hour, + $FinishHour.Minute, + $FinishHour.Second) + + } + $Params = @{ StartDate = $StartDate EndDate = $EndDate diff --git a/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 b/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 index 0127c88..286217d 100644 --- a/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 +++ b/tests/Public/Get-ElapsedBusinessTime.Tests.ps1 @@ -35,6 +35,12 @@ Describe "Get-ElapsedBusinessTime" { $EndDate = Get-Date '2022-04-08 08:00:00' (Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate).Hours | Should -Be 9 } + + It "should be 143 minutes, across 2 consecutive days, where 1 is a working days" { + $StartDate = Get-Date '2022-04-10 13:52:12' + $EndDate = Get-Date '2022-04-11 10:23:12' + (Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate).TotalMinutes | Should -Be 143.2 + } It "should be 10 hours, across 1 full day and 1 partial day, where both are working days" { $StartDate = Get-Date '2022-04-07 08:00:00'