-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompare-Events.ps1
178 lines (151 loc) · 5.61 KB
/
Compare-Events.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
function Compare-Events {
<#
.SYNOPSIS
Compares two events to determine if they overlap.
.DESCRIPTION
Compares two events to determine if they overlap.
.EXAMPLE
ipmo PsDateTools -force
$props = @{}
$props.ReferenceStart = '1/1/2000 1:00 AM'
$props.ReferenceEnd = '1/1/2000 5:00 AM'
$props.ReferenceLocation = '123 Main St, Anytown USA'
$props.DifferenceStart = '1/1/2000 7:00 AM'
$props.DifferenceEnd = '1/1/2000 11:00 AM'
$props.DifferenceLocation = '123 Main St, Anytown USA'
$props.GracePeriod = New-TimeSpan -Hours 2
$diffEvents = Compare-Events @props
$diffEvents
hasConflict : False
GracePeriod : 02:00:00
Overlap : -02:00:00
Downtime : 02:00:00
ExactSame : False
SameTime : False
SamePlace : True
ReferenceStart : 1/1/2000 1:00 AM
ReferenceEnd : 1/1/2000 5:00 AM
ReferenceLocation : '123 Main St, Anytown USA
DifferenceStart : 1/1/2000 7:00 AM
DifferenceEnd : 1/1/2000 11:00 AM
DifferenceLocation : '123 Main St, Anytown USA
.EXAMPLE
ipmo PsDateTools -force
$props = @{}
$props.ReferenceStart = '1/1/2000 1:00 AM'
$props.ReferenceEnd = '1/1/2000 5:00 AM'
$props.DifferenceStart = '1/1/2000 7:00 AM'
$props.DifferenceEnd = '1/1/2000 11:00 AM'
$props.GracePeriod = New-TimeSpan -Hours 2
$diffEvents = Compare-Events @props
$diffEvents
hasConflict : False
GracePeriod : 02:00:00
Overlap : -02:00:00
Downtime : 02:00:00
ExactSame : False
SameTime : False
ReferenceStart : 1/1/2000 1:00 AM
ReferenceEnd : 1/1/2000 5:00 AM
DifferenceStart : 1/1/2000 7:00 AM
DifferenceEnd : 1/1/2000 11:00 AM
.NOTES
Overlap is defined as the amount of time it takes for the earlier event to finish after the later event starts.
Downtime is defined as the amount of time after the earlier event finishes, before the later event starts.
Grace Period is defined as the amount of Overlap to allow before calling a Conflict true.
A negative Grace Period will enforce a minimum amount of Downtime.
#>
[CmdletBinding()]
param(
# Baseline Event time and date
[Parameter(Mandatory)]
[datetime]
$ReferenceStart,
# Baseline Event END time and date (Start plus duration)
[Parameter(Mandatory)]
[datetime]
$ReferenceEnd,
# Baseline Event Location (optional)
[Parameter()]
[string]
$ReferenceLocation,
# Comparison Event time and date
[Parameter(Mandatory)]
[datetime]
$DifferenceStart,
# Comparison Event END time and date (Start plus duration)
[Parameter(Mandatory)]
[datetime]
$DifferenceEnd,
# Baseline Event Location (optional)
[Parameter()]
[string]
$DifferenceLocation,
# Amount of overlap to allow (downtime required if negative)
[Parameter(HelpMessage='Amount of overlap to allow (downtime required if negative)')]
[ValidateNotNull()]
[timespan]
$GracePeriod=([timespan]0)
)#END: param()
$hasLocation = -not ([string]::IsNullOrWhiteSpace($ReferenceLocation) -and [string]::IsNullOrWhiteSpace($DifferenceLocation))
$SameTime = $false
$SamePlace = $false
# To calculate the overlap/downtime, first determine which event starts earlier
# This method REVEALS which variable has the early date
$referenceIsEarlyEvent = if ($ReferenceStart -eq $DifferenceStart) {
# Edge case--Use the end date instead
if ($ReferenceEnd -eq $DifferenceEnd) {
# Edge case--throw a conflict
$SameTime = $true
$false
} elseif ($ReferenceEnd -lt $DifferenceEnd) {
$true
} else {
$false
}
} elseif ($ReferenceStart -lt $DifferenceStart) {
$true
} else {
$false
}
# This method ABSTRACTS which variable has the early date
$sortedStarts = @($ReferenceStart,$DifferenceStart) | Sort-Object
$lateStart = $sortedStarts[1]
$earlyEnd = if ($referenceIsEarlyEvent) {$ReferenceEnd} else {$DifferenceEnd}
# Downtime time is later start minus early end
# |---EARLY---| <downtime> |---LATER---|
$Downtime = $lateStart - $earlyEnd
# Overlap is negative Downtime
$Overlap = -$Downtime
if ($hasLocation) {
# Also test if the location string matches
if ($ReferenceLocation.Trim() -eq $DifferenceLocation.Trim()) {
$SamePlace = $true
}
}
$hasConflict = if ($hasLocation) {
($Overlap - $GracePeriod) -gt 0 -and -not $SamePlace
} else {
($Overlap - $GracePeriod) -gt 0
}
$ExactSame = if ($hasLocation) {
$SameTime -and $SamePlace
} else {
$SameTime
}
$output = [ordered]@{}
$output.hasConflict = $hasConflict
$output.GracePeriod = $GracePeriod
$output.Overlap = $Overlap
$output.Downtime = $Downtime
$output.ExactSame = $ExactSame
$output.SameTime = $SameTime
if ($hasLocation) {$output.SamePlace = $SamePlace}
$output.ReferenceStart = $ReferenceStart
$output.ReferenceEnd = $ReferenceEnd
if ($hasLocation) {$output.ReferenceLocation = $ReferenceLocation}
$output.DifferenceStart = $DifferenceStart
$output.DifferenceEnd = $DifferenceEnd
if ($hasLocation) {$output.DifferenceLocation = $DifferenceLocation}
[pscustomobject]$output
}#END: function Compare-Events {}