-
Notifications
You must be signed in to change notification settings - Fork 86
/
PrayerTimes.js
213 lines (189 loc) · 8.64 KB
/
PrayerTimes.js
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
import SolarTime from './SolarTime';
import TimeComponents from './TimeComponents';
import Prayer from './Prayer';
import Astronomical from './Astronomical';
import {
dateByAddingDays,
dateByAddingMinutes,
dateByAddingSeconds,
roundedMinute,
dayOfYear,
isValidDate
} from './DateUtils';
import { Madhab, shadowLength } from './Madhab';
import { PolarCircleResolution, polarCircleResolvedValues } from './PolarCircleResolution';
export default class PrayerTimes {
constructor(coordinates, date, calculationParameters) {
this.coordinates = coordinates;
this.date = date;
this.calculationParameters = calculationParameters;
var solarTime = new SolarTime(date, coordinates);
var fajrTime;
var sunriseTime;
var dhuhrTime;
var asrTime;
var maghribTime;
var ishaTime;
var 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());
var sunsetTime = new TimeComponents(solarTime.sunset).utcDate(date.getFullYear(), date.getMonth(), date.getDate());
var tomorrow = dateByAddingDays(date, 1);
var 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);
this.coordinates = resolved.coordinates;
this.date.setTime(resolved.date.getTime());
solarTime = resolved.solarTime;
tomorrow = resolved.tomorrow;
tomorrowSolarTime = resolved.tomorrowSolarTime;
const dateComponents = [date.getFullYear(), date.getMonth(), date.getDate()];
dhuhrTime = new TimeComponents(solarTime.transit).utcDate(...dateComponents);
sunriseTime = new TimeComponents(solarTime.sunrise).utcDate(...dateComponents);
sunsetTime = new TimeComponents(solarTime.sunset).utcDate(...dateComponents);
}
asrTime = new TimeComponents(solarTime.afternoon(shadowLength(calculationParameters.madhab))).utcDate(date.getFullYear(), date.getMonth(), date.getDate());
var tomorrowSunrise = new TimeComponents(tomorrowSolarTime.sunrise).utcDate(tomorrow.getFullYear(), tomorrow.getMonth(), tomorrow.getDate());
var night = (tomorrowSunrise - 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);
}
var safeFajr = (function () {
if (calculationParameters.method == "MoonsightingCommittee") {
return Astronomical.seasonAdjustedMorningTwilight(coordinates.latitude, dayOfYear(date), date.getFullYear(), sunriseTime);
}
else {
var portion = calculationParameters.nightPortions().fajr;
nightFraction = portion * night;
return dateByAddingSeconds(sunriseTime, -nightFraction);
}
})();
if (fajrTime == null || 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);
}
var safeIsha = (function () {
if (calculationParameters.method == "MoonsightingCommittee") {
return Astronomical.seasonAdjustedEveningTwilight(coordinates.latitude, dayOfYear(date), date.getFullYear(), sunsetTime);
}
else {
var portion = calculationParameters.nightPortions().isha;
nightFraction = portion * night;
return dateByAddingSeconds(sunsetTime, nightFraction);
}
})();
if (ishaTime == null || isNaN(ishaTime.getTime()) || safeIsha < ishaTime) {
ishaTime = safeIsha;
}
}
maghribTime = sunsetTime;
if (calculationParameters.maghribAngle) {
let angleBasedMaghrib = new TimeComponents(solarTime.hourAngle(-1 * calculationParameters.maghribAngle, true)).utcDate(date.getFullYear(), date.getMonth(), date.getDate());
if (sunsetTime < angleBasedMaghrib && ishaTime > angleBasedMaghrib) {
maghribTime = angleBasedMaghrib;
}
}
var fajrAdjustment = (calculationParameters.adjustments.fajr || 0) + (calculationParameters.methodAdjustments.fajr || 0);
var sunriseAdjustment = (calculationParameters.adjustments.sunrise || 0) + (calculationParameters.methodAdjustments.sunrise || 0);
var dhuhrAdjustment = (calculationParameters.adjustments.dhuhr || 0) + (calculationParameters.methodAdjustments.dhuhr || 0);
var asrAdjustment = (calculationParameters.adjustments.asr || 0) + (calculationParameters.methodAdjustments.asr || 0);
var maghribAdjustment = (calculationParameters.adjustments.maghrib || 0) + (calculationParameters.methodAdjustments.maghrib || 0);
var ishaAdjustment = (calculationParameters.adjustments.isha || 0) + (calculationParameters.methodAdjustments.isha || 0);
this.fajr = roundedMinute(dateByAddingMinutes(fajrTime, fajrAdjustment));
this.sunrise = roundedMinute(dateByAddingMinutes(sunriseTime, sunriseAdjustment));
this.dhuhr = roundedMinute(dateByAddingMinutes(dhuhrTime, dhuhrAdjustment));
this.asr = roundedMinute(dateByAddingMinutes(asrTime, asrAdjustment));
this.maghrib = roundedMinute(dateByAddingMinutes(maghribTime, maghribAdjustment));
this.isha = roundedMinute(dateByAddingMinutes(ishaTime, ishaAdjustment));
}
timeForPrayer(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) {
if (typeof date === 'undefined') {
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) {
if (typeof date === 'undefined') {
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;
}
}
}