Struct perDiem::types::Date

source ·
pub struct Date {
    pub day: u8,
    pub month: u8,
    pub year: i32,
}
Expand description

The basic Date struct

Fields§

§day: u8

Contains the day number of the Date instance

§month: u8

Contains the month number of the Date instance

§year: i32

Contains the year number of the Date instance

Implementations§

source§

impl Date

source

pub fn is_valid(&self) -> bool

Checks if a Date is valid

§Example
 use perDiem::types::Date;
 
 assert_eq!(Date { day: 1, month: 1, year: 2000}.is_valid(), true);
 assert_eq!(Date { day: 29, month: 2, year: 2001}.is_valid(), false);
 assert_eq!(Date { day: 50, month: 4, year: 2000}.is_valid(), false);
source

pub fn allShare(vec: Vec<Date>) -> Vec<&'static str>

Returns a Vector of the shared fields in each Date from Vector

§Example
 use perDiem::types::Date;
 
 let shares_vec = Date::allShare(vec![Date::from(1, 1, 2001).unwrap(), Date::from(1, 1, 2000).unwrap()]);
 assert_eq!(shares_vec.contains(&"day"), true);
 assert_eq!(shares_vec.contains(&"month"), true);
 assert_eq!(shares_vec.contains(&"year"), false);
source

pub fn snapshot_date() -> Date

Takes a snapshot of the current date in Local time

source

pub fn days_in_month(&self) -> i8

Returns the # of days in the month of the Date/DateTime(Credit to TDark on Rust Discord)

§Example
 use perDiem::types::Date;
 
 assert_eq!(Date::from(1, 1, 2000).unwrap().days_in_month(), 31);
 assert_eq!(Date::from(1, 2, 2000).unwrap().days_in_month(), 29);
source

pub fn datesShare( &self, datetime2: &Date, compare_type: &str ) -> Result<bool, &str>

same function of sharesDay, sharesMonth, sharesYear, but adds comparison field as a param.

§Example
 use perDiem::types::Date;
 
 assert_eq!(Date::from(1, 5, 2000).unwrap().datesShare(&Date::from(1, 10, 2001).unwrap(), "day").unwrap(), true);
 assert_eq!(Date::from(1, 5, 2000).unwrap().datesShare(&Date::from(1, 10, 2001).unwrap(), "month").unwrap(), false);
source

pub fn is_after(&self, date: Date) -> bool

Takes self and Date fields and returns true if self is after the second, date, and false if not.

§Example
 use perDiem::types::Date;
 
 assert_eq!(Date::from(1, 1, 2001).unwrap().is_after(Date::from(1, 1, 2000).unwrap()), true);
 assert_eq!(Date::from(1, 1, 2000).unwrap().is_after(Date::from(1, 1, 2001).unwrap()), false);
source

pub fn is_before(&self, date: Date) -> bool

Takes self and Date fields and returns true if self is before the second, date, and false if not.

§Example
 use perDiem::types::Date;
 
 assert_eq!(Date::from(1, 1, 2000).unwrap().is_before(Date::from(1, 1, 2001).unwrap()), true);
 assert_eq!(Date::from(1, 1, 2001).unwrap().is_before(Date::from(1, 1, 2000).unwrap()), false);
source§

impl Date

source

pub fn last_two_digits_year(&self) -> String

Returns the last two digits of the given year

§Example
 
 use perDiem::types::Date;
 
 assert_eq!(Date::from(1, 1, 2021).unwrap().last_two_digits_year(), "21".to_string());
source

pub fn to_OrdinalDate(&self) -> Result<OrdinalDate, &'static str>

Converts given Date to OrdinalDate

§Example
 use perDiem::types::{Date, OrdinalDate};
 
 assert_eq!(Date::from(1, 1, 2021).unwrap().to_OrdinalDate().unwrap(), OrdinalDate::from(1, 2021).unwrap());
 assert_eq!(Date::from(25, 5, 2021).unwrap().to_OrdinalDate().unwrap(), OrdinalDate::from(145, 2021).unwrap());
source

pub fn start_of_year(&mut self)

Changes Date to be start of year

