1
+ /**
2
+ * Get the start and end time boundaries for a given date based on the provided start and end time strings.
3
+ * @param start - The start time string in the format "HH:mm".
4
+ * @param end - The end time string in the format "HH:mm".
5
+ * @param date - The date for which the boundaries are calculated.
6
+ * @returns An array containing the start and end time boundaries as Date objects.
7
+ */
1
8
function getBoundaries ( start : string , end : string , date : Date ) {
2
9
const [ startHour , startMinute ] = start . split ( ":" ) . map ( Number ) ;
3
10
const [ endHour , endMinute ] = end . split ( ":" ) . map ( Number ) ;
@@ -11,16 +18,29 @@ function getBoundaries(start: string, end: string, date: Date) {
11
18
return [ startTime , endTime ] ;
12
19
}
13
20
21
+ /**
22
+ * Add a specified number of seconds to a given date.
23
+ * @param date - The date to which the seconds are added.
24
+ * @param seconds - The number of seconds to add.
25
+ * @returns A new Date object with the added seconds.
26
+ */
14
27
function addSeconds ( date : Date , seconds : number ) : Date {
15
28
return new Date ( date . getTime ( ) + seconds * 1000 ) ;
16
29
}
17
30
31
+ /**
32
+ * Represents a scheduler that manages working hours and holidays.
33
+ */
18
34
export class Scheduler {
19
35
private hours : {
20
36
[ day : number ] : Array < { start : string ; end : string } > | null ;
21
37
} ;
22
38
private holidays : Date [ ] ;
23
39
40
+ /**
41
+ * Creates a new instance of the Scheduler.
42
+ * @param config - The configuration object containing working hours and holidays.
43
+ */
24
44
constructor ( config : {
25
45
hours : { [ day : number ] : Array < { start : string ; end : string } > | null } ;
26
46
holidays : Date [ ] ;
@@ -29,6 +49,11 @@ export class Scheduler {
29
49
this . holidays = config . holidays ;
30
50
}
31
51
52
+ /**
53
+ * Checks if a given date is a working day.
54
+ * @param date - The date to check.
55
+ * @returns True if the date is a working day, false otherwise.
56
+ */
32
57
public isWorkingDay ( date : Date ) : boolean {
33
58
if ( this . isHoliday ( date ) ) {
34
59
return false ;
@@ -37,6 +62,11 @@ export class Scheduler {
37
62
return workingHours !== null && workingHours . length > 0 ;
38
63
}
39
64
65
+ /**
66
+ * Checks if a given date and time is within working hours.
67
+ * @param date - The date and time to check.
68
+ * @returns True if the date and time is within working hours, false otherwise.
69
+ */
40
70
public isWorkingTime ( date : Date ) : boolean {
41
71
if ( ! this . isWorkingDay ( date ) ) {
42
72
return false ;
@@ -56,6 +86,11 @@ export class Scheduler {
56
86
return false ;
57
87
}
58
88
89
+ /**
90
+ * Checks if a given date is a holiday.
91
+ * @param date - The date to check.
92
+ * @returns True if the date is a holiday, false otherwise.
93
+ */
59
94
public isHoliday ( date : Date ) : boolean {
60
95
const dateOnly = date . toDateString ( ) ;
61
96
return this . holidays . some ( ( holiday ) => holiday . toDateString ( ) === dateOnly ) ;
@@ -80,6 +115,11 @@ export class Scheduler {
80
115
return nextTime ;
81
116
}
82
117
118
+ /**
119
+ * Get the next working day after a given date.
120
+ * @param date - The date after which the next working day is calculated.
121
+ * @returns The next working day as a Date object.
122
+ */
83
123
public nextWorkingDay ( date : Date ) : Date {
84
124
let nextDay = new Date ( date ) ;
85
125
nextDay . setDate ( nextDay . getDate ( ) + 1 ) ;
@@ -91,6 +131,11 @@ export class Scheduler {
91
131
return nextDay ;
92
132
}
93
133
134
+ /**
135
+ * Get the remaining working time for a given date.
136
+ * @param date - The date for which the remaining working time is calculated.
137
+ * @returns The remaining working time in seconds.
138
+ */
94
139
public getRemainingWorkingTime ( date : Date ) : number {
95
140
const workingHours = this . getWorkingHours ( date ) ;
96
141
@@ -107,6 +152,11 @@ export class Scheduler {
107
152
return 0 ;
108
153
}
109
154
155
+ /**
156
+ * Get the elapsed working time for a given date.
157
+ * @param date - The date for which the elapsed working time is calculated.
158
+ * @returns The elapsed working time in seconds.
159
+ */
110
160
public getElapsedWorkingTime ( date : Date ) : number {
111
161
const workingHours = this . getWorkingHours ( date ) ;
112
162
@@ -123,6 +173,12 @@ export class Scheduler {
123
173
return 0 ;
124
174
}
125
175
176
+ /**
177
+ * Add a specified number of seconds to a given date.
178
+ * @param date - The date to which the seconds are added.
179
+ * @param seconds - The number of seconds to add.
180
+ * @returns A new Date object with the added seconds.
181
+ */
126
182
public addTime ( date : Date , seconds : number ) : Date {
127
183
let newDate = new Date ( date ) ;
128
184
let remainingSeconds = seconds ;
@@ -142,6 +198,11 @@ export class Scheduler {
142
198
return newDate ;
143
199
}
144
200
201
+ /**
202
+ * Get the working hours for a given date.
203
+ * @param date - The date for which the working hours are retrieved.
204
+ * @returns The working hours for the given date.
205
+ */
145
206
private getWorkingHours ( date : Date ) {
146
207
const dayOfWeek = date . getDay ( ) ;
147
208
return this . hours [ dayOfWeek ] ;
0 commit comments