Skip to content

Commit

Permalink
Updated Get-ElapsedBusinessTime
Browse files Browse the repository at this point in the history
  • Loading branch information
codaamok committed Apr 15, 2022
1 parent a9867d2 commit 8d3d89a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 23 additions & 16 deletions src/Public/Get-ElapsedBusinessTime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions tests/Public/Get-ElapsedBusinessTime.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 8d3d89a

Please sign in to comment.