From f88e3ee7d12ff8605e4e88af8393509eb25141fd Mon Sep 17 00:00:00 2001 From: Farhan Ahmed Date: Wed, 17 Aug 2022 21:17:25 -0500 Subject: [PATCH] Updating version to `0.7.0` => Adhan v1.4.0 - 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 --- Cargo.toml | 2 +- README.md | 68 +++++++++++++++++++---------- src/astronomy/mod.rs | 2 +- src/astronomy/ops.rs | 46 +++++++++++++++----- src/astronomy/qiblah.rs | 2 +- src/astronomy/solar.rs | 2 +- src/astronomy/unit.rs | 75 ++++++++++++++++++++------------ src/lib.rs | 15 +------ src/models/adjustments.rs | 2 +- src/models/high_altitude_rule.rs | 59 ++++++++++++++++++++++++- src/models/madhab.rs | 6 +-- src/models/method.rs | 67 +++++++++++++++++++++++----- src/models/mod.rs | 4 +- src/models/parameters.rs | 34 ++++++++++++++- src/models/prayer.rs | 2 +- src/models/rounding.rs | 12 +++++ src/models/shafaq.rs | 22 ++++++++++ src/schedule.rs | 28 +++++++----- 18 files changed, 337 insertions(+), 111 deletions(-) create mode 100644 src/models/rounding.rs create mode 100644 src/models/shafaq.rs diff --git a/Cargo.toml b/Cargo.toml index a9b6abd..a58dd1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "salah" -version = "0.5.0" +version = "0.7.0" authors = ["Farhan Ahmed "] edition = "2018" description = "Islamic prayer time library for Rust" diff --git a/README.md b/README.md index 75e6b1f..4bed20b 100644 --- a/README.md +++ b/README.md @@ -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. @@ -64,14 +64,16 @@ 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** @@ -79,17 +81,19 @@ 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** @@ -97,7 +101,7 @@ Setting for the Asr prayer time. For Hanafi madhab, the Asr is bit later than th | Value | Description | | ----- | ----------- | -| `Shafi` | Earlier Asr time | +| `Shafi` | Earlier Asr time (use for Shafi, Maliki, Hanbali, and Jafari) | | `Hanafi` | Later Asr time | **HighLatitudeRule** @@ -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 diff --git a/src/astronomy/mod.rs b/src/astronomy/mod.rs index 10f4b65..2b927db 100644 --- a/src/astronomy/mod.rs +++ b/src/astronomy/mod.rs @@ -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; diff --git a/src/astronomy/ops.rs b/src/astronomy/ops.rs index 909996d..866cbd2 100644 --- a/src/astronomy/ops.rs +++ b/src/astronomy/ops.rs @@ -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 { @@ -323,7 +325,8 @@ pub fn season_adjusted_morning_twilight( sunrise: DateTime, ) -> DateTime { 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 @@ -331,8 +334,13 @@ pub fn season_adjusted_morning_twilight( .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 @@ -366,6 +374,7 @@ struct TwilightAdjustmentValues { fn twilight_adjustment_values( daytime: AdjustmentDaytime, latitude: f64, + shafaq: Shafaq, ) -> TwilightAdjustmentValues { if daytime == AdjustmentDaytime::Morning { TwilightAdjustmentValues { @@ -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()), + }, } } } @@ -391,16 +414,17 @@ pub fn season_adjusted_evening_twilight( day: u32, year: u32, sunset: DateTime, + shafaq: Shafaq, ) -> DateTime { 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. diff --git a/src/astronomy/qiblah.rs b/src/astronomy/qiblah.rs index d3dcf0f..f3855e2 100644 --- a/src/astronomy/qiblah.rs +++ b/src/astronomy/qiblah.rs @@ -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}; diff --git a/src/astronomy/solar.rs b/src/astronomy/solar.rs index 5d9310e..f95ec36 100644 --- a/src/astronomy/solar.rs +++ b/src/astronomy/solar.rs @@ -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}; diff --git a/src/astronomy/unit.rs b/src/astronomy/unit.rs index fe10ba2..bcd93bf 100644 --- a/src/astronomy/unit.rs +++ b/src/astronomy/unit.rs @@ -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 { @@ -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 Stride for DateTime { @@ -51,16 +52,29 @@ impl Stride for DateTime { ) } - 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(); @@ -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)); + } + } diff --git a/src/lib.rs b/src/lib.rs index 4c96140..91dbf18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. @@ -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)) diff --git a/src/models/adjustments.rs b/src/models/adjustments.rs index a799a6a..4edcba7 100644 --- a/src/models/adjustments.rs +++ b/src/models/adjustments.rs @@ -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; diff --git a/src/models/high_altitude_rule.rs b/src/models/high_altitude_rule.rs index 66540c2..227d803 100644 --- a/src/models/high_altitude_rule.rs +++ b/src/models/high_altitude_rule.rs @@ -1,13 +1,70 @@ // 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::Coordinates; + /// Rule for approximating Fajr and Isha at high latitudes #[derive(PartialEq, Debug, Copy, Clone)] pub enum HighLatitudeRule { + /// 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. MiddleOfTheNight, + + /// 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. SeventhOfTheNight, + + /// 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. TwilightAngle, } + +impl HighLatitudeRule { + pub fn recommended(coordinates: Coordinates) -> HighLatitudeRule { + if coordinates.latitude > 48.0 { + HighLatitudeRule::SeventhOfTheNight + } else { + HighLatitudeRule::MiddleOfTheNight + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn recommended_rule_seventh_of_night() { + let location = Coordinates { + latitude: 48.983226, + longitude: -3.216649, + }; + + assert_eq!( + HighLatitudeRule::recommended(location), + HighLatitudeRule::SeventhOfTheNight + ); + } + + #[test] + fn recommended_rule_middle_of_night() { + let location = Coordinates { + latitude: 45.983226, + longitude: -3.216649, + }; + + assert_eq!( + HighLatitudeRule::recommended(location), + HighLatitudeRule::MiddleOfTheNight + ); + } +} diff --git a/src/models/madhab.rs b/src/models/madhab.rs index 1c3993f..36aa6b6 100644 --- a/src/models/madhab.rs +++ b/src/models/madhab.rs @@ -1,11 +1,11 @@ // Salah // // See LICENSE for more details. -// Copyright (c) 2019-2021 Farhan Ahmed. All rights reserved. +// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved. // -/// Setting for the Asr prayer time. -/// For Hanafi madhab, the Asr is bit later +/// Setting for the Asr prayer time. +/// For Hanafi madhab, the Asr is bit later /// than that of the Shafi madhab. #[derive(PartialEq, Debug, Copy, Clone)] pub enum Madhab { diff --git a/src/models/method.rs b/src/models/method.rs index 7b1ada8..3ba427d 100644 --- a/src/models/method.rs +++ b/src/models/method.rs @@ -1,47 +1,72 @@ // Salah // // See README.md and LICENSE for more details. -// Copyright (c) 2019-2021 Farhan Ahmed. All rights reserved. +// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved. // use super::adjustments::Adjustment; use super::parameters::{Configuration, Parameters}; +use super::rounding::Rounding; /// Provides preset configuration for a few authorities /// for calculating prayer times. #[derive(PartialEq, Debug, Copy, Clone)] pub enum Method { - // Muslim World League + /// Muslim World League. Standard Fajr time with an angle of 18°. + /// Earlier Isha time with an angle of 17°. MuslimWorldLeague, - //Egyptian General Authority of Survey + /// 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°. Egyptian, - // University of Islamic Sciences, Karachi + /// University of Islamic Sciences, Karachi. A generally applicable method that + /// uses standard Fajr and Isha angles of 18°. Karachi, - // Umm al-Qura University, Makkah + /// 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. UmmAlQura, - // The Gulf Region + /// 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. Dubai, - // Moonsighting Committee + /// 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. MoonsightingCommittee, - // ISNA + /// 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°. NorthAmerica, - // Kuwait + /// Standard Fajr time with an angle of 18°. Slightly earlier Isha time with an angle of 17.5°. Kuwait, - // Qatar + /// Same Isha interval as `ummAlQura` but with the standard Fajr time using an angle of 18°. Qatar, - // Singapore + /// Used in Singapore, Malaysia, and Indonesia. Early Fajr time with an angle of 20° + /// and standard Isha time with an angle of 18°. Singapore, - // Other + /// 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. + Tehran, + + /// An approximation of the Diyanet method used in Turkey. + /// This approximation is less accurate outside the region of Turkey. + Turkey, + + /// Defaults to angles of 0°, should generally be used for making a custom method + /// and setting your own values. Other, } @@ -99,6 +124,24 @@ impl Method { Method::Singapore => Configuration::new(20.0, 18.0) .method(*self) .method_adjustments(Adjustment::new().dhuhr(1).done()) + .rounding(Rounding::Up) + .done(), + + Method::Tehran => Configuration::new(17.7, 14.0) + .method(*self) + .maghrib_angle(4.5) + .done(), + + Method::Turkey => Configuration::new(18.0, 17.0) + .method(*self) + .method_adjustments( + Adjustment::new() + .sunrise(-7) + .dhuhr(5) + .asr(4) + .maghrib(7) + .done(), + ) .done(), Method::Other => Configuration::new(0.0, 0.0).method(*self).done(), diff --git a/src/models/mod.rs b/src/models/mod.rs index fb63815..c1bd443 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -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 adjustments; @@ -10,3 +10,5 @@ pub mod madhab; pub mod method; pub mod parameters; pub mod prayer; +pub mod rounding; +pub mod shafaq; diff --git a/src/models/parameters.rs b/src/models/parameters.rs index 9ddcb2b..e7dbc97 100644 --- a/src/models/parameters.rs +++ b/src/models/parameters.rs @@ -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 super::adjustments::TimeAdjustment; @@ -9,6 +9,8 @@ use super::high_altitude_rule::HighLatitudeRule; use super::madhab::Madhab; use super::method::Method; use super::prayer::Prayer; +use super::rounding::Rounding; +use super::shafaq::Shafaq; /// Settings that are used for determining the /// the correct prayer time. @@ -19,18 +21,22 @@ use super::prayer::Prayer; pub struct Parameters { pub method: Method, pub fajr_angle: f64, + pub maghrib_angle: f64, pub isha_angle: f64, pub isha_interval: i32, pub madhab: Madhab, pub high_latitude_rule: HighLatitudeRule, pub adjustments: TimeAdjustment, pub method_adjustments: TimeAdjustment, + pub rounding: Rounding, + pub shafaq: Shafaq, } impl Parameters { pub fn new(fajr_angle: f64, isha_angle: f64) -> Parameters { Parameters { fajr_angle: fajr_angle, + maghrib_angle: 0.0, isha_angle: isha_angle, method: Method::Other, isha_interval: 0, @@ -38,6 +44,8 @@ impl Parameters { high_latitude_rule: HighLatitudeRule::MiddleOfTheNight, adjustments: TimeAdjustment::default(), method_adjustments: TimeAdjustment::default(), + rounding: Rounding::Nearest, + shafaq: Shafaq::General, } } @@ -69,18 +77,22 @@ impl Parameters { pub struct Configuration { method: Method, fajr_angle: f64, + maghrib_angle: f64, isha_angle: f64, isha_interval: i32, madhab: Madhab, high_latitude_rule: HighLatitudeRule, adjustments: TimeAdjustment, method_adjustments: TimeAdjustment, + rounding: Rounding, + shafaq: Shafaq, } impl Configuration { pub fn new(fajr_angle: f64, isha_angle: f64) -> Configuration { Configuration { fajr_angle: fajr_angle, + maghrib_angle: 0.0, isha_angle: isha_angle, method: Method::Other, isha_interval: 0, @@ -88,6 +100,8 @@ impl Configuration { high_latitude_rule: HighLatitudeRule::MiddleOfTheNight, adjustments: TimeAdjustment::default(), method_adjustments: TimeAdjustment::default(), + rounding: Rounding::Nearest, + shafaq: Shafaq::General, } } @@ -130,9 +144,25 @@ impl Configuration { self } + pub fn maghrib_angle<'a>(&'a mut self, angle: f64) -> &'a mut Configuration { + self.maghrib_angle = angle; + self + } + + pub fn rounding<'a>(&'a mut self, value: Rounding) -> &'a mut Configuration { + self.rounding = value; + self + } + + pub fn shafaq<'a>(&'a mut self, value: Shafaq) -> &'a mut Configuration { + self.shafaq = value; + self + } + pub fn done(&self) -> Parameters { Parameters { fajr_angle: self.fajr_angle, + maghrib_angle: self.maghrib_angle, isha_angle: self.isha_angle, method: self.method, isha_interval: self.isha_interval, @@ -140,6 +170,8 @@ impl Configuration { high_latitude_rule: self.high_latitude_rule, adjustments: self.adjustments, method_adjustments: self.method_adjustments, + rounding: self.rounding, + shafaq: self.shafaq, } } } diff --git a/src/models/prayer.rs b/src/models/prayer.rs index 9b6a7ab..b1dda00 100644 --- a/src/models/prayer.rs +++ b/src/models/prayer.rs @@ -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::{Datelike, Utc, Weekday}; diff --git a/src/models/rounding.rs b/src/models/rounding.rs new file mode 100644 index 0000000..a538fc5 --- /dev/null +++ b/src/models/rounding.rs @@ -0,0 +1,12 @@ +// Salah +// +// See LICENSE for more details. +// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved. +// + +#[derive(PartialEq, Debug, Copy, Clone)] +pub enum Rounding { + Nearest, + Up, + None, +} diff --git a/src/models/shafaq.rs b/src/models/shafaq.rs new file mode 100644 index 0000000..fa19de3 --- /dev/null +++ b/src/models/shafaq.rs @@ -0,0 +1,22 @@ +// Salah +// +// See LICENSE for more details. +// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved. +// + +/// Shafaq is the twilight in the sky. Different madhabs define the appearance of +/// twilight differently. These values are used by the MoonsightingComittee method +/// for the different ways to calculate Isha. +#[derive(PartialEq, Debug, Copy, Clone)] +pub enum Shafaq { + /// General is a combination of Ahmer and Abyad. + General, + + /// Ahmer means the twilight is the red glow in the sky. + /// Used by the Shafi, Maliki, and Hanbali madhabs. + Ahmer, + + /// Abyad means the twilight is the white glow in the sky. + /// Used by the Hanafi madhab. + Abyad, +} diff --git a/src/schedule.rs b/src/schedule.rs index d3db09f..ba006d9 100644 --- a/src/schedule.rs +++ b/src/schedule.rs @@ -1,12 +1,12 @@ // Salah // // See LICENSE for more details. -// Copyright (c) 2019-2021 Farhan Ahmed. All rights reserved. +// Copyright (c) 2019-2022 Farhan Ahmed. All rights reserved. // //! # Prayer Schedule //! -//! This module provide the main objects that are used for calculating +//! This module provides the main objects that are used for calculating //! the prayer times. use chrono::{Date, DateTime, Datelike, Duration, Utc}; @@ -17,6 +17,7 @@ use crate::astronomy::unit::{Angle, Coordinates, Stride}; use crate::models::method::Method; use crate::models::parameters::Parameters; use crate::models::prayer::Prayer; +use crate::models::rounding::Rounding; /// A data struct to hold the timing for all /// prayers. @@ -49,20 +50,24 @@ impl PrayerTimes { .signed_duration_since(solar_time.sunset); let final_fajr = - PrayerTimes::calculate_fajr(parameters, solar_time, night, coordinates, prayer_date); + PrayerTimes::calculate_fajr(parameters, solar_time, night, coordinates, prayer_date) + .rounded_minute(parameters.rounding); let final_sunrise = solar_time .sunrise - .adjust_time(parameters.time_adjustments(Prayer::Sunrise)); + .adjust_time(parameters.time_adjustments(Prayer::Sunrise)) + .rounded_minute(parameters.rounding); let final_dhuhr = solar_time .transit - .adjust_time(parameters.time_adjustments(Prayer::Dhuhr)); - let final_asr = asr.adjust_time(parameters.time_adjustments(Prayer::Asr)); + .adjust_time(parameters.time_adjustments(Prayer::Dhuhr)) + .rounded_minute(parameters.rounding); + let final_asr = asr.adjust_time(parameters.time_adjustments(Prayer::Asr)) + .rounded_minute(parameters.rounding); let final_maghrib = ops::adjust_time( &solar_time.sunset, - parameters.time_adjustments(Prayer::Maghrib), - ); + parameters.time_adjustments(Prayer::Maghrib)).rounded_minute(parameters.rounding); let final_isha = - PrayerTimes::calculate_isha(parameters, solar_time, night, coordinates, prayer_date); + PrayerTimes::calculate_isha(parameters, solar_time, night, coordinates, prayer_date) + .rounded_minute(parameters.rounding); // Calculate the middle of the night and qiyam times let (final_middle_of_night, final_qiyam, final_fajr_tomorrow) = @@ -241,6 +246,7 @@ impl PrayerTimes { day_of_year, prayer_date.year() as u32, solar_time.sunset, + parameters.shafaq, ) } else { let portion = parameters.night_portions().1; @@ -285,11 +291,11 @@ impl PrayerTimes { let middle_of_night = current_maghrib .checked_add_signed(Duration::seconds(middle_night_portion)) .unwrap() - .nearest_minute(); + .rounded_minute(Rounding::Nearest); let last_third_of_night = current_maghrib .checked_add_signed(Duration::seconds(last_third_portion)) .unwrap() - .nearest_minute(); + .rounded_minute(Rounding::Nearest); (middle_of_night, last_third_of_night, tomorrow_fajr) }