Skip to content

Commit

Permalink
Updating version to 0.7.0 => Adhan v1.4.0
Browse files Browse the repository at this point in the history
- Adding `Shafaq` enum
- Adding `Rounding` enum; allows for customization on how the final prayer times are rounded using, `Nearest`, `Up`, or `None` variants
- Adding Maghrib angle to the `Parameter` struct
- Adding a proptery called `rounding` to the `Parameter` struct to use the `Rounding` enum
- Adding a property called `shafaq` to the `Parameter` struct, using the `Shafaq` enum
- Replaced `nearest_minute` method with `rounded_minute`; This method takes a parameter of type `Rounding`
- Updating method signature of `season_adjusted_evening_twilight(latitude:day:year:sunset:shafaq:)` with the the `Shafaq` enum
- Adding doc comments
- Using the `rounding` method for Singapore `Method`
- Adding `Turkey` to the `Method` enum
- Adding `Tehran` to the `Method` enum; this method uses the newly added `maghrib_angle` property of the `Parameter` struct.
- Adding `recomended` method; based on the latitude of the provided location a variant of the `HighLatitudeRule` is returned
- Adding and updating tests
- Code formatting
- Updating copyright
- Documenting changes in the `readme.md` file
  • Loading branch information
insha committed Aug 18, 2022
1 parent d25ffe7 commit f88e3ee
Show file tree
Hide file tree
Showing 18 changed files with 337 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "salah"
version = "0.5.0"
version = "0.7.0"
authors = ["Farhan Ahmed <[email protected]>"]
edition = "2018"
description = "Islamic prayer time library for Rust"
Expand Down
68 changes: 44 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Add the following to your `Cargo.toml` file under the `[dependencies]` section:

```
[dependencies]
salah = "0.5.0"
salah = "0.7.0"
```

To get prayer times, use the `PrayerSchedule` struct passing in coordinates, date, and calculation parameters.
Expand Down Expand Up @@ -64,40 +64,44 @@ let params = Configuration::with(Method::NorthAmerica, Madhab::Hanafi);

| Parameter | Description |
| --------- | ----------- |
| `method` | Member of `Method` enum |
| `fajr_angle` | Angle of the sun used to calculate Fajr |
| `isha_angle` | Angle of the sun used to calculate Isha |
| `isha_interval` | Minutes after Maghrib (if set, the time for Isha will be Maghrib plus `isha_interval`) |
| `madhab` | Member of the `Madhab` enum, used to calculate Asr |
| `high_latitude_rule` | Member of the `HighLatitudeRule` enum, used to set a minimum time for Fajr and a max time for Isha |
| `time_adjustments` | `TimeAdjustment` struct with adjustment based on the selected `Method`. |
| `custom_time_adjustments` | `TimeAdjustment` struct with custom prayer time adjustments in minutes for each prayer time. By default, all values are `0`.|
| `method` | Which preset from the CalculationMethod enum was used. Default value is `other`. |
| `fajr_angle` | Angle of the sun below the horizon used to calculate Fajr. |
| `maghrib_angle` | Angle of the sun below the horizon used to calculate Maghrib, used for some Calculation Methods. |
| `isha_angle` | Angle of the sun below the horizon used to calculate Isha. |
| `isha_interval` | Minutes after Maghrib (if set, the time for Isha will be Maghrib plus `isha_interval`). |
| `madhab` | Which setting from the Madhab enum to use for calculating Asr. |
| `high_latitude_rule` | Which setting from the HighLatitudeRule enum to use for calculating the minimum time for Fajr and the maximum time for Isha. |
| `adjustments` | PrayerAdjustments struct with custom prayer time adjustments in minutes for each prayer time. |
| `rounding` | The behavior for rounding prayer times. Either to nearest minute, to the higher minute, or none. |
| `shafaq` | Used by the MoonsightingCommittee method to determine how to calculate Isha. See explanation of values below. |

**Method**

Provides preset configuration for a few authorities for calculating prayer times.

