Skip to content

Commit

Permalink
Fixed Get-WorkingDate
Browse files Browse the repository at this point in the history
  • Loading branch information
codaamok committed Apr 12, 2022
1 parent 73136df commit 57f4d62
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 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
- Some typo and grammar issues in comment based help
- `Get-WorkingDates` would not return the correct number of working dates, which also impacted `Get-ElapsedBusinessTime`

## [0.1.0] - 2022-04-11
### Added
Expand Down
16 changes: 8 additions & 8 deletions src/Public/Get-ElapsedBusinessTime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ function Get-ElapsedBusinessTime {
.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 a "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 are Monday through Friday and 08:00 through 17:00.
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
Expand Down Expand Up @@ -94,8 +94,8 @@ function Get-ElapsedBusinessTime {
$Params = @{
StartDate = $StartDate
EndDate = $EndDate
StartHour = $StartHour
FinishHour = $FinishHour
StartHour = $StartHour
FinishHour = $FinishHour
}

GetElapsedTime @Params
Expand All @@ -115,8 +115,8 @@ function Get-ElapsedBusinessTime {
$Params = @{
StartDate = $StartDate
EndDate = $FirstDayEndDate
StartHour = $StartHour
FinishHour = $FinishHour
StartHour = $StartHour
FinishHour = $FinishHour
}
$ElapsedTime += (GetElapsedTime @Params)
$NumberOfWorkingDays--
Expand All @@ -133,8 +133,8 @@ function Get-ElapsedBusinessTime {
$Params = @{
StartDate = $LastDayStartDate
EndDate = $EndDate
StartHour = $StartHour
FinishHour = $FinishHour
StartHour = $StartHour
FinishHour = $FinishHour
}
$ElapsedTime += (GetElapsedTime @Params)
$NumberOfWorkingDays--
Expand Down
17 changes: 8 additions & 9 deletions src/Public/Get-WorkingDates.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function Get-WorkingDates {
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 are Monday through Friday.
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
Expand Down Expand Up @@ -62,14 +62,13 @@ function Get-WorkingDates {
[Parameter()]
[DateTime[]]$NonWorkingDates
)

if ($StartDate.TimeOfDay -eq $EndDate.TimeOfDay) {
# This can return 1 less than intended if we do not do this change.
# For example, if the dates in between are 3 working days,
# but the time span between them are 2 whole days,
# this returns 2 instead of 3
$EndDate = $EndDate.AddSeconds(1)
}

# This can return 1 less than intended if we do not do this change.
# For example, if the dates in between are 3 working days,
# but the time span between them are 2 whole days,
# this returns 2 instead of 3
$StartDate = $StartDate.Date
$EndDate = $EndDate.Date.AddSeconds(1)

$TimeSpan = New-TimeSpan -Start $StartDate -End $EndDate
$Days = [Math]::Ceiling($TimeSpan.TotalDays)
Expand Down
2 changes: 1 addition & 1 deletion src/Public/Test-WorkingDay.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Test-WorkingDay {
.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 are Monday through Friday, and normal working hours are 08:00 through 17:00.
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
Expand Down
19 changes: 17 additions & 2 deletions tests/Public/Get-WorkingDates.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,28 @@ Describe "Get-WorkingDates" {
It "should be 2 working days across 2 consecutive days" {
$StartDate = Get-Date '2022-04-07'
$EndDate = Get-Date '2022-04-08'
Get-WorkingDates -StartDate $StartDate -EndDate $EndDate | Should -Be (0..1 | ForEach-Object { $StartDate.AddDays($_).Date })
Get-WorkingDates -StartDate $StartDate -EndDate $EndDate | Should -Be @(
Get-Date '2022-04-07'
Get-Date '2022-04-08'
)
}

It "should be 2 working days across 3 consecutive days, with traditional weekends" {
$StartDate = Get-Date '2022-04-07'
$EndDate = Get-Date '2022-04-09'
Get-WorkingDates -StartDate $StartDate -EndDate $EndDate | Should -Be (0..1 | ForEach-Object { $StartDate.AddDays($_).Date })
Get-WorkingDates -StartDate $StartDate -EndDate $EndDate | Should -Be @(
Get-Date '2022-04-07'
Get-Date '2022-04-08'
)
}

It "should be 2 working days across 4 consecutive days, with traditional weekends" {
$StartDate = Get-Date '2022-04-08 12:40:12'
$EndDate = Get-Date '2022-04-11 07:37:52'
Get-WorkingDates -StartDate $StartDate -EndDate $EndDate | Should -Be @(
Get-Date '2022-04-08'
Get-Date '2022-04-11'
)
}

It "should be 10 working days across 14 consecutive days, with traditional weekends" {
Expand Down

0 comments on commit 57f4d62

Please sign in to comment.