-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendar.ps1
82 lines (70 loc) · 2.03 KB
/
Calendar.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
function Get-LaborSalesCalendar {
param (
$ProductionYearStartDate = "1/1/2023"
)
$YearStartDate = Get-Date $ProductionYearStartDate
$WeekSpan = [TimeSpan]::FromDays(7)
$ShortMonthSpan = [TimeSpan]::FromDays(28)
$LongMonthSpan = [TimeSpan]::FromDays(35)
$WeekStartDates = @()
$MonthStartDates = @()
$QuarterStartDates = @()
# Define Weeks
$DateCursor = $YearStartDate
while ($DateCursor.Year -lt ([int]$YearStartDate.Year + 1)) {
$WeekStartDates += $DateCursor
$DateCursor = $DateCursor + $WeekSpan
}
# Define Months
$DateCursor = $YearStartDate
while ($DateCursor.Year -lt ([int]$YearStartDate.Year + 1)) {
$MonthStartDates += $DateCursor
$IsThirdMonth = ($MonthStartDates.Length % 3) -eq 0
if ($IsThirdMonth) {
$DateCursor = $DateCursor + $LongMonthSpan
} else {
$DateCursor = $DateCursor + $ShortMonthSpan
}
}
# Define Quarters
for ($Index = 0; $Index -lt $MonthStartDates.Count; $Index++) {
$IsQuarterStart = ($Index % 3) -eq 0
if ($IsQuarterStart) {
$QuarterStartDates += $MonthStartDates[$Index]
}
}
# Find next year start date and add to all timespans
$NextYearStartDate = $YearStartDate.AddYears(1)
while ($NextYearStartDate.DayOfWeek -ne "Sunday") {
$NextYearStartDate = $NextYearStartDate.AddDays(-1)
}
$WeekStartDates += $NextYearStartDate
$MonthStartDates += $NextYearStartDate
$QuarterStartDates += $NextYearStartDate
return [PSCustomObject]@{
WeekStartDates = $WeekStartDates
MonthStartDates = $MonthStartDates
QuarterStartDates = $QuarterStartDates
}
}
function Get-IndexOfDateFromList {
param (
[Parameter(Mandatory)][datetime]$Date,
[Parameter(Mandatory)]$DateList
)
$DateIndex = -1
for ($i = 0; $i -lt $DateList.Count; $i++) {
try {
$IsDateInTimespan =
$Date -ge $DateList[$i] -and
$Date -lt $DateList[$i + 1]
if ($IsDateInTimespan) {
$DateIndex = $i
break
}
} catch {
$DateIndex = -1
}
}
return $DateIndex
}