forked from batoulapps/adhan-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrayerTimes.ts
289 lines (266 loc) · 8.8 KB
/
PrayerTimes.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import SolarTime from './SolarTime';
import TimeComponents from './TimeComponents';
import Prayer from './Prayer';
import Astronomical from './Astronomical';
import CalculationParameters from './CalculationParameters';
import Coordinates from './Coordinates';
import {
dateByAddingDays,
dateByAddingMinutes,
dateByAddingSeconds,
dayOfYear,
isValidDate,
roundedMinute,
} from './DateUtils';
import { shadowLength } from './Madhab';
import {
PolarCircleResolution,
polarCircleResolvedValues,
} from './PolarCircleResolution';
import { ValueOf } from './TypeUtils';
export default class PrayerTimes {
fajr: Date;
sunrise: Date;
dhuhr: Date;
asr: Date;
sunset: Date;
maghrib: Date;
isha: Date;
// eslint-disable-next-line complexity
constructor(
public coordinates: Coordinates,
public date: Date,
public calculationParameters: CalculationParameters,
) {
let solarTime = new SolarTime(date, coordinates);
let fajrTime: Date;
let sunriseTime: Date;
let dhuhrTime: Date;
let asrTime: Date;
let sunsetTime: Date;
let maghribTime: Date;
let ishaTime: Date;
let nightFraction;
dhuhrTime = new TimeComponents(solarTime.transit).utcDate(
date.getFullYear(),
date.getMonth(),
date.getDate(),
);
sunriseTime = new TimeComponents(solarTime.sunrise).utcDate(
date.getFullYear(),
date.getMonth(),
date.getDate(),
);
sunsetTime = new TimeComponents(solarTime.sunset).utcDate(
date.getFullYear(),
date.getMonth(),
date.getDate(),
);
const tomorrow = dateByAddingDays(date, 1);
let tomorrowSolarTime = new SolarTime(tomorrow, coordinates);
const polarCircleResolver = calculationParameters.polarCircleResolution;
if (
(!isValidDate(sunriseTime) ||
!isValidDate(sunsetTime) ||
isNaN(tomorrowSolarTime.sunrise)) &&
polarCircleResolver !== PolarCircleResolution.Unresolved
) {
const resolved = polarCircleResolvedValues(
polarCircleResolver,
date,
coordinates,
);
solarTime = resolved.solarTime;
tomorrowSolarTime = resolved.tomorrowSolarTime;
const dateComponents = [
date.getFullYear(),
date.getMonth(),
date.getDate(),
] as const;
dhuhrTime = new TimeComponents(solarTime.transit).utcDate(
...dateComponents,
);
sunriseTime = new TimeComponents(solarTime.sunrise).utcDate(
...dateComponents,
);
sunsetTime = new TimeComponents(solarTime.sunset).utcDate(
...dateComponents,
);
}
// eslint-disable-next-line prefer-const
asrTime = new TimeComponents(
solarTime.afternoon(shadowLength(calculationParameters.madhab)),
).utcDate(date.getFullYear(), date.getMonth(), date.getDate());
const tomorrowSunrise = new TimeComponents(
tomorrowSolarTime.sunrise,
).utcDate(tomorrow.getFullYear(), tomorrow.getMonth(), tomorrow.getDate());
const night = (Number(tomorrowSunrise) - Number(sunsetTime)) / 1000;
fajrTime = new TimeComponents(
solarTime.hourAngle(-1 * calculationParameters.fajrAngle, false),
).utcDate(date.getFullYear(), date.getMonth(), date.getDate());
// special case for moonsighting committee above latitude 55
if (
calculationParameters.method === 'MoonsightingCommittee' &&
coordinates.latitude >= 55
) {
nightFraction = night / 7;
fajrTime = dateByAddingSeconds(sunriseTime, -nightFraction);
}
const safeFajr = (function () {
if (calculationParameters.method === 'MoonsightingCommittee') {
return Astronomical.seasonAdjustedMorningTwilight(
coordinates.latitude,
dayOfYear(date),
date.getFullYear(),
sunriseTime,
);
} else {
const portion = calculationParameters.nightPortions().fajr;
nightFraction = portion * night;
return dateByAddingSeconds(sunriseTime, -nightFraction);
}
})();
if (isNaN(fajrTime.getTime()) || safeFajr > fajrTime) {
fajrTime = safeFajr;
}
if (calculationParameters.ishaInterval > 0) {
ishaTime = dateByAddingMinutes(
sunsetTime,
calculationParameters.ishaInterval,
);
} else {
ishaTime = new TimeComponents(
solarTime.hourAngle(-1 * calculationParameters.ishaAngle, true),
).utcDate(date.getFullYear(), date.getMonth(), date.getDate());
// special case for moonsighting committee above latitude 55
if (
calculationParameters.method === 'MoonsightingCommittee' &&
coordinates.latitude >= 55
) {
nightFraction = night / 7;
ishaTime = dateByAddingSeconds(sunsetTime, nightFraction);
}
const safeIsha = (function () {
if (calculationParameters.method === 'MoonsightingCommittee') {
return Astronomical.seasonAdjustedEveningTwilight(
coordinates.latitude,
dayOfYear(date),
date.getFullYear(),
sunsetTime,
calculationParameters.shafaq,
);
} else {
const portion = calculationParameters.nightPortions().isha;
nightFraction = portion * night;
return dateByAddingSeconds(sunsetTime, nightFraction);
}
})();
if (isNaN(ishaTime.getTime()) || safeIsha < ishaTime) {
ishaTime = safeIsha;
}
}
maghribTime = sunsetTime;
if (calculationParameters.maghribAngle) {
const angleBasedMaghrib = new TimeComponents(
solarTime.hourAngle(-1 * calculationParameters.maghribAngle, true),
).utcDate(date.getFullYear(), date.getMonth(), date.getDate());
if (sunsetTime < angleBasedMaghrib && ishaTime > angleBasedMaghrib) {
maghribTime = angleBasedMaghrib;
}
}
const fajrAdjustment =
(calculationParameters.adjustments.fajr || 0) +
(calculationParameters.methodAdjustments.fajr || 0);
const sunriseAdjustment =
(calculationParameters.adjustments.sunrise || 0) +
(calculationParameters.methodAdjustments.sunrise || 0);
const dhuhrAdjustment =
(calculationParameters.adjustments.dhuhr || 0) +
(calculationParameters.methodAdjustments.dhuhr || 0);
const asrAdjustment =
(calculationParameters.adjustments.asr || 0) +
(calculationParameters.methodAdjustments.asr || 0);
const maghribAdjustment =
(calculationParameters.adjustments.maghrib || 0) +
(calculationParameters.methodAdjustments.maghrib || 0);
const ishaAdjustment =
(calculationParameters.adjustments.isha || 0) +
(calculationParameters.methodAdjustments.isha || 0);
this.fajr = roundedMinute(
dateByAddingMinutes(fajrTime, fajrAdjustment),
calculationParameters.rounding,
);
this.sunrise = roundedMinute(
dateByAddingMinutes(sunriseTime, sunriseAdjustment),
calculationParameters.rounding,
);
this.dhuhr = roundedMinute(
dateByAddingMinutes(dhuhrTime, dhuhrAdjustment),
calculationParameters.rounding,
);
this.asr = roundedMinute(
dateByAddingMinutes(asrTime, asrAdjustment),
calculationParameters.rounding,
);
this.sunset = roundedMinute(sunsetTime, calculationParameters.rounding);
this.maghrib = roundedMinute(
dateByAddingMinutes(maghribTime, maghribAdjustment),
calculationParameters.rounding,
);
this.isha = roundedMinute(
dateByAddingMinutes(ishaTime, ishaAdjustment),
calculationParameters.rounding,
);
}
timeForPrayer(prayer: ValueOf<typeof Prayer>) {
if (prayer === Prayer.Fajr) {
return this.fajr;
} else if (prayer === Prayer.Sunrise) {
return this.sunrise;
} else if (prayer === Prayer.Dhuhr) {
return this.dhuhr;
} else if (prayer === Prayer.Asr) {
return this.asr;
} else if (prayer === Prayer.Maghrib) {
return this.maghrib;
} else if (prayer === Prayer.Isha) {
return this.isha;
} else {
return null;
}
}
currentPrayer(date = new Date()) {
if (date >= this.isha) {
return Prayer.Isha;
} else if (date >= this.maghrib) {
return Prayer.Maghrib;
} else if (date >= this.asr) {
return Prayer.Asr;
} else if (date >= this.dhuhr) {
return Prayer.Dhuhr;
} else if (date >= this.sunrise) {
return Prayer.Sunrise;
} else if (date >= this.fajr) {
return Prayer.Fajr;
} else {
return Prayer.None;
}
}
nextPrayer(date = new Date()) {
if (date >= this.isha) {
return Prayer.None;
} else if (date >= this.maghrib) {
return Prayer.Isha;
} else if (date >= this.asr) {
return Prayer.Maghrib;
} else if (date >= this.dhuhr) {
return Prayer.Asr;
} else if (date >= this.sunrise) {
return Prayer.Dhuhr;
} else if (date >= this.fajr) {
return Prayer.Sunrise;
} else {
return Prayer.Fajr;
}
}
}