forked from batoulapps/Adhan
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdhan.js
More file actions
861 lines (731 loc) · 33.6 KB
/
Adhan.js
File metadata and controls
861 lines (731 loc) · 33.6 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
"use strict";
// UMD pattern from https://github.com/umdjs/umd
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.adhan = factory();
}
}(this, function () {
var Prayer = {
Fajr: 0,
Sunrise: 1,
Dhuhr: 2,
Asr: 3,
Maghrib: 4,
Isha: 5,
None: 6
};
var Madhab = {
Shafi: 1,
Hanafi: 2
};
var HighLatitudeRule = {
MiddleOfTheNight: 1,
SeventhOfTheNight: 2,
TwilightAngle: 3
};
function Coordinates(latitude, longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
function CalculationParameters(fajrAngle, ishaAngle, ishaInterval, methodName) {
this.method = methodName || "Other";
this.fajrAngle = fajrAngle || 0;
this.ishaAngle = ishaAngle || 0;
this.ishaInterval = ishaInterval || 0;
this.madhab = Madhab.Shafi;
this.highLatitudeRule = HighLatitudeRule.MiddleOfTheNight;
this.adjustments = { fajr: 0, sunrise: 0, dhuhr: 0, asr: 0, maghrib: 0, isha: 0 };
this.nightPortions = function() {
switch(this.highLatitudeRule) {
case HighLatitudeRule.MiddleOfTheNight:
return { fajr: 1/2, isha: 1/2 };
case HighLatitudeRule.SeventhOfTheNight:
return { fajr: 1/7, isha: 1/7 };
case HighLatitudeRule.TwilightAngle:
return { fajr: this.fajrAngle / 60, isha: this.ishaAngle / 60 };
}
}
}
var CalculationMethod = {
// Muslim World League
MuslimWorldLeague: function() {
return new CalculationParameters(18, 17, 0, "MuslimWorldLeague");
},
// Egyptian General Authority of Survey
Egyptian: function() {
return new CalculationParameters(19.5, 17.5, 0, "Egyptian");
},
// University of Islamic Sciences, Karachi
Karachi: function() {
return new CalculationParameters(18, 18, 0, "Karachi");
},
// Umm al-Qura University, Makkah
UmmAlQura: function() {
return new CalculationParameters(18.5, 0, 90, "UmmAlQura");
},
// The Gulf Region
Gulf: function() {
return new CalculationParameters(19.5, 0, 90, "Gulf");
},
// Moonsighting Committee
MoonsightingCommittee: function() {
return new CalculationParameters(18, 18, 0, "MoonsightingCommittee");
},
// ISNA
NorthAmerica: function() {
return new CalculationParameters(15, 15, 0, "NorthAmerica");
},
// Kuwait
Kuwait: function() {
return new CalculationParameters(18, 17.5, 0, "Kuwait");
},
// Qatar
Qatar: function() {
return new CalculationParameters(18, 0, 90, "Qatar");
},
// Other
Other: function() {
return new CalculationParameters(0, 0, 0, "Other");
}
};
function PrayerTimes(coordinates, date, calculationParameters) {
var solarTime = new SolarTime(date, coordinates);
var fajrTime;
var sunriseTime;
var dhuhrTime;
var asrTime;
var maghribTime;
var ishaTime;
var nightFraction;
dhuhrTime = timeComponents(solarTime.transit).UTCDate(date.getFullYear(), date.getMonth(), date.getDate());
sunriseTime = timeComponents(solarTime.sunrise).UTCDate(date.getFullYear(), date.getMonth(), date.getDate());
maghribTime = timeComponents(solarTime.sunset).UTCDate(date.getFullYear(), date.getMonth(), date.getDate());
asrTime = timeComponents(solarTime.afternoon(calculationParameters.madhab)).UTCDate(date.getFullYear(), date.getMonth(), date.getDate());
var tomorrowSunrise = dateByAddingDays(sunriseTime, 1);
var night = (tomorrowSunrise - maghribTime) / 1000;
fajrTime = 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(maghribTime, calculationParameters.ishaInterval);
} else {
ishaTime = 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(maghribTime, nightFraction);
}
var safeIsha = (function(){
if (calculationParameters.method == "MoonsightingCommittee") {
return Astronomical.seasonAdjustedEveningTwilight(coordinates.latitude, dayOfYear(date), date.getFullYear(), maghribTime);
} else {
var portion = calculationParameters.nightPortions().isha;
nightFraction = portion * night;
return dateByAddingSeconds(maghribTime, nightFraction);
}
})();
if (ishaTime == null || isNaN(ishaTime.getTime()) || safeIsha < ishaTime) {
ishaTime = safeIsha
}
}
// method based offsets
var dhuhrOffset = (function(){
switch(calculationParameters.method) {
case "MoonsightingCommittee":
// Moonsighting Committee requires 5 minutes for
// the sun to pass the zenith and dhuhr to enter
return 5;
case "UmmAlQura":
case "Gulf":
case "Qatar":
// UmmAlQura and derivatives don't add
// anything to zenith for dhuhr
return 0;
default:
// Default behavior waits 1 minute for the
// sun to pass the zenith and dhuhr to enter
return 1;
}
})();
var maghribOffset = (function(){
switch(calculationParameters.method) {
case "MoonsightingCommittee":
// Moonsighting Committee adds 3 minutes to
// sunset time to account for light refraction
return 3;
default:
return 0;
}
})();
this.fajr = roundedMinute(dateByAddingMinutes(fajrTime, calculationParameters.adjustments.fajr));
this.sunrise = roundedMinute(dateByAddingMinutes(sunriseTime, calculationParameters.adjustments.sunrise));
this.dhuhr = roundedMinute(dateByAddingMinutes(dateByAddingMinutes(dhuhrTime, calculationParameters.adjustments.dhuhr), dhuhrOffset));
this.asr = roundedMinute(dateByAddingMinutes(asrTime, calculationParameters.adjustments.asr));
this.maghrib = roundedMinute(dateByAddingMinutes(dateByAddingMinutes(maghribTime, calculationParameters.adjustments.maghrib), maghribOffset));
this.isha = roundedMinute(dateByAddingMinutes(ishaTime, calculationParameters.adjustments.isha));
this.timeForPrayer = function(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;
}
};
this.currentPrayer = function(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;
}
};
this.nextPrayer = function(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;
}
}
}
//
// Astronomical equations
//
function SolarTime(date, coordinates) {
// calculations need to occur at 0h0m UTC
date.setHours(0);
date.setMinutes(0);
this.date = date;
this.observer = coordinates;
this.solar = new SolarCoordinates(julianDate(date));
var previous = new Date(date.getFullYear(), date.getMonth(), date.getDate() - 1);
var next = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
this.prevSolar = new SolarCoordinates(julianDate(previous));
this.nextSolar = new SolarCoordinates(julianDate(next));
var m0 = Astronomical.approximateTransit(coordinates.longitude, this.solar.apparentSiderealTime, this.solar.rightAscension);
var solarAltitude = -50.0 / 60.0;
this.approxTransit = m0;
this.transit = Astronomical.correctedTransit(m0, coordinates.longitude, this.solar.apparentSiderealTime,
this.solar.rightAscension, this.prevSolar.rightAscension, this.nextSolar.rightAscension);
this.sunrise = Astronomical.correctedHourAngle(m0, solarAltitude, coordinates, false, this.solar.apparentSiderealTime,
this.solar.rightAscension, this.prevSolar.rightAscension, this.nextSolar.rightAscension,
this.solar.declination, this.prevSolar.declination, this.nextSolar.declination);
this.sunset = Astronomical.correctedHourAngle(m0, solarAltitude, coordinates, true, this.solar.apparentSiderealTime,
this.solar.rightAscension, this.prevSolar.rightAscension, this.nextSolar.rightAscension,
this.solar.declination, this.prevSolar.declination, this.nextSolar.declination);
// Methods
this.hourAngle = function(angle, afterTransit) {
return Astronomical.correctedHourAngle(this.approxTransit, angle, this.observer, afterTransit, this.solar.apparentSiderealTime,
this.solar.rightAscension, this.prevSolar.rightAscension, this.nextSolar.rightAscension,
this.solar.declination, this.prevSolar.declination, this.nextSolar.declination);
};
this.afternoon = function(shadowLength) {
// TODO source shadow angle calculation
var tangent = Math.abs(this.observer.latitude - this.solar.declination);
var inverse = shadowLength + Math.tan(degreesToRadians(tangent));
var angle = radiansToDegrees(Math.atan(1.0 / inverse));
return this.hourAngle(angle, true);
}
}
function SolarCoordinates(julianDay) {
/* declination: The declination of the sun, the angle between
the rays of the Sun and the plane of the Earth's
equator, in degrees. */
/* rightAscension: Right ascension of the Sun, the angular distance on the
celestial equator from the vernal equinox to the hour circle,
in degrees. */
/* apparentSiderealTime: Apparent sidereal time, the hour angle of the vernal
equinox, in degrees. */
var T = Astronomical.julianCentury(julianDay);
var L0 = Astronomical.meanSolarLongitude(T);
var Lp = Astronomical.meanLunarLongitude(T);
var Omega = Astronomical.ascendingLunarNodeLongitude(T);
var Lambda = degreesToRadians(Astronomical.apparentSolarLongitude(T, L0));
var Theta0 = Astronomical.meanSiderealTime(T);
var dPsi = Astronomical.nutationInLongitude(T, L0, Lp, Omega);
var dEpsilon = Astronomical.nutationInObliquity(T, L0, Lp, Omega);
var Epsilon0 = Astronomical.meanObliquityOfTheEcliptic(T);
var EpsilonApparent = degreesToRadians(Astronomical.apparentObliquityOfTheEcliptic(T, Epsilon0));
/* Equation from Astronomical Algorithms page 165 */
this.declination = radiansToDegrees(Math.asin(Math.sin(EpsilonApparent) * Math.sin(Lambda)));
/* Equation from Astronomical Algorithms page 165 */
this.rightAscension = unwindAngle(radiansToDegrees(Math.atan2(Math.cos(EpsilonApparent) * Math.sin(Lambda), Math.cos(Lambda))));
/* Equation from Astronomical Algorithms page 88 */
this.apparentSiderealTime = Theta0 + (((dPsi * 3600) * Math.cos(degreesToRadians(Epsilon0 + dEpsilon))) / 3600);
}
var Astronomical = {
/* The geometric mean longitude of the sun in degrees. */
meanSolarLongitude: function(julianCentury) {
var T = julianCentury;
/* Equation from Astronomical Algorithms page 163 */
var term1 = 280.4664567;
var term2 = 36000.76983 * T;
var term3 = 0.0003032 * Math.pow(T, 2);
var L0 = term1 + term2 + term3;
return unwindAngle(L0);
},
/* The geometric mean longitude of the moon in degrees. */
meanLunarLongitude: function(julianCentury) {
var T = julianCentury;
/* Equation from Astronomical Algorithms page 144 */
var term1 = 218.3165;
var term2 = 481267.8813 * T;
var Lp = term1 + term2;
return unwindAngle(Lp);
},
ascendingLunarNodeLongitude: function(julianCentury) {
var T = julianCentury;
/* Equation from Astronomical Algorithms page 144 */
var term1 = 125.04452;
var term2 = 1934.136261 * T;
var term3 = 0.0020708 * Math.pow(T, 2);
var term4 = Math.pow(T, 3) / 450000;
var Omega = term1 - term2 + term3 + term4;
return unwindAngle(Omega);
},
/* The mean anomaly of the sun. */
meanSolarAnomaly: function(julianCentury) {
var T = julianCentury;
/* Equation from Astronomical Algorithms page 163 */
var term1 = 357.52911;
var term2 = 35999.05029 * T;
var term3 = 0.0001537 * Math.pow(T, 2);
var M = term1 + term2 - term3;
return unwindAngle(M);
},
/* The Sun's equation of the center in degrees. */
solarEquationOfTheCenter: function(julianCentury, meanAnomaly) {
var T = julianCentury;
/* Equation from Astronomical Algorithms page 164 */
var Mrad = degreesToRadians(meanAnomaly);
var term1 = (1.914602 - (0.004817 * T) - (0.000014 * Math.pow(T, 2))) * Math.sin(Mrad);
var term2 = (0.019993 - (0.000101 * T)) * Math.sin(2 * Mrad);
var term3 = 0.000289 * Math.sin(3 * Mrad);
return term1 + term2 + term3;
},
/* The apparent longitude of the Sun, referred to the
true equinox of the date. */
apparentSolarLongitude: function(julianCentury, meanLongitude) {
var T = julianCentury;
var L0 = meanLongitude;
/* Equation from Astronomical Algorithms page 164 */
var longitude = L0 + Astronomical.solarEquationOfTheCenter(T, Astronomical.meanSolarAnomaly(T));
var Omega = 125.04 - (1934.136 * T);
var Lambda = longitude - 0.00569 - (0.00478 * Math.sin(degreesToRadians(Omega)));
return unwindAngle(Lambda);
},
/* The mean obliquity of the ecliptic, formula
adopted by the International Astronomical Union.
Represented in degrees. */
meanObliquityOfTheEcliptic: function(julianCentury) {
var T = julianCentury;
/* Equation from Astronomical Algorithms page 147 */
var term1 = 23.439291;
var term2 = 0.013004167 * T;
var term3 = 0.0000001639 * Math.pow(T, 2);
var term4 = 0.0000005036 * Math.pow(T, 3);
return term1 - term2 - term3 + term4;
},
/* The mean obliquity of the ecliptic, corrected for
calculating the apparent position of the sun, in degrees. */
apparentObliquityOfTheEcliptic: function(julianCentury, meanObliquityOfTheEcliptic) {
var T = julianCentury;
var Epsilon0 = meanObliquityOfTheEcliptic;
/* Equation from Astronomical Algorithms page 165 */
var O = 125.04 - (1934.136 * T);
return Epsilon0 + (0.00256 * Math.cos(degreesToRadians(O)));
},
/* Mean sidereal time, the hour angle of the vernal equinox, in degrees. */
meanSiderealTime: function(julianCentury) {
var T = julianCentury;
/* Equation from Astronomical Algorithms page 165 */
var JD = (T * 36525) + 2451545.0;
var term1 = 280.46061837;
var term2 = 360.98564736629 * (JD - 2451545);
var term3 = 0.000387933 * Math.pow(T, 2);
var term4 = Math.pow(T, 3) / 38710000;
var Theta = term1 + term2 + term3 - term4;
return unwindAngle(Theta)
},
nutationInLongitude: function(julianCentury, solarLongitude, lunarLongitude, ascendingNode) {
var L0 = solarLongitude;
var Lp = lunarLongitude;
var Omega = ascendingNode;
/* Equation from Astronomical Algorithms page 144 */
var term1 = (-17.2/3600) * Math.sin(degreesToRadians(Omega));
var term2 = (1.32/3600) * Math.sin(2 * degreesToRadians(L0));
var term3 = (0.23/3600) * Math.sin(2 * degreesToRadians(Lp));
var term4 = (0.21/3600) * Math.sin(2 * degreesToRadians(Omega));
return term1 - term2 - term3 + term4;
},
nutationInObliquity: function(julianCentury, solarLongitude, lunarLongitude, ascendingNode) {
var L0 = solarLongitude;
var Lp = lunarLongitude;
var Omega = ascendingNode;
/* Equation from Astronomical Algorithms page 144 */
var term1 = (9.2/3600) * Math.cos(degreesToRadians(Omega));
var term2 = (0.57/3600) * Math.cos(2 * degreesToRadians(L0));
var term3 = (0.10/3600) * Math.cos(2 * degreesToRadians(Lp));
var term4 = (0.09/3600) * Math.cos(2 * degreesToRadians(Omega));
return term1 + term2 + term3 - term4;
},
altitudeOfCelestialBody: function(observerLatitude, declination, localHourAngle) {
var Phi = observerLatitude;
var delta = declination;
var H = localHourAngle;
/* Equation from Astronomical Algorithms page 93 */
var term1 = Math.sin(degreesToRadians(Phi)) * Math.sin(degreesToRadians(delta));
var term2 = Math.cos(degreesToRadians(Phi)) * Math.cos(degreesToRadians(delta)) * Math.cos(degreesToRadians(H));
return radiansToDegrees(Math.asin(term1 + term2));
},
approximateTransit: function(longitude, siderealTime, rightAscension) {
var L = longitude;
var Theta0 = siderealTime;
var a2 = rightAscension;
/* Equation from page Astronomical Algorithms 102 */
var Lw = L * -1;
return normalizeWithBound((a2 + Lw - Theta0) / 360, 1);
},
/* The time at which the sun is at its highest point in the sky (in universal time) */
correctedTransit: function(approximateTransit, longitude, siderealTime, rightAscension, previousRightAscension, nextRightAscension) {
var m0 = approximateTransit;
var L = longitude;
var Theta0 = siderealTime;
var a2 = rightAscension;
var a1 = previousRightAscension;
var a3 = nextRightAscension;
/* Equation from page Astronomical Algorithms 102 */
var Lw = L * -1;
var Theta = unwindAngle((Theta0 + (360.985647 * m0)));
var a = unwindAngle(Astronomical.interpolateAngles(a2, a1, a3, m0));
var H = closestAngle(Theta - Lw - a);
var dm = H / -360;
return (m0 + dm) * 24;
},
correctedHourAngle: function(approximateTransit, angle, coordinates, afterTransit, siderealTime,
rightAscension, previousRightAscension, nextRightAscension, declination, previousDeclination, nextDeclination) {
var m0 = approximateTransit;
var h0 = angle;
var Theta0 = siderealTime;
var a2 = rightAscension;
var a1 = previousRightAscension;
var a3 = nextRightAscension;
var d2 = declination;
var d1 = previousDeclination;
var d3 = nextDeclination;
/* Equation from page Astronomical Algorithms 102 */
var Lw = coordinates.longitude * -1;
var term1 = Math.sin(degreesToRadians(h0)) - (Math.sin(degreesToRadians(coordinates.latitude)) * Math.sin(degreesToRadians(d2)));
var term2 = Math.cos(degreesToRadians(coordinates.latitude)) * Math.cos(degreesToRadians(d2));
var H0 = radiansToDegrees(Math.acos(term1 / term2));
var m = afterTransit ? m0 + (H0 / 360) : m0 - (H0 / 360);
var Theta = unwindAngle((Theta0 + (360.985647 * m)));
var a = unwindAngle(Astronomical.interpolateAngles(a2, a1, a3, m));
var delta = Astronomical.interpolate(d2, d1, d3, m);
var H = (Theta - Lw - a);
var h = Astronomical.altitudeOfCelestialBody(coordinates.latitude, delta, H);
var term3 = h - h0;
var term4 = 360 * Math.cos(degreesToRadians(delta)) * Math.cos(degreesToRadians(coordinates.latitude)) * Math.sin(degreesToRadians(H));
var dm = term3 / term4;
return (m + dm) * 24;
},
/* Interpolation of a value given equidistant
previous and next values and a factor
equal to the fraction of the interpolated
point's time over the time between values. */
interpolate: function(y2, y1, y3, n) {
/* Equation from Astronomical Algorithms page 24 */
var a = y2 - y1;
var b = y3 - y2;
var c = b - a;
return y2 + ((n/2) * (a + b + (n * c)));
},
/* Interpolation of three angles, accounting for
angle unwinding. */
interpolateAngles: function(y2, y1, y3, n) {
/* Equation from Astronomical Algorithms page 24 */
var a = unwindAngle(y2 - y1);
var b = unwindAngle(y3 - y2);
var c = b - a;
return y2 + ((n/2) * (a + b + (n * c)));
},
/* The Julian Day for a given Gregorian date. */
julianDay: function(year, month, day, hours) {
/* Equation from Astronomical Algorithms page 60 */
if (typeof hours === 'undefined') {
hours = 0;
}
var Y = trunc(month > 2 ? year : year - 1);
var M = trunc(month > 2 ? month : month + 12);
var D = day + (hours / 24);
var A = trunc(Y/100);
var B = trunc(2 - A + trunc(A/4));
var i0 = trunc(365.25 * (Y + 4716));
var i1 = trunc(30.6001 * (M + 1));
return i0 + i1 + D + B - 1524.5;
},
/* Julian century from the epoch. */
julianCentury: function(julianDay) {
/* Equation from Astronomical Algorithms page 163 */
return (julianDay - 2451545.0) / 36525;
},
/* Whether or not a year is a leap year (has 366 days). */
isLeapYear: function(year) {
if (year % 4 != 0) {
return false;
}
if (year % 100 == 0 && year % 400 != 0) {
return false;
}
return true;
},
seasonAdjustedMorningTwilight: function(latitude, dayOfYear, year, sunrise) {
var a = 75 + ((28.65 / 55.0) * Math.abs(latitude));
var b = 75 + ((19.44 / 55.0) * Math.abs(latitude));
var c = 75 + ((32.74 / 55.0) * Math.abs(latitude));
var d = 75 + ((48.10 / 55.0) * Math.abs(latitude));
var adjustment = (function() {
var dyy = Astronomical.daysSinceSolstice(dayOfYear, year, latitude);
if ( dyy < 91) {
return a + ( b - a ) / 91.0 * dyy;
} else if ( dyy < 137) {
return b + ( c - b ) / 46.0 * ( dyy - 91 );
} else if ( dyy < 183 ) {
return c + ( d - c ) / 46.0 * ( dyy - 137 );
} else if ( dyy < 229 ) {
return d + ( c - d ) / 46.0 * ( dyy - 183 );
} else if ( dyy < 275 ) {
return c + ( b - c ) / 46.0 * ( dyy - 229 );
} else {
return b + ( a - b ) / 91.0 * ( dyy - 275 );
}
})();
return dateByAddingSeconds(sunrise, Math.round(adjustment * -60.0));
},
seasonAdjustedEveningTwilight: function(latitude, dayOfYear, year, sunset) {
var a = 75 + ((25.60 / 55.0) * Math.abs(latitude));
var b = 75 + ((2.050 / 55.0) * Math.abs(latitude));
var c = 75 - ((9.210 / 55.0) * Math.abs(latitude));
var d = 75 + ((6.140 / 55.0) * Math.abs(latitude));
var adjustment = (function() {
var dyy = Astronomical.daysSinceSolstice(dayOfYear, year, latitude);
if ( dyy < 91) {
return a + ( b - a ) / 91.0 * dyy;
} else if ( dyy < 137) {
return b + ( c - b ) / 46.0 * ( dyy - 91 );
} else if ( dyy < 183 ) {
return c + ( d - c ) / 46.0 * ( dyy - 137 );
} else if ( dyy < 229 ) {
return d + ( c - d ) / 46.0 * ( dyy - 183 );
} else if ( dyy < 275 ) {
return c + ( b - c ) / 46.0 * ( dyy - 229 );
} else {
return b + ( a - b ) / 91.0 * ( dyy - 275 );
}
})();
return dateByAddingSeconds(sunset, Math.round(adjustment * 60.0));
},
daysSinceSolstice: function(dayOfYear, year, latitude) {
var daysSinceSolstice = 0;
var northernOffset = 10;
var southernOffset = Astronomical.isLeapYear(year) ? 173 : 172;
var daysInYear = Astronomical.isLeapYear(year) ? 366 : 365;
if (latitude >= 0) {
daysSinceSolstice = dayOfYear + northernOffset;
if (daysSinceSolstice >= daysInYear) {
daysSinceSolstice = daysSinceSolstice - daysInYear;
}
} else {
daysSinceSolstice = dayOfYear - southernOffset;
if (daysSinceSolstice < 0) {
daysSinceSolstice = daysSinceSolstice + daysInYear;
}
}
return daysSinceSolstice;
}
};
//
// Math convenience extensions
//
function timeComponents(number) {
return new TimeComponents(number);
}
function TimeComponents(number) {
this.hours = Math.floor(number);
this.minutes = Math.floor((number - this.hours) * 60);
this.seconds = Math.floor((number - (this.hours + this.minutes/60)) * 60 * 60);
return this;
}
TimeComponents.prototype.UTCDate = function (year, month, date) {
return new Date(Date.UTC(year, month, date, this.hours, this.minutes, this.seconds));
};
function degreesToRadians(degrees) {
return (degrees * Math.PI) / 180.0;
}
function radiansToDegrees(radians) {
return (radians * 180.0) / Math.PI;
}
function normalizeWithBound(number, max) {
return number - (max * (Math.floor(number / max)))
}
function unwindAngle(angle) {
return normalizeWithBound(angle, 360.0);
}
function closestAngle(angle) {
if (angle >= -180 && angle <= 180) {
return angle;
}
return angle - (360 * Math.round(angle/360));
}
function formattedTime(date, UTCOffset, style) {
var offset = dateByAddingHours(date, UTCOffset);
var hours, minutes;
if (style == '24h') {
hours = offset.getUTCHours().toString();
if (hours.length < 2) {
hours = '0' + hours;
}
minutes = offset.getUTCMinutes().toString();
if (minutes.length < 2) {
minutes = '0' + minutes;
}
return hours + ':' + minutes;
} else {
hours = offset.getUTCHours() > 12 ? (offset.getUTCHours() - 12).toString() : offset.getUTCHours().toString();
minutes = offset.getUTCMinutes().toString();
if (minutes.length < 2) {
minutes = '0' + minutes;
}
var ampm = offset.getUTCHours() >= 12 ? 'PM' : 'AM';
return hours + ':' + minutes + ' ' + ampm;
}
}
function dateByAddingDays(date, days) {
var year = date.getUTCFullYear();
var month = date.getUTCMonth();
var day = date.getUTCDate() + days;
var hours = date.getUTCHours();
var minutes = date.getUTCMinutes();
var seconds = date.getUTCSeconds();
return new Date(Date.UTC(year, month, day, hours, minutes, seconds));
}
function dateByAddingHours(date, hours) {
return dateByAddingMinutes(date, hours * 60);
}
function dateByAddingMinutes(date, minutes) {
return dateByAddingSeconds(date, minutes * 60);
}
function dateByAddingSeconds(date, seconds) {
return new Date(date.getTime() + (seconds * 1000));
}
function roundedMinute(date) {
var seconds = date.getUTCSeconds();
var offset = seconds >= 30 ? 60 - seconds : -1 * seconds;
return dateByAddingSeconds(date, offset);
}
function dayOfYear(date) {
var returnedDayOfYear = 0;
var feb = Astronomical.isLeapYear(date.getFullYear()) ? 29 : 28;
var months = [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
for (var i = 0; i < date.getMonth(); i++) {
returnedDayOfYear += months[i];
}
returnedDayOfYear += date.getDate();
return returnedDayOfYear;
}
function julianDate(date) {
return Astronomical.julianDay(date.getFullYear(), date.getMonth() + 1, date.getDate(), date.getHours() + (date.getMinutes() / 60));
}
//
// Polyfill
//
var trunc = Math.trunc || function (x) { return x < 0 ? Math.ceil(x) : Math.floor(x); };
var adhan = {
Prayer: Prayer,
Madhab: Madhab,
HighLatitudeRule: HighLatitudeRule,
Coordinates: Coordinates,
CalculationParameters: CalculationParameters,
CalculationMethod: CalculationMethod,
PrayerTimes: PrayerTimes,
SolarTime: SolarTime,
SolarCoordinates: SolarCoordinates,
Astronomical: Astronomical,
Math: {
degreesToRadians: degreesToRadians,
radiansToDegrees: radiansToDegrees,
normalizeWithBound: normalizeWithBound,
unwindAngle: unwindAngle,
closestAngle: closestAngle,
timeComponents: timeComponents
},
Date: {
formattedTime: formattedTime,
dateByAddingDays: dateByAddingDays,
dateByAddingHours: dateByAddingHours,
dateByAddingMinutes: dateByAddingMinutes,
dateByAddingSeconds: dateByAddingSeconds,
roundedMinute: roundedMinute,
dayOfYear: dayOfYear,
julianDate: julianDate
}
};
return adhan;
}));