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 14, 2022
1 parent cfa2e1e commit dfeaf4e
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ 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 when either the start date or end date were outside of business hours
- Added line breaks in between each line for all comment based help

## [0.1.2] - 2022-04-13
### Fixed
Expand Down
9 changes: 9 additions & 0 deletions docs/Get-ElapsedBusinessTime.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ Get-ElapsedBusinessTime [-StartDate] <DateTime> [-EndDate] <DateTime> [[-StartHo

## DESCRIPTION
Get the elapsed time between two dates, where the time measured is only inbetween "business hours".

This is helpful to measure the amount of time past from a start datetime, to an end datetime, while only considering "business hours".

What constitutes "business hours" in terms of day of the week, or calendar date, including working hours, is arbitrary and completely customisable.

In other words, the default parameters dictate normal working days, which are Monday through Friday and 08:00 through 17:00.

You can also specify particular dates, or days of the week, to be regarded as non-working dates via the -NonWorkingDates and -NonWorkingDaysOfWeek parameters.

This function does consider both date and time while calculating the elapsed time.

## EXAMPLES
Expand Down Expand Up @@ -116,6 +121,7 @@ Accept wildcard characters: False
### -StartHour
The starting hour of a typical working day.
The default starting hour is 08:00 (AM).
Note: this parameter is a datetime object is, however only the time is used for calculation.
The date is ignored.
Expand All @@ -134,6 +140,7 @@ Accept wildcard characters: False
### -FinishHour
The final hour of a typical working day.
The default final hour is 17:00.
Note: this parameter is a datetime object is, however only the time is used for calculation.
The date is ignored.
Expand All @@ -152,6 +159,7 @@ Accept wildcard characters: False
### -NonWorkingDaysOfWeek
The days of the week, representated as strings e.g.
'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday', which denotes non-working days of the week.
Days specified in this parameter will not be considered as working days.
```yaml
Expand All @@ -168,6 +176,7 @@ Accept wildcard characters: False
### -NonWorkingDates
An array of datetime objects which denote specific non-working dates.
Dates specified in this parameter will not be considered as working days.
```yaml
Expand Down
7 changes: 7 additions & 0 deletions docs/Get-WorkingDates.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ Get-WorkingDates [-StartDate] <DateTime> [-EndDate] <DateTime> [[-NonWorkingDays

## DESCRIPTION
Return all the working dates between two given datetimes.

This is helpful to identify the specific dates between two dates which are considered to be "working day(s)".

What constitutes a "working day" in terms of day of the week, or calendar date, including working hours, is arbitrary and completely customisable.

In other words, the default parameters dictate normal working days, which are Monday through Friday.

You can also specify particular dates, or days of the week, to be regarded as non-working dates via the -NonWorkingDates and -NonWorkingDaysOfWeek parameters.

This function does not consider the time, only the date, when determining whether it is a working date or not.

## EXAMPLES
Expand Down Expand Up @@ -107,6 +112,7 @@ Accept wildcard characters: False
### -NonWorkingDaysOfWeek
The days of the week, representated as strings e.g.
'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday', which denotes non-working days of the week.
Days specified in this parameter will not be considered as working days.
```yaml
Expand All @@ -123,6 +129,7 @@ Accept wildcard characters: False
### -NonWorkingDates
An array of datetime objects which denote specific non-working dates.
Dates specified in this parameter will not be considered as working days.
```yaml
Expand Down
8 changes: 8 additions & 0 deletions docs/Test-WorkingDay.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ Test-WorkingDay [-Date] <DateTime> [[-StartHour] <DateTime>] [[-FinishHour] <Dat

## DESCRIPTION
Determine whether a given datetime is a working day.

What constitutes a "working day" in terms of day of the week, or calendar date, including working hours, is arbitrary and completely customisable.

In other words, the default parameters dictate normal working days, which are Monday through Friday, and normal working hours are 08:00 through 17:00.

You can also specify particular dates, or days of the week, to be regarded as non-working dates via the -NonWorkingDates and -NonWorkingDaysOfWeek parameters.

If the datetime of -Date falls outside of these parameters, you'll receive a boolean result.

## EXAMPLES
Expand Down Expand Up @@ -88,6 +92,7 @@ Accept wildcard characters: False
### -StartHour
The starting hour of a typical working day.
The default starting hour is 08:00 (AM).
Note: this parameter is a datetime object is, however only the time is used for calculation.
The date is ignored.
Expand All @@ -106,6 +111,7 @@ Accept wildcard characters: False
### -FinishHour
The final hour of a typical working day.
The default final hour is 17:00.
Note: this parameter is a datetime object is, however only the time is used for calculation.
The date is ignored.
Expand All @@ -124,6 +130,7 @@ Accept wildcard characters: False
### -NonWorkingDaysOfWeek
The days of the week, representated as strings e.g.
'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday', which denotes non-working days of the week.
Days specified in this parameter will not be considered as working days.
```yaml
Expand All @@ -140,6 +147,7 @@ Accept wildcard characters: False
### -NonWorkingDates
An array of datetime objects which denote specific non-working dates.
Dates specified in this parameter will not be considered as working days.
```yaml
Expand Down
37 changes: 26 additions & 11 deletions src/Public/Get-ElapsedBusinessTime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,35 @@ function Get-ElapsedBusinessTime {
Get the elapsed time between two dates, where the time measured is only inbetween "business hours".
.DESCRIPTION
Get the elapsed time between two dates, where the time measured is only inbetween "business hours".
This is helpful to measure the amount of time past from a start datetime, to an end datetime, while only considering "business hours".
What constitutes "business hours" in terms of day of the week, or calendar date, including working hours, is arbitrary and completely customisable.
In other words, the default parameters dictate normal working days, which are Monday through Friday and 08:00 through 17:00.
You can also specify particular dates, or days of the week, to be regarded as non-working dates via the -NonWorkingDates and -NonWorkingDaysOfWeek parameters.
This function does consider both date and time while calculating the elapsed time.
.PARAMETER StartDate
The datetime object to start calculating the elapsed time from. It must be an older datetime than -EndDate.
.PARAMETER EndDate
The datetime object to end calculating the elapsed time to. It must be a newer datetime than -StartDate.
.PARAMETER StartHour
The starting hour of a typical working day. The default starting hour is 08:00 (AM).
Note: this parameter is a datetime object is, however only the time is used for calculation. The date is ignored.
.PARAMETER FinishHour
The final hour of a typical working day. The default final hour is 17:00.
Note: this parameter is a datetime object is, however only the time is used for calculation. The date is ignored.
.PARAMETER NonWorkingDaysOfWeek
The days of the week, representated as strings e.g. 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday', which denotes non-working days of the week.
Days specified in this parameter will not be considered as working days.
.PARAMETER NonWorkingDates
An array of datetime objects which denote specific non-working dates.
Dates specified in this parameter will not be considered as working days.
.EXAMPLE
Get-ElapsedBusinessTime -StartDate (Get-Date '2022-04-11 10:00:00') -EndDate (Get-Date '2022-04-11 10:37:00')
Expand Down Expand Up @@ -123,14 +132,14 @@ function Get-ElapsedBusinessTime {
$NumberOfWorkingDays = $WorkingDays.Count
$ElapsedTime = New-TimeSpan

if (Test-WorkingDay -Date $StartDate -StartHour $StartHour -FinishHour $FinishHour @CommonParams) {
$FirstDayEndDate = Get-Date ('{0}/{1}/{2} {3}:{4}:{5}' -f $StartDate.Year,
$StartDate.Month,
$StartDate.Day,
$FinishHour.Hour,
$FinishHour.Minute,
$FinishHour.Second)

$FirstDayEndDate = Get-Date ('{0}/{1}/{2} {3}:{4}:{5}' -f $StartDate.Year,
$StartDate.Month,
$StartDate.Day,
$FinishHour.Hour,
$FinishHour.Minute,
$FinishHour.Second)

if (Test-WorkingDay -Date $StartDate -StartHour $StartHour -FinishHour $FinishHour @CommonParams) {
$Params = @{
StartDate = $StartDate
EndDate = $FirstDayEndDate
Expand All @@ -140,15 +149,18 @@ function Get-ElapsedBusinessTime {
$ElapsedTime += (GetElapsedTime @Params)
$NumberOfWorkingDays--
}
elseif ($StartDate -gt $FirstDayEndDate) {
$NumberOfWorkingDays--
}

if (Test-WorkingDay -Date $EndDate -StartHour $StartHour -FinishHour $FinishHour @CommonParams) {
$LastDayStartDate = Get-Date ('{0}/{1}/{2} {3}:{4}:{5}' -f $EndDate.Year,
$LastDayStartDate = Get-Date ('{0}/{1}/{2} {3}:{4}:{5}' -f $EndDate.Year,
$EndDate.Month,
$EndDate.Day,
$StartHour.Hour,
$StartHour.Minute,
$StartHour.Second)


if (Test-WorkingDay -Date $EndDate -StartHour $StartHour -FinishHour $FinishHour @CommonParams) {
$Params = @{
StartDate = $LastDayStartDate
EndDate = $EndDate
Expand All @@ -158,6 +170,9 @@ function Get-ElapsedBusinessTime {
$ElapsedTime += (GetElapsedTime @Params)
$NumberOfWorkingDays--
}
elseif ($EndDate -lt $LastDayStartDate) {
$NumberOfWorkingDays--
}

$InBetweenHours = $NumberOfWorkingDays * $WorkingHours.Hours
(New-TimeSpan -Hours $InBetweenHours) + $ElapsedTime
Expand Down
7 changes: 7 additions & 0 deletions src/Public/Get-WorkingDates.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ function Get-WorkingDates {
Return all the working dates between two given datetimes.
.DESCRIPTION
Return all the working dates between two given datetimes.
This is helpful to identify the specific dates between two dates which are considered to be "working day(s)".
What constitutes a "working day" in terms of day of the week, or calendar date, including working hours, is arbitrary and completely customisable.
In other words, the default parameters dictate normal working days, which are Monday through Friday.
You can also specify particular dates, or days of the week, to be regarded as non-working dates via the -NonWorkingDates and -NonWorkingDaysOfWeek parameters.
This function does not consider the time, only the date, when determining whether it is a working date or not.
.PARAMETER StartDate
The datetime object to identify all the working dates from. It must be an older datetime than -EndDate.
.PARAMETER EndDate
The datetime object to identify all the working dates to. It must be a newer datetime than -StartDate.
.PARAMETER NonWorkingDaysOfWeek
The days of the week, representated as strings e.g. 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday', which denotes non-working days of the week.
Days specified in this parameter will not be considered as working days.
.PARAMETER NonWorkingDates
An array of datetime objects which denote specific non-working dates.
Dates specified in this parameter will not be considered as working days.
.EXAMPLE
Get-WorkingDates -StartDate (Get-Date '2022-04-11') -EndDate (Get-Date '2022-04-11')
Expand Down
8 changes: 8 additions & 0 deletions src/Public/Test-WorkingDay.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@ function Test-WorkingDay {
Determine whether a given datetime is a working day.
.DESCRIPTION
Determine whether a given datetime is a working day.
What constitutes a "working day" in terms of day of the week, or calendar date, including working hours, is arbitrary and completely customisable.
In other words, the default parameters dictate normal working days, which are Monday through Friday, and normal working hours are 08:00 through 17:00.
You can also specify particular dates, or days of the week, to be regarded as non-working dates via the -NonWorkingDates and -NonWorkingDaysOfWeek parameters.
If the datetime of -Date falls outside of these parameters, you'll receive a boolean result.
.PARAMETER Date
The datetime to determine whether it is a working day or not.
.PARAMETER StartHour
The starting hour of a typical working day. The default starting hour is 08:00 (AM).
Note: this parameter is a datetime object is, however only the time is used for calculation. The date is ignored.
.PARAMETER FinishHour
The final hour of a typical working day. The default final hour is 17:00.
Note: this parameter is a datetime object is, however only the time is used for calculation. The date is ignored.
.PARAMETER NonWorkingDaysOfWeek
The days of the week, representated as strings e.g. 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday', which denotes non-working days of the week.
Days specified in this parameter will not be considered as working days.
.PARAMETER NonWorkingDates
An array of datetime objects which denote specific non-working dates.
Dates specified in this parameter will not be considered as working days.
.EXAMPLE
Test-WorkingDay -Date (Get-Date '2022-04-11 09:00:00')
Expand Down
30 changes: 30 additions & 0 deletions tests/Public/Get-ElapsedBusinessTime.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,30 @@ Describe "Get-ElapsedBusinessTime" {
(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'
(Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate).Hours | Should -Be 1
}

It "should be 1 hour, across 2 consecutive days, where both are working days" {
$StartDate = Get-Date '2022-04-07 22:00:00'
$EndDate = Get-Date '2022-04-08 09:00:00'
(Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate).Hours | Should -Be 1
}

It "should be 9 hours, across 2 consecutive days, where both are working days" {
$StartDate = Get-Date '2022-04-07 08:00:00'
$EndDate = Get-Date '2022-04-08 08:00:00'
(Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate).Hours | Should -Be 9
}

It "should be 9 hours, across 3 consecutive days, where all are working days" {
$StartDate = Get-Date '2022-04-06 22:00:00'
$EndDate = Get-Date '2022-04-08 02: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'
Expand Down Expand Up @@ -139,5 +157,17 @@ Describe "Get-ElapsedBusinessTime" {
$NonWorkingDaysOfWeek = 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'
(Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate -NonWorkingDaysOfWeek $NonWorkingDaysOfWeek).TotalHours | Should -Be 0
}

It "should be 0 hours, across 2 consecutive days, where 2 are working days" {
$StartDate = Get-Date '2022-04-07 18:00:00'
$EndDate = Get-Date '2022-04-08 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'
(Get-ElapsedBusinessTime -StartDate $StartDate -EndDate $EndDate).Hours | Should -Be 0
}
}
}
2 changes: 1 addition & 1 deletion tests/invoke.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ $PesterConfig = New-PesterConfiguration -Hashtable @{
Run = @{
Path = '{0}/Public' -f $PSScriptRoot
Throw = $true
SkipRemainingOnFailure = 'Block'
#SkipRemainingOnFailure = 'Block'
}
Output = @{
Verbosity = 'Detailed'
Expand Down

0 comments on commit dfeaf4e

Please sign in to comment.