radnelac 0.0.2

Calculations in a variety of different timekeeping systems.
Documentation
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::day_count::fixed::CalculatedBounds;
use crate::day_count::fixed::Epoch;
use crate::day_count::fixed::Fixed;
use crate::day_count::fixed::FromFixed;
use crate::day_count::fixed::ToFixed;
use crate::day_count::prelude::BoundedDayCount;

//LISTING 1.3 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.)
const JD_EPOCH: f64 = -1721424.5;

/// Represents a Julian Day Number (not to be confused with the Julian Calendar)
///
/// The Julian Day Number is the count of days since noon on January 1, 4713 BC
/// in the proleptic Julian Calendar (November 24, 4714 BCE in the proleptic Gregorian
/// Calendar).
///
/// This is internally a floating point number, where the integer portion represents a
/// particular day and the fractional portion represents a particular time of day.
///
/// Note that equality and ordering operations are subject to limitations similar to
/// equality and ordering operations on a floating point number. Two `JulianDay` values represent
/// the same day or even the same second, but still appear different on the sub-second level.
///
/// Further reading:
/// + [Wikipedia](https://en.m.wikipedia.org/wiki/Julian_day)
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Default)]
pub struct JulianDay(f64);

impl CalculatedBounds for JulianDay {}

impl FromFixed for JulianDay {
    fn from_fixed(t: Fixed) -> JulianDay {
        //LISTING 1.5 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.)
        JulianDay(t.get() - JD_EPOCH)
    }
}

impl ToFixed for JulianDay {
    fn to_fixed(self) -> Fixed {
        //LISTING 1.4 (*Calendrical Calculations: The Ultimate Edition* by Reingold & Dershowitz.)
        Fixed::new(JD_EPOCH + self.0)
    }
}

impl Epoch for JulianDay {
    fn epoch() -> Fixed {
        Fixed::new(JD_EPOCH)
    }
}

impl BoundedDayCount<f64> for JulianDay {
    fn new(t: f64) -> JulianDay {
        debug_assert!(JulianDay::in_effective_bounds(t).is_ok());
        JulianDay(t)
    }
    fn get(self) -> f64 {
        self.0
    }
}