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
impl Date
sourcepub fn is_valid(&self) -> bool
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);
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);
sourcepub fn snapshot_date() -> Date
pub fn snapshot_date() -> Date
Takes a snapshot of the current date in Local time
sourcepub fn days_in_month(&self) -> i8
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);
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);
sourcepub fn is_after(&self, date: Date) -> bool
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);
sourcepub fn is_before(&self, date: Date) -> bool
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
impl Date
sourcepub fn last_two_digits_year(&self) -> String
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());
sourcepub fn to_OrdinalDate(&self) -> Result<OrdinalDate, &'static str>
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());
sourcepub fn start_of_year(&mut self)
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());
sourcepub fn start_of_month(&mut self)
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());
sourcepub fn end_of_year(&mut self)
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());
sourcepub fn end_of_month(&mut self)
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());
sourcepub fn increase(&mut self, length: TimeSpan) -> Result<(), &'static str>
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());
sourcepub fn increase_ordinally(
&mut self,
length: TimeSpan
) -> Result<(), &'static str>
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());
sourcepub fn decrease_ordinally(
&mut self,
length: TimeSpan
) -> Result<(), &'static str>
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());
sourcepub fn difference(&self, date: &Date) -> TimeDifference
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});
sourcepub fn new() -> Date
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());
sourcepub fn from(day: u8, month: u8, year: i32) -> Result<Date, &'static str>
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});
sourcepub fn to_DateTime(&self) -> DateTime
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());
sourcepub fn decrease_ordinally_as_new(
&self,
length: TimeSpan
) -> Result<Date, &'static str>
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());
sourcepub fn increase_ordinally_as_new(
&self,
length: TimeSpan
) -> Result<Date, &'static str>
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());
sourcepub fn increase_as_new(&self, length: TimeSpan) -> Result<Date, &'static str>
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§impl Date
impl Date
sourcepub fn to_string(
&self,
date_format: &str,
separator: &char
) -> Result<String, &str>
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());
sourcepub fn to_ChronoNaiveDate(&self) -> Option<NaiveDate>
pub fn to_ChronoNaiveDate(&self) -> Option<NaiveDate>
Converts a perDiem::types::Date to a chrono::NaiveDate
Trait Implementations§
source§impl PartialEq for Date
impl PartialEq for Date
source§impl datekindEvals for Date
impl datekindEvals for Date
source§fn isStartOfMonth(&self) -> bool
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
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
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
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
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>
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>
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);
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);
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);
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);
impl Eq for Date
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
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
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
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
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.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
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.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
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.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
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.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
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.