| Value | Description |
| ----- | ----------- |
| `MuslimWorldLeague` | Muslim World League. Fajr angle: 18, Isha angle: 17 |
| `Egyptian` | Egyptian General Authority of Survey. Fajr angle: 19.5, Isha angle: 17.5 |
| `Karachi` | University of Islamic Sciences, Karachi. Fajr angle: 18, Isha angle: 18 |
| `UmmAlQura` | Umm al-Qura University, Makkah. Fajr angle: 18.5, Isha interval: 90. *Note: you should add a +30 minute custom adjustment for Isha during Ramadan.* |
| `Dubai` | Method used in UAE. Fajr angle: 18.2, Isha angle: 18.2. |
| `Qatar` | Modified version of Umm al-Qura used in Qatar. Fajr angle: 18, Isha interval: 90. |
| `Kuwait` | Method used by the country of Kuwait. Fajr angle: 18, Isha angle: 17.5 |
| `MoonsightingCommittee` | Moonsighting Committee. Fajr angle: 18, Isha angle: 18. Also uses seasonal adjustment values. |
| `Singapore` | Method used by Singapore. Fajr angle: 20, Isha angle: 18. |
| `NorthAmerica` | Referred to as the ISNA method. Fajr angle: 15, Isha angle: 15 |
| `Other` | Fajr angle: 0, Isha angle: 0. This is the default value for when manually initializing the `Parameters` struct. |
| `MuslimWorldLeague` | Muslim World League. Standard Fajr time with an angle of 18°. Earlier Isha time with an angle of 17°. |
| `Egyptian` | Egyptian General Authority of Survey. Early Fajr time using an angle 19.5° and a slightly earlier Isha time using an angle of 17.5°. |
| `Karachi` | University of Islamic Sciences, Karachi. A generally applicable method that uses standard Fajr and Isha angles of 18°. |
| `UmmAlQura` | Umm al-Qura University, Makkah. Uses a fixed interval of 90 minutes from maghrib to calculate Isha. And a slightly earlier Fajr time with an angle of 18.5°. *Note: you should add a +30 minute custom adjustment for Isha during Ramadan.* |
| `Dubai` | Used in the UAE. Slightly earlier Fajr time and slightly later Isha time with angles of 18.2° for Fajr and Isha in addition to 3 minute offsets for sunrise, Dhuhr, Asr, and Maghrib. |
| `Qatar` | Same Isha interval as `ummAlQura` but with the standard Fajr time using an angle of 18°. |
| `Kuwait` | Standard Fajr time with an angle of 18°. Slightly earlier Isha time with an angle of 17.5°. |
| `MoonsightingCommittee` | Method developed by Khalid Shaukat, founder of Moonsighting Committee Worldwide. Uses standard 18° angles for Fajr and Isha in addition to seasonal adjustment values. This method automatically applies the 1/7 approximation rule for locations above 55° latitude. Recommended for North America and the UK. |
| `Singapore` | Used in Singapore, Malaysia, and Indonesia. Early Fajr time with an angle of 20° and standard Isha time with an angle of 18°. |
| `Turkey` | An approximation of the Diyanet method used in Turkey. This approximation is less accurate outside the region of Turkey. |
| `Tehran` | Institute of Geophysics, University of Tehran. Early Isha time with an angle of 14°. Slightly later Fajr time with an angle of 17.7°. Calculates Maghrib based on the sun reaching an angle of 4.5° below the horizon. |
| `NorthAmerica` | Also known as the ISNA method. Can be used for North America, but the moonsightingCommittee method is preferable. Gives later Fajr times and early Isha times with angles of 15°. |
| `Other` | Defaults to angles of 0°, should generally be used for making a custom method and setting your own values. |

**Madhab**

Setting for the Asr prayer time. For Hanafi madhab, the Asr is bit later than that of the Shafi madhab.

| Value | Description |
| ----- | ----------- |
| `Shafi` | Earlier Asr time |
| `Shafi` | Earlier Asr time (use for Shafi, Maliki, Hanbali, and Jafari) |
| `Hanafi` | Later Asr time |

**HighLatitudeRule**
Expand All @@ -106,10 +110,26 @@ Rule for approximating Fajr and Isha at high latitudes.

