1
|
|
|
import { Injectable } from '@nestjs/common'; |
2
|
|
|
import { |
3
|
|
|
format as fnsFormat, |
4
|
|
|
isWeekend as fnsIsWeekend, |
5
|
|
|
getDaysInMonth as fnsGetDaysInMonth, |
6
|
|
|
eachDayOfInterval, |
7
|
|
|
addDays, |
8
|
|
|
getYear |
9
|
|
|
} from 'date-fns'; |
10
|
|
|
import { MonthDate } from 'src/Application/Common/MonthDate'; |
11
|
|
|
import { IDateUtils } from 'src/Application/IDateUtils'; |
12
|
|
|
|
13
|
|
|
@Injectable() |
14
|
|
|
export class DateUtilsAdapter implements IDateUtils { |
15
|
|
|
public format(date: Date, format: string): string { |
16
|
|
|
return fnsFormat(date, format); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public getDaysInMonth(date: Date): number { |
20
|
|
|
return fnsGetDaysInMonth(date); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public isWeekend(date: Date): boolean { |
24
|
|
|
return fnsIsWeekend(date); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public isAWorkingDay(date: Date): boolean { |
28
|
|
|
if (this.isWeekend(date)) { |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
const workedFreeDays = this.getWorkedFreeDays(date.getFullYear()); |
33
|
|
|
const formatedDate = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`; |
34
|
|
|
|
35
|
|
|
for (const day of workedFreeDays) { |
36
|
|
|
const formatedDay = `${day.getFullYear()}-${day.getMonth()}-${day.getDate()}`; |
37
|
|
|
|
38
|
|
|
if (formatedDate === formatedDay) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public getCurrentDate(): Date { |
47
|
|
|
return new Date(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public getYear(date: Date): number { |
51
|
|
|
return getYear(date); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public getMonth(date: Date): MonthDate { |
55
|
|
|
return new MonthDate(date.getFullYear(), date.getMonth() + 1); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public getLastDayOfYear(date: Date): Date { |
59
|
|
|
return new Date(`${getYear(date)}/12/31`); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public getFirstDayOfYear(date: Date): Date { |
63
|
|
|
return new Date(`${getYear(date)}/01/01`); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public getCurrentDateToISOString(): string { |
67
|
|
|
return this.getCurrentDate().toISOString(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public addDaysToDate(date: Date, days: number): Date { |
71
|
|
|
return addDays(date, days); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public getWorkedDaysDuringAPeriod(start: Date, end: Date): Date[] { |
75
|
|
|
const dates: Date[] = []; |
76
|
|
|
const workedFreeDays: Date[] = []; |
77
|
|
|
|
78
|
|
|
for (let year = start.getFullYear(); year <= end.getFullYear(); year++) { |
79
|
|
|
workedFreeDays.push(...this.getWorkedFreeDays(year)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
for (const day of eachDayOfInterval({ start, end })) { |
83
|
|
|
if ( |
84
|
|
|
this.isWeekend(day) || |
85
|
|
|
workedFreeDays.filter(d => d.toISOString() === day.toISOString()) |
86
|
|
|
.length > 0 |
87
|
|
|
) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
dates.push(day); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return dates; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public getWorkedFreeDays(year: number): Date[] { |
98
|
|
|
const fixedDays: Date[] = [ |
99
|
|
|
new Date(`${year}-01-01`), // New Year's Day |
100
|
|
|
new Date(`${year}-05-01`), // Labour Day |
101
|
|
|
new Date(`${year}-05-08`), // Victory in 1945 |
102
|
|
|
new Date(`${year}-07-14`), // National Day |
103
|
|
|
new Date(`${year}-08-15`), // Assumption |
104
|
|
|
new Date(`${year}-11-01`), // All Saints' Day |
105
|
|
|
new Date(`${year}-11-11`), // The Armistice |
106
|
|
|
new Date(`${year}-12-25`) // Christmas |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
const easterDate = this.getEasterDate(year); |
110
|
|
|
const easterDays: Date[] = [ |
111
|
|
|
addDays(easterDate, 1), // Easter Monday |
112
|
|
|
addDays(easterDate, 39) // Ascension |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
return [...fixedDays, ...easterDays]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public getWorkedFreeDaysInMonth(year: number, month: number): Date[] { |
119
|
|
|
return this.getWorkedFreeDays(year).filter( |
120
|
|
|
date => date.getUTCMonth() === month |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public getEasterDate(year: number): Date { |
125
|
|
|
const a = year % 19; |
126
|
|
|
const b = Math.floor(year / 100); |
127
|
|
|
const c = year % 100; |
128
|
|
|
const d = Math.floor(b / 4); |
129
|
|
|
const e = b % 4; |
130
|
|
|
const f = Math.floor((b + 8) / 25); |
131
|
|
|
const g = Math.floor((b - f + 1) / 3); |
132
|
|
|
const h = (19 * a + b - d - g + 15) % 30; |
133
|
|
|
const i = Math.floor(c / 4); |
134
|
|
|
const k = c % 4; |
135
|
|
|
const l = (32 + 2 * e + 2 * i - h - k) % 7; |
136
|
|
|
const m = Math.floor((a + 11 * h + 22 * l) / 451); |
137
|
|
|
const n0 = h + l + 7 * m + 114; |
138
|
|
|
const n = Math.floor(n0 / 31) - 1; |
139
|
|
|
const p = (n0 % 31) + 1; |
140
|
|
|
|
141
|
|
|
return new Date(year, n, p); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public getLeaveDuration( |
145
|
|
|
startDate: string, |
146
|
|
|
isStartsAllDay: boolean, |
147
|
|
|
endDate: string, |
148
|
|
|
isEndsAllDay: boolean |
149
|
|
|
): number { |
150
|
|
|
let duration = this.getWorkedDaysDuringAPeriod( |
151
|
|
|
new Date(startDate), |
152
|
|
|
new Date(endDate) |
153
|
|
|
).length; |
154
|
|
|
|
155
|
|
|
if (false === isStartsAllDay) { |
156
|
|
|
duration -= 0.5; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (false === isEndsAllDay && duration > 0.5) { |
160
|
|
|
duration -= 0.5; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return duration; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|