Struct fundu_gnu::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 copy 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) -> Selfwhere Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

impl PartialEq<JulianDay> for JulianDay

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<JulianDay> 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

This method 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

This method 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

This method 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

This method 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 StructuralEq for JulianDay

source§

impl StructuralPartialEq for JulianDay

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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 Twhere T: for<'de> Deserialize<'de>,