| Value | Description |
| ----- | ----------- |
| `MiddleOfTheNight` | Fajr will never be earlier than the middle of the night and Isha will never be later than the middle of the night |
| `SeventhOfTheNight` | Fajr will never be earlier than the beginning of the last seventh of the night and Isha will never be later than the end of the first seventh of the night |
| `TwilightAngle` | Similar to SeventhOfTheNight, but instead of 1/7, the fraction of the night used is fajr_angle/60 and isha_angle/60 |
| `MiddleOfTheNight` | Fajr won't be earlier than the midpoint of the night and isha won't be later than the midpoint of the night. This is the default value to prevent fajr and isha crossing boundaries. |
| `SeventhOfTheNight` | Fajr will never be earlier than the beginning of the last seventh of the night and Isha will never be later than the end of the first seventh of the night. This is recommended to use for locations above 48° latitude to prevent prayer times that would be difficult to perform. |
| `TwilightAngle` | The night is divided into portions of roughly 1/3. The exact value is derived by dividing the fajr/isha angles by 60. This can be used to prevent difficult fajr and isha times at certain locations. |

You can get the recommended High Latitude Rule for a location by calling the `recommended(coordinates:)` function and passing in the coordinates for the location.

```rust
let myCoordinates = Coordinates { latitude: 48.983226, longitude: -3.216649 };
let highLatRule = HighLatitudeRule::recommended(myCoordinates);
```

**Shafaq**

Shafaq is used by the MoonsightingCommittee method to determine what type of twilight to use in order to determine the time for Isha.

| Value | Description |
| ----- | ----------- |
| `General` | General is a combination of Ahmer and Abyad. This is the defualt value and will provide more reasonable times for locations at higher latitudes. |
| `Ahmer` | Ahmer means the twilight is the red glow in the sky. Used by the Shafi, Maliki, and Hanbali madhabs. This generally produces an earlier Isha time. |
| `Abyad` | Abyad means the twilight is the white glow in the sky. Used by the Hanafi madhab. This generally produces a later Isha time. |

### Prayer Schedule

Expand Down
2 changes: 1 addition & 1 deletion src/astronomy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Salah
//
// See LICENSE for more details.
// Copyright (c) 2019-2021 Farhan Ahmed. All rights reserved.
// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved.
//

pub mod ops;
Expand Down
46 changes: 35 additions & 11 deletions src/astronomy/ops.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Salah
//
// See LICENSE for more details.
// Copyright (c) 2019-2021 Farhan Ahmed. All rights reserved.
// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved.
//

use chrono::{DateTime, Duration, Utc};

use crate::astronomy::unit::{Angle, Coordinates};
use crate::astronomy::unit::{Normalize, Stride};
use crate::models::shafaq::Shafaq;
use crate::models::rounding::Rounding;