§Example
 use perDiem::types::Date;
 
 let mut date = Date::from(20, 11, 2021).unwrap();
 date.start_of_year();
 assert_eq!(date, Date::from(1, 1, 2021).unwrap());
source

pub fn start_of_month(&mut self)

Changes Date to be start of current month

§Example
 use perDiem::types::Date;
 
 let mut date = Date::from(20, 11, 2021).unwrap();
 date.start_of_month();
 assert_eq!(date, Date::from(1, 11, 2021).unwrap());
source

pub fn end_of_year(&mut self)

Changes Date to be end of current year

§Example
 use perDiem::types::Date;
 
 let mut date = Date::from(20, 11, 2021).unwrap();
 date.end_of_year();
 assert_eq!(date, Date::from(31, 12, 2021).unwrap());
source

pub fn end_of_month(&mut self)

Changes Date to be end of current month

§Example
 use perDiem::types::Date; 
 
 let mut date = Date::from(20, 11, 2021).unwrap();
 date.end_of_month();
 assert_eq!(date, Date::from(30, 11, 2021).unwrap());
source

pub fn increase(&mut self, length: TimeSpan) -> Result<(), &'static str>

Increases the receiver-in-place Date by the TimeSpan specified and returns a Result.

§Example
 use perDiem::types::{Date, TimeSpan};
 
 let mut date = Date::from(20, 11, 2021).unwrap();
 date.increase(TimeSpan::days(5)).unwrap();
 assert_eq!(date, Date::from(25, 11, 2021).unwrap());
source

pub fn increase_ordinally( &mut self, length: TimeSpan ) -> Result<(), &'static str>

Increases the receiver-in-place Date by the TimeSpan specified and returns a result(same result as increase method, just better at larger increases)

§Example
 use perDiem::types::{Date, TimeSpan};
 
 let mut date = Date::from(20, 11, 2021).unwrap();
 date.increase_ordinally(TimeSpan::days(5));
 assert_eq!(date, Date::from(25, 11, 2021).unwrap());
source

pub fn decrease_ordinally( &mut self, length: TimeSpan ) -> Result<(), &'static str>

Decreases the receiver-in-place Date by the TimeSpan specified and returns a Result.

§Example
 use perDiem::types::{Date, TimeSpan};
 
 let mut date = Date::from(20, 11, 2021).unwrap();
 date.decrease_ordinally(TimeSpan::days(5));
 assert_eq!(date, Date::from(15, 11, 2021).unwrap());
source

pub fn difference(&self, date: &Date) -> TimeDifference

Returns the difference between two Dates as a TimeDifference with seconds, minutes, and hours set to 0

§Example
 use perDiem::types::{Date, TimeDifference};
 
 let date1 = Date::from(20, 11, 2021).unwrap();
 let date2 = Date::from(25, 5, 2024).unwrap();
 assert_eq!(date1.difference(&date2), TimeDifference {seconds: 0, minutes: 0, hours: 0, days: 5, months: 6, years: 3});
source

pub fn new() -> Date

Creates an instance of Date at the start of BCE

§Example
 use perDiem::types::Date;
 
 assert_eq!(Date::new(), Date::from(1, 1, 0).unwrap());
source

pub fn from(day: u8, month: u8, year: i32) -> Result<Date, &'static str>

Creates a instance of Date with fields provided

§Example
 use perDiem::types::Date;
 
 assert_eq!(Date::from(1, 1, 2021).unwrap(), Date { day: 1, month: 1, year: 2021});
source

pub fn to_DateTime(&self) -> DateTime

Converts Date to DateTime and sets second, minute, and hour to 0.

§Example
 use perDiem::types::{Date, DateTime};
 
 assert_eq!(Date::from(1, 1, 2021).unwrap().to_DateTime(), DateTime::from(0, 0, 0, 1, 1, 2021).unwrap());
source

pub fn decrease_ordinally_as_new( &self, length: TimeSpan ) -> Result<Date, &'static str>

Decreases given Date using conversion to ordinal for days. Much better at larger increases than other, but much poorer at smaller increases.*Difference is only for TimeSpan::days

