#region overlap type function
function OverlapType {
param ([datetime]$Time1Start, [datetime]$Time1End, [datetime]$Time2Start, [datetime]$Time2End)
if ($Time1Start -ge $Time2Start -AND $Time1End -le $Time2End) {
$OverlapType = "1 In Between 2"
}
elseif ($Time2Start -ge $Time1Start -AND $Time2End -le $Time1End) {
$OverlapType = "2 In Between 1"
}
elseif ((($Time2Start) -lt ($Time1End)) -and (($Time2Start) -gt ($Time1Start)) -and ($Time2End ) -gt ($Time1End) ) {
$OverlapType = "Start2 Over End1"
}
elseif (($Time2Start) -lt ($Time1Start) -AND (($Time2End ) -lt ($Time1End) -AND ($Time2Start) -gt ($Time1Start))) {
$OverlapType = "End2 Over Start1"
}
elseif ((($Time1Start) -lt ($Time2End ) -and ($Time2Start) -lt ($Time1Start)) -AND (($Time1End) -gt $Time2End) ) {
$OverlapType = "Start1 Over End2"
}
elseif (($Time1Start) -lt ($Time2Start) -AND (($Time1End) -lt ($Time2End ) -AND ($Time1Start) -gt ($Time2Start)) ) {
$OverlapType = "End1 Over Start2"
}
else {
$OverlapType = "Unknown From Function"
}
return $OverlapType
}
#endregion
$EntriesArray = @()
$EntriesCount = $null
#region process and output
foreach ($Entry in $Time1) {
foreach ($SecondEntry in $Time2 ) {
if ($SecondEntry.Time_Start -lt $Entry.Time_End -and ($null -eq $SecondEntry.Time_End -OR $SecondEntry.Time_End -gt $Entry.Time_Start) -and $Entry.Time_Recid -ne $SecondEntry.Time_Recid -and $Entry.member_id -eq $SecondEntry.member_id) {
#Set time entry start and end times and send to function to work out the overlap type
($Time1Start) = $Entry.Time_Start
($Time1End) = $Entry.Time_End
($Time2Start) = $SecondEntry.Time_Start
($Time2End ) = $SecondEntry.Time_End
$OverlapConfirm = OverlapType -Time1Start $Time1Start -Time1End $Time1End -Time2Start $Time2Start -Time2End $Time2End
#Find correct duration time based on overlap type
switch ($OverlapConfirm) {
"Start2 Over End1" { $Duration = New-TimeSpan -Start $SecondEntry.Time_Start -End $Entry.Time_End }
"2 In Between 1" { $Duration = New-TimeSpan -Start $SecondEntry.Time_Start -End $SecondEntry.Time_End }
"End2 Over Start1" { $Duration = New-TimeSpan -Start $SecondEntry.Time_End -End $Entry.Time_Start }
"Start1 Over End2" { $Duration = New-TimeSpan -Start $Entry.Time_Start -End $SecondEntry.Time_End }
"1 In Between 2" { $Duration = New-TimeSpan -Start $Entry.Time_Start -End $Entry.Time_End }
"End1 Over Start2" { $Duration = New-TimeSpan -Start $Entry.Time_End -End $SecondEntry.Time_Start }
"Unknown From Function" { "Function Didnt Pass A Correct Response" }
Default { "Unknown From Switch" }
}
#Entry Found Output
Write-Host "Overlap Type: " $OverlapConfirm -ForegroundColor Red
Write-Host ([datetime]$Time1Start) " - " ([datetime]$Time1End) " - " $Entry.Time_RecID
Write-Host ([datetime]$Time2Start) " - " ([datetime]$Time2End ) " - " $SecondEntry.Time_RecID
Write-Host "Duration " $Duration.TotalMinutes
$EntriesArray += $Entry.Time_RecID
$EntriesCount++
}
}
}
$EntriesArray
$EntriesCount
#endregion