Struct JulianDay

Source
pub struct JulianDay(pub i64);
Expand description

Store a proleptic gregorian date as JulianDay

The Julian Day Number or Julian Day is the number of days since noon UTC on November 24 of the year -4713 in the Gregorian Calendar. The year 0 equals 1BC. The JD number is a possibly negative integer representing the number of whole days since the reference instant to noon of that day. This JulianDay implementation does not have a fraction and stores the day as if no time was specified which is equivalent of a time being always noon.

§Examples

use fundu_gnu::JulianDay;

assert_eq!(JulianDay(0).to_gregorian(), Some((-4713, 11, 24)));
assert_eq!(JulianDay(-365).to_gregorian(), Some((-4714, 11, 24)));
assert_eq!(JulianDay(1_721_060).to_gregorian(), Some((0, 1, 1)));
assert_eq!(JulianDay(2_440_588).to_gregorian(), Some((1970, 1, 1)));

Tuple Fields§

§0: i64

Implementations§

Source§

impl JulianDay

Source

pub const fn from_gregorian(year: i64, month: u8, day: u8) -> Self

Calculate the JulianDay from a proleptic gregorian date

For a non-panicking version see JulianDay::try_from_gregorian.

§Panics

Panics if the input arguments are invalid. Valid ranges are:

  • 1 <= month <= 12
  • 1 <= day <= 31

or an overflow occurred. Use the safer alternative JulianDay::try_from_gregorian if very high values for years are expected.

§Examples
use fundu_gnu::JulianDay;

assert_eq!(JulianDay::from_gregorian(-4713, 11, 24), JulianDay(0));
assert_eq!(JulianDay::from_gregorian(-4714, 11, 24), JulianDay(-365));
assert_eq!(JulianDay::from_gregorian(0, 1, 1), JulianDay(1_721_060));
assert_eq!(JulianDay::from_gregorian(1970, 1, 1), JulianDay(2440588));
Source

pub const fn try_from_gregorian(year: i64, month: u8, day: u8) -> Option<Self>

Calculate the JulianDay from a proleptic gregorian date

Returns None if an overflow occurred most likely to a year greater than approximately 25_200_470_046_046_596 or smaller than approximately -25_200_470_046_046_596

This method is based on the work of Peter Baum and his publication of Date Algorithms.

§Panics

Panics if the input arguments are invalid. Valid ranges are:

  • 1 <= month <= 12
  • 1 <= day <= 31

Note it is not an error to specify day = 31 for example for the month 4 (April). In such a case the month is assumed to be the next month (here May) and day = 1.

§Examples
use fundu_gnu::JulianDay;

assert_eq!(
    JulianDay::try_from_gregorian(-4713, 11, 24),
    Some(JulianDay(0))
);
assert_eq!(
    JulianDay::try_from_gregorian(-4714, 11, 24),
    Some(JulianDay(-365))
);
assert_eq!(
    JulianDay::try_from_gregorian(0, 1, 1),
    Some(JulianDay(1_721_060))
);
assert_eq!(
    JulianDay::try_from_gregorian(1970, 1, 1),
    Some(JulianDay(2440588))
);
Source

pub const fn as_days(self) -> i64

Return the julian day

§Examples
use fundu_gnu::JulianDay;

assert_eq!(JulianDay(-1).as_days(), -1);
Source

pub fn to_gregorian(self) -> Option<(i64, u8, u8)>

Calculate the proleptic gregorian date from this JulianDay

The method returns None if an overflow occurred.

This method is based on the work of Peter Baum and his publication of Date Algorithms.

§Examples
use fundu_gnu::JulianDay;

assert_eq!(JulianDay(-365).to_gregorian(), Some((-4714, 11, 24)));
assert_eq!(JulianDay(0).to_gregorian(), Some((-4713, 11, 24)));
assert_eq!(JulianDay(1721060).to_gregorian(), Some((0, 1, 1)));
assert_eq!(JulianDay(2440588).to_gregorian(), Some((1970, 1, 1)));
Source

pub const fn checked_add_days(self, days: i64) -> Option<Self>

Checked days addition. Computes self.0 + days, returning None if an overflow occurred.

§Examples
use fundu_gnu::JulianDay;

assert_eq!(
    JulianDay(0).checked_add_days(10_000),
    Some(JulianDay(10_000))
);
assert_eq!(JulianDay(0).checked_add_days(-1), Some(JulianDay(-1)));
Source

pub const fn checked_sub_days(self, days: i64) -> Option<Self>

Checked days subtraction. Computes self.0 - days, returning None if an overflow occurred.

§Examples
use fundu_gnu::JulianDay;

assert_eq!(
    JulianDay(1_700_000).checked_sub_days(1),
    Some(JulianDay(1_699_999))
);
assert_eq!(JulianDay(0).checked_sub_days(366), Some(JulianDay(-366)));
Source

pub const fn checked_add(self, rhs: Self) -> Option<Self>

Checked addition. Computes self + rhs, returning None if an overflow occurred.

§Examples
use fundu_gnu::JulianDay;

assert_eq!(
    JulianDay(0).checked_add(JulianDay(10_000)),
    Some(JulianDay(10_000))
);
assert_eq!(JulianDay(0).checked_add(JulianDay(-1)), Some(JulianDay(-1)));
Source

pub const fn checked_sub(self, rhs: Self) -> Option<Self>

Checked subtraction. Computes self - rhs, returning None if an overflow occurred.

§Examples
use fundu_gnu::JulianDay;

assert_eq!(
    JulianDay(1_700_000).checked_sub(JulianDay(1)),
    Some(JulianDay(1_699_999))
);
assert_eq!(
    JulianDay(0).checked_sub(JulianDay(366)),
    Some(JulianDay(-366))
);

Trait Implementations§

Source§

impl Clone for JulianDay

Source§

fn clone(&self) -> JulianDay

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JulianDay

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for JulianDay

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Hash for JulianDay

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for JulianDay

Source§

fn cmp(&self, other: &JulianDay) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for JulianDay

Source§

fn eq(&self, other: &JulianDay) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for JulianDay

Source§

fn partial_cmp(&self, other: &JulianDay) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for JulianDay

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for JulianDay

Source§

impl Eq for JulianDay

Source§

impl StructuralPartialEq for JulianDay

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,