§Example
 use perDiem::types::{Date, TimeSpan};
 
 assert_eq!(Date::from(20, 11, 2021).unwrap().decrease_ordinally_as_new(TimeSpan::days(5)).unwrap(), Date::from(15, 11, 2021).unwrap());
 assert_eq!(Date::from(20, 11, 2021).unwrap().decrease_ordinally_as_new(TimeSpan::months(5)).unwrap(), Date::from(20, 6, 2021).unwrap());
source

pub fn increase_ordinally_as_new( &self, length: TimeSpan ) -> Result<Date, &'static str>

Increases given Date using conversion to ordinal for days. Much better at larger increases than other, but much poorer at smaller increases.*Difference is only for TimeSpan::days

§Example
 use perDiem::types::{Date, TimeSpan};
 
 assert_eq!(Date::from(20, 11, 2021).unwrap().increase_ordinally_as_new(TimeSpan::days(5)).unwrap(), Date::from(25, 11, 2021).unwrap());
 assert_eq!(Date::from(20, 11, 2021).unwrap().increase_ordinally_as_new(TimeSpan::months(5)).unwrap(), Date::from(20, 4, 2022).unwrap());
source

pub fn increase_as_new(&self, length: TimeSpan) -> Result<Date, &'static str>

Creates a new instance of Date that has been incremented by given TimeSpan parameter

§Example
 use perDiem::types::{Date, TimeSpan};
 
 assert_eq!(Date::from(20, 11, 2021).unwrap().increase_as_new(TimeSpan::days(5)).unwrap(), Date::from(25, 11, 2021).unwrap());
 assert_eq!(Date::from(20, 11, 2021).unwrap().increase_as_new(TimeSpan::months(5)).unwrap(), Date::from(20, 4, 2022).unwrap());
source

pub fn decrease_as_new(&self, _length: TimeSpan) -> Result<Date, &'static str>

Decreases Date by given TimeSpan parameter. (Unfinished, use decrease_ordinally_as_new)

source§

impl Date

source

pub fn to_string( &self, date_format: &str, separator: &char ) -> Result<String, &str>

Converts a Date object to a String. Give date format something like ddmmyyyy or ddmmyy(for last two digits of year). And separator to output in the format given.

§Example
 use perDiem::types::Date;
 let date = Date::from(1, 1, 2021).unwrap();
 assert_eq!(date.to_string("ddmmyyyy", &'/').unwrap(), "01/01/2021".to_string());
 assert_eq!(date.to_string("ddmmyy", &'-').unwrap(), "01-01-21".to_string());
source

pub fn to_ChronoNaiveDate(&self) -> Option<NaiveDate>

Converts a perDiem::types::Date to a chrono::NaiveDate

Trait Implementations§

source§

impl Clone for Date

source§

fn clone(&self) -> Date

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 Date

source§

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

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

impl Iterable for Date

source§

fn iter<'a>(&'a self) -> IntoIter<(&'static str, &'a dyn Any)>

Returns an iterator over the struct’s fields as tuples. Read more
source§

impl PartialEq for Date

source§

fn eq(&self, other: &Date) -> 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 datekindEvals for Date

source§

fn isStartOfMonth(&self) -> bool

Method returns bool if Date or DateTime is start of month.

§Example
 use perDiem::types::Date;
 use perDiem::types::datekindEvals;
 
 assert_eq!(Date::from(1, 1, 2000).unwrap().isStartOfMonth(), true);
 assert_eq!(Date::from(15, 1, 2000).unwrap().isStartOfMonth(), false);
source§

fn isEndOfMonth(&self) -> bool

Method returns bool if Date or DateTime is end of month.

§Example
 use perDiem::types::Date; 
 use perDiem::types::datekindEvals;
 
 assert_eq!(Date::from(29, 2, 2000).unwrap().isEndOfMonth(), true);
 assert_eq!(Date::from(31, 3, 2000).unwrap().isEndOfMonth(), true);
 assert_eq!(Date::from(15, 1, 2000).unwrap().isEndOfMonth(), false);
source§

fn isStartOfYear(&self) -> bool

Method returns bool if Date or DateTime is start of year.