// The geometric mean longitude of the sun.
pub fn mean_solar_longitude(julian_century: f64) -> Angle {
Expand Down Expand Up @@ -323,16 +325,22 @@ pub fn season_adjusted_morning_twilight(
sunrise: DateTime<Utc>,
) -> DateTime<Utc> {
let dyy = days_since_solstice(day, year, latitude) as f64;
let adjustment = twilight_adjustments(AdjustmentDaytime::Morning, latitude, dyy);
let adjustment =
twilight_adjustments(AdjustmentDaytime::Morning, latitude, dyy, Shafaq::General);

let rounded_adjustment = (adjustment * -60.0).round() as i64;
sunrise
.checked_add_signed(Duration::seconds(rounded_adjustment))
.unwrap()
}

fn twilight_adjustments(daytime: AdjustmentDaytime, latitude: f64, dyy: f64) -> f64 {
let adjustment_values = twilight_adjustment_values(daytime, latitude);
fn twilight_adjustments(
daytime: AdjustmentDaytime,
latitude: f64,
dyy: f64,
shafaq: Shafaq,
) -> f64 {
let adjustment_values = twilight_adjustment_values(daytime, latitude, shafaq);

if (0.00..=90.0).contains(&dyy) {
adjustment_values.a + (adjustment_values.b - adjustment_values.a) / 91.0 * dyy
Expand Down Expand Up @@ -366,6 +374,7 @@ struct TwilightAdjustmentValues {
fn twilight_adjustment_values(
daytime: AdjustmentDaytime,
latitude: f64,
shafaq: Shafaq,
) -> TwilightAdjustmentValues {
if daytime == AdjustmentDaytime::Morning {
TwilightAdjustmentValues {
Expand All @@ -375,11 +384,25 @@ fn twilight_adjustment_values(
d: 75.0 + ((48.10 / 55.0) * latitude.abs()),
}
} else {
TwilightAdjustmentValues {
a: 75.0 + ((25.60 / 55.0) * latitude.abs()),
b: 75.0 + ((2.050 / 55.0) * latitude.abs()),
c: 75.0 - ((9.210 / 55.0) * latitude.abs()),
d: 75.0 + ((6.140 / 55.0) * latitude.abs()),
match shafaq {
Shafaq::General => TwilightAdjustmentValues {
a: 75.0 + ((25.60 / 55.0) * latitude.abs()),
b: 75.0 + ((2.050 / 55.0) * latitude.abs()),
c: 75.0 - ((9.210 / 55.0) * latitude.abs()),
d: 75.0 + ((6.140 / 55.0) * latitude.abs()),
},
Shafaq::Ahmer => TwilightAdjustmentValues {
a: 62.0 + ((17.40 / 55.0) * latitude.abs()),
b: 62.0 - ((7.160 / 55.0) * latitude.abs()),
c: 62.0 + ((5.120 / 55.0) * latitude.abs()),
d: 62.0 + ((19.44 / 55.0) * latitude.abs()),
},
Shafaq::Abyad => TwilightAdjustmentValues {
a: 75.0 + ((25.60 / 55.0) * latitude.abs()),
b: 75.0 + ((7.160 / 55.0) * latitude.abs()),
c: 75.0 + ((36.84 / 55.0) * latitude.abs()),
d: 75.0 + ((81.84 / 55.0) * latitude.abs()),
},
}
}
}
Expand All @@ -391,16 +414,17 @@ pub fn season_adjusted_evening_twilight(
day: u32,
year: u32,
sunset: DateTime<Utc>,
shafaq: Shafaq,
) -> DateTime<Utc> {
let dyy = days_since_solstice(day, year, latitude) as f64;
let adjustment = twilight_adjustments(AdjustmentDaytime::Evening, latitude, dyy);
let adjustment = twilight_adjustments(AdjustmentDaytime::Evening, latitude, dyy, shafaq);

let rounded_adjustment = (adjustment * 60.0).round() as i64;
let adjusted_date = sunset
.checked_add_signed(Duration::seconds(rounded_adjustment))
.unwrap();

adjusted_date.nearest_minute()
adjusted_date.rounded_minute(Rounding::Nearest)
}

// Solstice calculation to determine a date's seasonal progression.
Expand Down
2 changes: 1 addition & 1 deletion src/astronomy/qiblah.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Salah
//
// See LICENSE for more details.
// Copyright (c) 2019-2021 Farhan Ahmed. All rights reserved.
// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved.
//

use crate::astronomy::unit::{Angle, Coordinates};
Expand Down
2 changes: 1 addition & 1 deletion src/astronomy/solar.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Salah
//
// See LICENSE for more details.
// Copyright (c) 2019-2021 Farhan Ahmed. All rights reserved.
// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved.
//

use chrono::{DateTime, Datelike, TimeZone, Utc};
Expand Down
75 changes: 48 additions & 27 deletions src/astronomy/unit.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Salah
//
// See LICENSE for more details.
// Copyright (c) 2019-2021 Farhan Ahmed. All rights reserved.
// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved.
//

use std::f64::consts::PI;
use std::ops::{Add, Div, Mul, Sub};

use crate::astronomy::ops;
use crate::models::rounding::Rounding;
use chrono::{DateTime, Datelike, Duration, TimeZone, Timelike};

pub trait Normalize {
Expand All @@ -25,9 +26,9 @@ pub trait Stride {
fn tomorrow(&self) -> Self;
fn yesterday(&self) -> Self;
fn julian_day(&self) -> f64;
fn nearest_minute(&self) -> Self;
fn adjust_time(&self, minutes: i64) -> Self;
fn next_date(&self, fwd: bool) -> Self;
fn rounded_minute(&self, rounding: Rounding) -> Self;
}

impl<Tz: TimeZone> Stride for DateTime<Tz> {
Expand All @@ -51,16 +52,29 @@ impl<Tz: TimeZone> Stride for DateTime<Tz> {
)
}

fn nearest_minute(&self) -> Self {
let adjusted = self.clone();
let seconds = adjusted.second() as i64;

if adjusted.second() >= 30 {
adjusted + Duration::seconds(60 - seconds)
} else {
adjusted + Duration::seconds(seconds * -1)
}
}
fn rounded_minute(&self, rounding: Rounding) -> Self {
let adjusted = self.clone();
let seconds = adjusted.second();

match rounding {
Rounding::Nearest => {
let rounded = ((seconds as f64)/60.0).round() as i64;
let adjusted_seconds = seconds as i64;

if rounded == 1 {
adjusted + Duration::seconds(60 - adjusted_seconds)
} else {
adjusted + Duration::seconds(adjusted_seconds * -1)
}
},
Rounding::Up => {
let adjusted_seconds = seconds as i64;

adjusted + Duration::seconds(60 - adjusted_seconds)
},
Rounding::None => adjusted,
}
}

fn adjust_time(&self, minutes: i64) -> Self {
let some_date = self.clone();
Expand Down Expand Up @@ -271,21 +285,28 @@ mod tests {
let angle_a = Angle::new(45.0);
let angle_b = Angle::new(45.0);

assert_eq!((angle_a + angle_b).degrees, 90.0)
assert_eq!((angle_a + angle_b).degrees, 90.0);
}

#[test]
fn calculate_nearest_minute() {
#[test]
fn calculate_rounding_nearest() {
let time_1 = Utc.ymd(2015, 7, 13).and_hms(4, 37, 30);
let time_2 = Utc.ymd(2015, 07, 13).and_hms(05, 59, 20);

assert_eq!(
time_1.nearest_minute(),
Utc.ymd(2015, 7, 13).and_hms(4, 38, 00)
);
assert_eq!(
time_2.nearest_minute(),
Utc.ymd(2015, 07, 13).and_hms(05, 59, 00)
);
}

assert_eq!(time_1.rounded_minute(Rounding::Nearest), Utc.ymd(2015, 7, 13).and_hms(4, 38, 00));
}

#[test]
fn calculate_rounding_up() {
let time_1 = Utc.ymd(2015, 07, 13).and_hms(05, 59, 20);

assert_eq!(time_1.rounded_minute(Rounding::Up), Utc.ymd(2015, 7, 13).and_hms(6, 00, 00));
}

#[test]
fn calculate_rounding_none() {
let time_1 = Utc.ymd(2015, 07, 13).and_hms(05, 59, 20);

assert_eq!(time_1.rounded_minute(Rounding::None), Utc.ymd(2015, 7, 13).and_hms(5, 59, 20));
}

}
15 changes: 1 addition & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Salah
//
// See LICENSE for more details.
// Copyright (c) 2019-2021 Farhan Ahmed. All rights reserved.
// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved.
//

//! An Islamic prayer time implementation based on the [Adhan](https://github.com/batoulapps/Adhan) library by Batoul Apps.
Expand Down Expand Up @@ -206,19 +206,6 @@ mod tests {

params.high_latitude_rule = HighLatitudeRule::MiddleOfTheNight;

// The adjustment below are based on the prayer times that are provided
// on the website (http://www.muis.gov.sg). I don't know the exact
// calculation that this site is using for the prayer times; therefore the
// use of time adjustment. However, these are within the 2 minute variance.
params.method_adjustments = Adjustment::new()
.fajr(1)
.sunrise(1)
.dhuhr(2)
.asr(1)
.maghrib(1)
.isha(1)
.done();

let result = PrayerSchedule::new()
.on(Utc.ymd(2021, 1, 13))
.for_location(Coordinates::new(1.370844612058886, 103.80145644060552))
Expand Down
2 changes: 1 addition & 1 deletion src/models/adjustments.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Salah
//
// See LICENSE for more details.
// Copyright (c) 2019-2021 Farhan Ahmed. All rights reserved.
// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved.
//

use std::default::Default;
Expand Down
Loading

0 comments on commit f88e3ee

Please sign in to comment.