Skip to content

Commit

Permalink
Updating Chrono to version 0.4.38
Browse files Browse the repository at this point in the history
Associated changes where the `Date<T>` struct has been deprecated and thus recommended, by `chrono` lib, to be replaced with `NaiveDate` struct.
  • Loading branch information
insha committed Apr 21, 2024
1 parent 1cd334e commit 2ad1c79
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ license = "MIT"
spectral = "0.6.0"

[dependencies]
chrono = "0.4.6"
chrono = "0.4.38"
67 changes: 43 additions & 24 deletions src/astronomy/solar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ impl SolarTime {
pub fn new(date: DateTime<Utc>, coordinates: Coordinates) -> SolarTime {
// All calculation need to occur at 0h0m UTC
let today = Utc
.ymd(date.year(), date.month(), date.day())
.and_hms(0, 0, 0);
.with_ymd_and_hms(date.year(), date.month(), date.day(), 0, 0, 0)
.single()
.expect("Invalid date received.");
let tomorrow = today.tomorrow();
let yesterday = today.yesterday();
let prev_solar = SolarCoordinates::new(yesterday.julian_day());
Expand Down Expand Up @@ -186,8 +187,6 @@ impl SolarTime {
}

fn setting_hour(value: f64, date: &DateTime<Utc>) -> Option<DateTime<Utc>> {
let mut adjusted_time: Option<DateTime<Utc>> = None;

if value.is_normal() {
let calculated_hours = value.floor();
let calculated_minutes = ((value - calculated_hours) * 60.0).floor();
Expand All @@ -202,19 +201,20 @@ impl SolarTime {
let adjusted_secs: u32 = 0;

let adjusted = Utc
.ymd(
.with_ymd_and_hms(
adjusted_date.year(),
adjusted_date.month(),
adjusted_date.day(),
hour,
mins,
secs,
)
.and_hms(adjusted_hour, adjusted_mins, adjusted_secs);
.single();

adjusted_time = Some(adjusted);
adjusted
} else {
// Nothing to do.
None
}

adjusted_time
}

fn hour_adjustment(calculated_hours: f64, date: &DateTime<Utc>) -> (u32, DateTime<Utc>) {
Expand Down Expand Up @@ -250,17 +250,23 @@ mod tests {
#[test]
fn zero_out_time_for_a_date() {
// Local date below is 2019-01-11T04:41:19Z in UTC
let utc_date = Utc.ymd(2019, 1, 11).and_hms(23, 41, 19);
let utc_date = Utc
.with_ymd_and_hms(2019, 1, 11, 23, 41, 19)
.single()
.expect("Invalid date and time provided");
let updated = Utc
.ymd(utc_date.year(), utc_date.month(), utc_date.day())
.and_hms(0, 0, 0);
.with_ymd_and_hms(utc_date.year(), utc_date.month(), utc_date.day(), 0, 0, 0)
.single();

assert_eq!(updated, Utc.ymd(2019, 1, 11).and_hms(0, 0, 0));
assert_eq!(updated, Utc.with_ymd_and_hms(2019, 1, 11, 0, 0, 0).single());
}

#[test]
fn calculate_date_for_tomorrow() {
let date = Local.ymd(2019, 1, 10).and_hms(0, 0, 0);
let date = Local
.with_ymd_and_hms(2019, 1, 10, 0, 0, 0)
.single()
.expect("Invalid date and time provided");
let tomorrow = date.tomorrow();

assert_eq!(tomorrow.year(), 2019);
Expand All @@ -270,7 +276,10 @@ mod tests {

#[test]
fn calculate_julian_date() {
let local = Local.ymd(1992, 10, 13).and_hms(0, 0, 0);
let local = Local
.with_ymd_and_hms(1992, 10, 13, 0, 0, 0)
.single()
.expect("Invalid data and time provided");
let utc = local.with_timezone(&Utc);
let julian_day = ops::julian_day(1992, 10, 13, 0.0);

Expand All @@ -280,11 +289,14 @@ mod tests {
#[test]
fn calculate_solar_time() {
let coordinates = Coordinates::new(35.0 + 47.0 / 60.0, -78.0 - 39.0 / 60.0);
let date = Utc.ymd(2015, 7, 12).and_hms(0, 0, 0);
let date = Utc
.with_ymd_and_hms(2015, 7, 12, 0, 0, 0)
.single()
.expect("Invalid date and time provided");
let solar = SolarTime::new(date, coordinates);
let transit_date = Utc.ymd(2015, 07, 12).and_hms(17, 20, 0);
let sunrise_date = Utc.ymd(2015, 07, 12).and_hms(10, 08, 0);
let sunset_date = Utc.ymd(2015, 07, 13).and_hms(00, 32, 0);
let transit_date = Utc.with_ymd_and_hms(2015, 07, 12, 17, 20, 0).unwrap();
let sunrise_date = Utc.with_ymd_and_hms(2015, 07, 12, 10, 08, 0).unwrap();
let sunset_date = Utc.with_ymd_and_hms(2015, 07, 13, 00, 32, 0).unwrap();

assert_eq!(solar.transit, transit_date);
assert_eq!(solar.sunrise, sunrise_date);
Expand All @@ -294,7 +306,10 @@ mod tests {
#[test]
fn calculate_time_for_solar_angle() {
let coordinates = Coordinates::new(35.0 + 47.0 / 60.0, -78.0 - 39.0 / 60.0);
let date = Utc.ymd(2015, 7, 12).and_hms(0, 0, 0);
let date = Utc
.with_ymd_and_hms(2015, 7, 12, 0, 0, 0)
.single()
.expect("Invalida date and time provided");
let solar = SolarTime::new(date, coordinates);
let angle = Angle::new(-6.0);
let twilight_start = solar.time_for_solar_angle(angle, false);
Expand All @@ -307,10 +322,14 @@ mod tests {
#[test]
fn calculate_corrected_hour_angle() {
let coordinates = Coordinates::new(35.0 + 47.0 / 60.0, -78.0 - 39.0 / 60.0);
let date = Utc.ymd(2015, 7, 12).and_hms(0, 0, 0);
let date = Utc
.with_ymd_and_hms(2015, 7, 12, 0, 0, 0)
.single()
.expect("Invalid date and time provided");
let today = Utc
.ymd(date.year(), date.month(), date.day())
.and_hms(0, 0, 0);
.with_ymd_and_hms(date.year(), date.month(), date.day(), 0, 0, 0)
.single()
.expect("Invalid date and time provided.");
let tomorrow = today.tomorrow();
let yesterday = today.yesterday();
let prev_solar = SolarCoordinates::new(yesterday.julian_day());
Expand Down
115 changes: 69 additions & 46 deletions src/astronomy/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait Stride {
fn julian_day(&self) -> f64;
fn adjust_time(&self, minutes: i64) -> Self;
fn next_date(&self, fwd: bool) -> Self;
fn rounded_minute(&self, rounding: Rounding) -> Self;
fn rounded_minute(&self, rounding: Rounding) -> Self;
}

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

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 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 @@ -287,26 +287,49 @@ mod tests {

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

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

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));
}


#[test]
fn calculate_rounding_nearest() {
let time_1 = Utc
.with_ymd_and_hms(2015, 7, 13, 4, 37, 30)
.single()
.expect("Invalid date and time.");

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

#[test]
fn calculate_rounding_up() {
let time_1 = Utc
.with_ymd_and_hms(2015, 07, 13, 05, 59, 20)
.single()
.expect("Invalid date and time.");

assert_eq!(
time_1.rounded_minute(Rounding::Up),
Utc.with_ymd_and_hms(2015, 7, 13, 6, 00, 00)
.single()
.unwrap()
);
}

#[test]
fn calculate_rounding_none() {
let time_1 = Utc
.with_ymd_and_hms(2015, 07, 13, 05, 59, 20)
.single()
.expect("Invalid date and time.");

assert_eq!(
time_1.rounded_minute(Rounding::None),
Utc.with_ymd_and_hms(2015, 7, 13, 5, 59, 20)
.single()
.unwrap()
);
}
}
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! use salah::prelude::*;
//!
//! let new_york_city = Coordinates::new(40.7128, -74.0059);
//! let date = Utc.ymd(2019, 1, 25);
//! let date = NaiveDate::from_ymd_opt(2019, 1, 25).expect("Invalid date provided");
//! let params = Configuration::with(Method::NorthAmerica, Madhab::Hanafi);
//! let prayers = PrayerSchedule::new()
//! .on(date)
Expand All @@ -34,7 +34,7 @@ pub use crate::models::method::Method;
pub use crate::models::parameters::{Configuration, Parameters};
pub use crate::models::prayer::Prayer;
pub use crate::schedule::{PrayerSchedule, PrayerTimes};
pub use chrono::{Date, DateTime, Datelike, Duration, Local, TimeZone, Timelike, Utc};
pub use chrono::{DateTime, Datelike, Duration, Local, NaiveDate, TimeZone, Timelike, Utc};

/// A convenience module appropriate for glob imports (`use salah::prelude::*;`).
pub mod prelude {
Expand All @@ -55,7 +55,7 @@ pub mod prelude {
#[doc(no_inline)]
pub use crate::schedule::{PrayerSchedule, PrayerTimes};
#[doc(no_inline)]
pub use chrono::{Date, DateTime, Datelike, Duration, Local, TimeZone, Timelike, Utc};
pub use chrono::{DateTime, Datelike, Duration, Local, NaiveDate, TimeZone, Timelike, Utc};
}

#[cfg(test)]
Expand All @@ -66,7 +66,7 @@ mod tests {

#[test]
fn calculate_prayer_times() {
let local_date = Utc.ymd(2015, 7, 12);
let local_date = NaiveDate::from_ymd_opt(2015, 7, 12).expect("Invalid date provided");
let params = Configuration::with(Method::NorthAmerica, Madhab::Hanafi);
let coordinates = Coordinates::new(35.7750, -78.6336);
let schedule = PrayerTimes::new(local_date, coordinates, params);
Expand Down Expand Up @@ -105,7 +105,7 @@ mod tests {

#[test]
fn calculate_times_using_the_builder_successfully() {
let date = Utc.ymd(2015, 7, 12);
let date = NaiveDate::from_ymd_opt(2015, 7, 12).expect("Invalid date provided");
let params = Configuration::with(Method::NorthAmerica, Madhab::Hanafi);
let coordinates = Coordinates::new(35.7750, -78.6336);
let result = PrayerSchedule::new()
Expand Down Expand Up @@ -154,7 +154,7 @@ mod tests {

#[test]
fn calculate_times_using_the_builder_failure() {
let date = Utc.ymd(2015, 7, 12);
let date = NaiveDate::from_ymd_opt(2015, 7, 12).expect("Invalid date provided");
let params = Configuration::with(Method::NorthAmerica, Madhab::Hanafi);
let result = PrayerSchedule::new()
.on(date)
Expand All @@ -169,7 +169,7 @@ mod tests {

#[test]
fn calculate_qiyam_times() {
let date = Utc.ymd(2015, 7, 12);
let date = NaiveDate::from_ymd_opt(2015, 7, 12).expect("Invalid date provided");
let params = Configuration::with(Method::NorthAmerica, Madhab::Hanafi);
let coordinates = Coordinates::new(35.7750, -78.6336);
let result = PrayerSchedule::new()
Expand Down Expand Up @@ -207,15 +207,15 @@ mod tests {
params.high_latitude_rule = HighLatitudeRule::MiddleOfTheNight;

let result = PrayerSchedule::new()
.on(Utc.ymd(2021, 1, 13))
.on(NaiveDate::from_ymd_opt(2021, 1, 13).expect("Invalid date provided"))
.for_location(Coordinates::new(1.370844612058886, 103.80145644060552))
.with_configuration(params)
.calculate();

match result {
Ok(schedule) => {
let hour = 3600;
let sgt_offset = FixedOffset::east(8 * hour);
let sgt_offset = FixedOffset::east_opt(8 * hour).expect("Invalid offset provided");
let sgt_fajr = schedule.time(Prayer::Fajr).with_timezone(&sgt_offset);
let sgt_sunrise = schedule.time(Prayer::Sunrise).with_timezone(&sgt_offset);
let sgt_dhuhr = schedule.time(Prayer::Dhuhr).with_timezone(&sgt_offset);
Expand Down Expand Up @@ -256,15 +256,15 @@ mod tests {
.done();

let result = PrayerSchedule::new()
.on(Utc.ymd(2021, 1, 12))
.on(NaiveDate::from_ymd_opt(2021, 1, 12).expect("Invalid date provided"))
.for_location(Coordinates::new(-6.18233995, 106.84287154))
.with_configuration(params)
.calculate();

match result {
Ok(schedule) => {
let hour = 3600;
let wib_offset = FixedOffset::east(7 * hour);
let wib_offset = FixedOffset::east_opt(7 * hour).expect("Invalid offset provided");
let wib_fajr = schedule.time(Prayer::Fajr).with_timezone(&wib_offset);
let wib_sunrise = schedule.time(Prayer::Sunrise).with_timezone(&wib_offset);
let wib_dhuhr = schedule.time(Prayer::Dhuhr).with_timezone(&wib_offset);
Expand Down
Loading

0 comments on commit 2ad1c79

Please sign in to comment.