§Example
 use perDiem::types::Date;
 use perDiem::types::datekindEvals;
 
 assert_eq!(Date::from(1, 1, 2000).unwrap().isStartOfYear(), true);
 assert_eq!(Date::from(15, 1, 2000).unwrap().isStartOfYear(), false);
source§

fn isEndOfYear(&self) -> bool

Method returns bool if Date or DateTime is end of year.

§Example
 use perDiem::types::Date; 
 use perDiem::types::datekindEvals;
 
 assert_eq!(Date::from(31, 12, 2000).unwrap().isEndOfYear(), true);
 assert_eq!(Date::from(15, 1, 2000).unwrap().isEndOfYear(), false);
source§

fn isLeapYear(&self) -> bool

Method pass Date or DateTime and returns true if Date or DateTime’s year field is a leap year

§Example
 use perDiem::types::Date; 
 use perDiem::types::datekindEvals;
 
 assert_eq!(Date::from(1, 1, 2000).unwrap().isLeapYear(), true);
 assert_eq!(Date::from(1, 1, 2001).unwrap().isLeapYear(), false);
 assert_eq!(Date::from(1, 1, 1900).unwrap().isLeapYear(), false); // Make sure you understand leap year rules
source§

fn weekday(&self) -> Result<String, &str>

Method returns the day of the week as a String of the Date or DateTime passed to it.

§Example
 use perDiem::types::Date; 
 use perDiem::types::datekindEvals;
 
 assert_eq!(Date::from(22, 5, 2024).unwrap().weekday().unwrap(), "Wednesday");
 assert_eq!(Date::from(23, 5, 2024).unwrap().weekday().unwrap(), "Thursday");
source§

fn weekday_as_int(&self) -> Result<u8, &str>

Method returns the day of the week as a i8 with 0 being Sunday

§Example

use perDiem::evals::weekday_as_int;

println!(“Out test {}”, Date::from(23, 5, 2024).unwrap().weekday_as_int().unwrap()); assert_eq!(Date::from(23, 5, 2024).unwrap().weekday_as_int().unwrap(), 4); assert_eq!(Date::from(24, 5, 2024).unwrap().weekday_as_int().unwrap(), 5);

source§

fn sharesDay(&self, date2: &Date) -> bool

Returns true if both params share the same day of month

§Example
 use perDiem::types::Date;
 use perDiem::types::datekindEvals;
 
 assert_eq!(Date::from(1, 3, 2002).unwrap().sharesDay(&Date::from(1, 5, 2020).unwrap()), true);
 assert_eq!(Date::from(1, 3, 2020).unwrap().sharesDay(&Date::from(2, 3, 2020).unwrap()), false);
source§

fn sharesYear(&self, date2: &Date) -> bool

Returns true if both params share the same year

§Example
 use perDiem::types::Date;
 use perDiem::types::datekindEvals;
 
 assert_eq!(Date::from(5, 3, 2002).unwrap().sharesYear(&Date::from(1, 5, 2002).unwrap()), true);
 assert_eq!(Date::from(1, 3, 2000).unwrap().sharesYear(&Date::from(2, 3, 2020).unwrap()), false);
source§

fn sharesMonth(&self, date2: &Date) -> bool

Returns true if both params share the same month

§Example
 use perDiem::types::Date;
 use perDiem::types::datekindEvals;
 
 assert_eq!(Date::from(1, 3, 2002).unwrap().sharesMonth(&Date::from(15, 3, 2020).unwrap()), true);
 assert_eq!(Date::from(1, 3, 2000).unwrap().sharesMonth(&Date::from(2, 4, 2020).unwrap()), false);
source§

impl Eq for Date

source§

impl StructuralPartialEq for Date

Auto Trait Implementations§

§

impl Freeze for Date

§

impl RefUnwindSafe for Date

§

impl Send for Date

§

impl Sync for Date

§

impl Unpin for Date

§

impl UnwindSafe for Date

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> Conv for T

source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. 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> Pipe for T
where T: ?Sized,

source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
source§

impl<T> Tap for T

source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
source§

impl<T> ToOwned for T
where 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> TryConv for T

source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.