-
Notifications
You must be signed in to change notification settings - Fork 7
/
5-high-lat-middle-night.go
36 lines (31 loc) · 1.18 KB
/
5-high-lat-middle-night.go
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
package prayer
import (
"time"
)
// MiddleNight is adapter where the night period is divided into two halves. The
// first half is considered to be the "night" and the other half as "day break".
// Fajr and Isha in this method are assumed to be at mid-night during the abnormal
// periods.
//
// This adapter depends on sunrise and sunset time, so it might not be suitable for
// area in extreme latitudes (>=65 degrees).
//
// Reference: http://praytimes.org/calculation
func MiddleNight() HighLatitudeAdapter {
return highLatMiddleNight
}
func highLatMiddleNight(_ Config, _ int, schedules []Schedule) []Schedule {
for i, s := range schedules {
// Middle night require Sunrise and Maghrib, and only done if Fajr or Isha missing
if !s.Sunrise.IsZero() && !s.Maghrib.IsZero() && (s.Fajr.IsZero() || s.Isha.IsZero()) {
// Calculate night duration
dayDuration := s.Maghrib.Sub(s.Sunrise).Seconds()
nightDuration := float64(24*60*60) - dayDuration
// Calculate Fajr and Isha time
halfDuration := time.Duration(nightDuration * 0.5 * float64(time.Second))
schedules[i].Fajr = s.Sunrise.Add(-halfDuration)
schedules[i].Isha = s.Maghrib.Add(halfDuration)
}
}
return schedules
}