Skip to content

Instantly share code, notes, and snippets.

@SMoni
Created April 1, 2022 07:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SMoni/f0a2f72334c6fe63a9ae8f9f5763c7bc to your computer and use it in GitHub Desktop.
Save SMoni/f0a2f72334c6fe63a9ae8f9f5763c7bc to your computer and use it in GitHub Desktop.
$sample = @(
[PSCustomObject]@{
From = [datetime]::Parse("01.01.2022 00:00:00")
To = [datetime]::Parse("10.01.2022 23:59:59")
}
[PSCustomObject]@{
From = [datetime]::Parse("05.01.2022 00:00:00")
To = [datetime]::Parse("15.01.2022 23:59:59")
}
[PSCustomObject]@{
From = [datetime]::Parse("08.01.2022 00:00:00")
To = [datetime]::Parse("09.01.2022 23:59:59")
}
[PSCustomObject]@{
From = [datetime]::Parse("20.01.2022 00:00:00")
To = [datetime]::Parse("25.01.2022 23:59:59")
}
[PSCustomObject]@{
From = [datetime]::Parse("24.01.2022 00:00:00")
To = [datetime]::Parse("31.01.2022 23:59:59")
}
)
$main = {
Clear-Host
getOverlapsFrom($sample)
}
function getOverlapsFrom($List) {
process {
$sorted = $List | Sort-Object -Property From
$head = $sorted | Select-Object -First 1
$tail = $sorted | Select-Object -Skip 1
$tail | getOverlapsStartingWith $head
}
}
function getOverlapsStartingWith($init) {
begin {
$last = $init
$result = @()
}
process {
if(-not ($_ | isOverlapping $last)) {
$last = $_
return
}
$result += [PSCustomObject]@{
From = if($last.From -lt $_.From ) { $_.From } else { $last.From }
To = if($_.To -lt $last.To) { $_.To } else { $last.To }
}
if($_.To -lt $last.To) {
$last = $_
}
}
end {
$result
}
}
function isOverlapping($that) {
process {
return $that.From -lt $_.To -and $_.From -le $that.To
}
}
&$main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment