Enum CalendarDate

Source
pub enum CalendarDate {
    Previous(NaiveDate),
    Current(NaiveDate),
    Next(NaiveDate),
}
Expand description

Utility enum to represent a calendar date. Implements Deref to chrono::NaiveDate.

Variants§

§

Previous(NaiveDate)

§

Current(NaiveDate)

§

Next(NaiveDate)

Implementations§

Source§

impl CalendarDate

Source

pub fn is_other_month(&self) -> bool

Source

pub fn is_today(&self) -> bool

Source

pub fn is_selected(&self, selected_date: &NaiveDate) -> bool

Source

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

Source

pub fn is_between(&self, start_date: &NaiveDate, end_date: &NaiveDate) -> bool

Source

pub fn is_between_current_month( &self, start_date: &NaiveDate, end_date: &NaiveDate, ) -> bool

Source

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

Source

pub fn is_first_day_of_month(&self) -> bool

Source

pub fn is_last_day_of_month(&self) -> bool

Methods from Deref<Target = NaiveDate>§

Source

pub const MIN: NaiveDate

Source

pub const MAX: NaiveDate

Source

pub fn and_time(&self, time: NaiveTime) -> NaiveDateTime

Makes a new NaiveDateTime from the current date and given NaiveTime.

§Example
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};

let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
let t = NaiveTime::from_hms_milli_opt(12, 34, 56, 789).unwrap();

let dt: NaiveDateTime = d.and_time(t);
assert_eq!(dt.date(), d);
assert_eq!(dt.time(), t);
Source

pub fn and_hms(&self, hour: u32, min: u32, sec: u32) -> NaiveDateTime

👎Deprecated since 0.4.23: use and_hms_opt() instead

Makes a new NaiveDateTime from the current date, hour, minute and second.

No leap second is allowed here; use NaiveDate::and_hms_* methods with a subsecond parameter instead.

§Panics

Panics on invalid hour, minute and/or second.

Source

pub fn and_hms_opt( &self, hour: u32, min: u32, sec: u32, ) -> Option<NaiveDateTime>

Makes a new NaiveDateTime from the current date, hour, minute and second.

No leap second is allowed here; use NaiveDate::and_hms_*_opt methods with a subsecond parameter instead.

§Errors

Returns None on invalid hour, minute and/or second.

§Example
use chrono::NaiveDate;

let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
assert!(d.and_hms_opt(12, 34, 56).is_some());
assert!(d.and_hms_opt(12, 34, 60).is_none()); // use `and_hms_milli_opt` instead
assert!(d.and_hms_opt(12, 60, 56).is_none());
assert!(d.and_hms_opt(24, 34, 56).is_none());
Source

pub fn and_hms_milli( &self, hour: u32, min: u32, sec: u32, milli: u32, ) -> NaiveDateTime

👎Deprecated since 0.4.23: use and_hms_milli_opt() instead

Makes a new NaiveDateTime from the current date, hour, minute, second and millisecond.

The millisecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

§Panics

Panics on invalid hour, minute, second and/or millisecond.

Source

pub fn and_hms_milli_opt( &self, hour: u32, min: u32, sec: u32, milli: u32, ) -> Option<NaiveDateTime>

Makes a new NaiveDateTime from the current date, hour, minute, second and millisecond.

The millisecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

§Errors

Returns None on invalid hour, minute, second and/or millisecond.

§Example
use chrono::NaiveDate;

let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
assert!(d.and_hms_milli_opt(12, 34, 56, 789).is_some());
assert!(d.and_hms_milli_opt(12, 34, 59, 1_789).is_some()); // leap second
assert!(d.and_hms_milli_opt(12, 34, 59, 2_789).is_none());
assert!(d.and_hms_milli_opt(12, 34, 60, 789).is_none());
assert!(d.and_hms_milli_opt(12, 60, 56, 789).is_none());
assert!(d.and_hms_milli_opt(24, 34, 56, 789).is_none());
Source

pub fn and_hms_micro( &self, hour: u32, min: u32, sec: u32, micro: u32, ) -> NaiveDateTime

👎Deprecated since 0.4.23: use and_hms_micro_opt() instead

Makes a new NaiveDateTime from the current date, hour, minute, second and microsecond.

The microsecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

§Panics

Panics on invalid hour, minute, second and/or microsecond.

§Example
use chrono::{Datelike, NaiveDate, NaiveDateTime, Timelike, Weekday};

let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();

let dt: NaiveDateTime = d.and_hms_micro_opt(12, 34, 56, 789_012).unwrap();
assert_eq!(dt.year(), 2015);
assert_eq!(dt.weekday(), Weekday::Wed);
assert_eq!(dt.second(), 56);
assert_eq!(dt.nanosecond(), 789_012_000);
Source

pub fn and_hms_micro_opt( &self, hour: u32, min: u32, sec: u32, micro: u32, ) -> Option<NaiveDateTime>

Makes a new NaiveDateTime from the current date, hour, minute, second and microsecond.

The microsecond part is allowed to exceed 1,000,000 in order to represent a leap second, but only when sec == 59.

§Errors

Returns None on invalid hour, minute, second and/or microsecond.

§Example
use chrono::NaiveDate;

let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
assert!(d.and_hms_micro_opt(12, 34, 56, 789_012).is_some());
assert!(d.and_hms_micro_opt(12, 34, 59, 1_789_012).is_some()); // leap second
assert!(d.and_hms_micro_opt(12, 34, 59, 2_789_012).is_none());
assert!(d.and_hms_micro_opt(12, 34, 60, 789_012).is_none());
assert!(d.and_hms_micro_opt(12, 60, 56, 789_012).is_none());
assert!(d.and_hms_micro_opt(24, 34, 56, 789_012).is_none());
Source

pub fn and_hms_nano( &self, hour: u32, min: u32, sec: u32, nano: u32, ) -> NaiveDateTime

👎Deprecated since 0.4.23: use and_hms_nano_opt() instead

Makes a new NaiveDateTime from the current date, hour, minute, second and nanosecond.

The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

§Panics

Panics on invalid hour, minute, second and/or nanosecond.

Source

pub fn and_hms_nano_opt( &self, hour: u32, min: u32, sec: u32, nano: u32, ) -> Option<NaiveDateTime>

Makes a new NaiveDateTime from the current date, hour, minute, second and nanosecond.

The nanosecond part is allowed to exceed 1,000,000,000 in order to represent a leap second, but only when sec == 59.

§Errors

Returns None on invalid hour, minute, second and/or nanosecond.

§Example
use chrono::NaiveDate;

let d = NaiveDate::from_ymd_opt(2015, 6, 3).unwrap();
assert!(d.and_hms_nano_opt(12, 34, 56, 789_012_345).is_some());
assert!(d.and_hms_nano_opt(12, 34, 59, 1_789_012_345).is_some()); // leap second
assert!(d.and_hms_nano_opt(12, 34, 59, 2_789_012_345).is_none());
assert!(d.and_hms_nano_opt(12, 34, 60, 789_012_345).is_none());
assert!(d.and_hms_nano_opt(12, 60, 56, 789_012_345).is_none());
assert!(d.and_hms_nano_opt(24, 34, 56, 789_012_345).is_none());
Source

pub fn succ(&self) -> NaiveDate

👎Deprecated since 0.4.23: use succ_opt() instead

Makes a new NaiveDate for the next calendar date.

§Panics

Panics when self is the last representable date.

Source

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

Makes a new NaiveDate for the next calendar date.

§Errors

Returns None when self is the last representable date.

§Example
use chrono::NaiveDate;

assert_eq!(
    NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().succ_opt(),
    Some(NaiveDate::from_ymd_opt(2015, 6, 4).unwrap())
);
assert_eq!(NaiveDate::MAX.succ_opt(), None);
Source

pub fn pred(&self) -> NaiveDate

👎Deprecated since 0.4.23: use pred_opt() instead

Makes a new NaiveDate for the previous calendar date.

§Panics

Panics when self is the first representable date.

Source

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

Makes a new NaiveDate for the previous calendar date.

§Errors

Returns None when self is the first representable date.

§Example
use chrono::NaiveDate;

assert_eq!(
    NaiveDate::from_ymd_opt(2015, 6, 3).unwrap().pred_opt(),
    Some(NaiveDate::from_ymd_opt(2015, 6, 2).unwrap())
);
assert_eq!(NaiveDate::MIN.pred_opt(), None);
Source

pub fn years_since(&self, base: NaiveDate) -> Option<u32>

Returns the number of whole years from the given base until self.

§Errors

Returns None if base > self.

Source

pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat<I>
where I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>,

Formats the date with the specified formatting items. Otherwise it is the same as the ordinary format method.

The Iterator of items should be Cloneable, since the resulting DelayedFormat value may be formatted multiple times.

§Example
use chrono::format::strftime::StrftimeItems;
use chrono::NaiveDate;

let fmt = StrftimeItems::new("%Y-%m-%d");
let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
assert_eq!(d.format_with_items(fmt.clone()).to_string(), "2015-09-05");
assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");

The resulting DelayedFormat can be formatted directly via the Display trait.

assert_eq!(format!("{}", d.format_with_items(fmt)), "2015-09-05");
Source

pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>>

Formats the date with the specified format string. See the format::strftime module on the supported escape sequences.

This returns a DelayedFormat, which gets converted to a string only when actual formatting happens. You may use the to_string method to get a String, or just feed it into print! and other formatting macros. (In this way it avoids the redundant memory allocation.)

§Panics

Converting or formatting the returned DelayedFormat panics if the format string is wrong. Because of this delayed failure, you are recommended to immediately use the DelayedFormat value.

§Example
use chrono::NaiveDate;

let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
assert_eq!(d.format("%Y-%m-%d").to_string(), "2015-09-05");
assert_eq!(d.format("%A, %-d %B, %C%y").to_string(), "Saturday, 5 September, 2015");

The resulting DelayedFormat can be formatted directly via the Display trait.

assert_eq!(format!("{}", d.format("%Y-%m-%d")), "2015-09-05");
assert_eq!(format!("{}", d.format("%A, %-d %B, %C%y")), "Saturday, 5 September, 2015");
Source

pub fn iter_days(&self) -> NaiveDateDaysIterator

Returns an iterator that steps by days across all representable dates.

§Example

let expected = [
    NaiveDate::from_ymd_opt(2016, 2, 27).unwrap(),
    NaiveDate::from_ymd_opt(2016, 2, 28).unwrap(),
    NaiveDate::from_ymd_opt(2016, 2, 29).unwrap(),
    NaiveDate::from_ymd_opt(2016, 3, 1).unwrap(),
];

let mut count = 0;
for (idx, d) in NaiveDate::from_ymd_opt(2016, 2, 27).unwrap().iter_days().take(4).enumerate() {
    assert_eq!(d, expected[idx]);
    count += 1;
}
assert_eq!(count, 4);

for d in NaiveDate::from_ymd_opt(2016, 3, 1).unwrap().iter_days().rev().take(4) {
    count -= 1;
    assert_eq!(d, expected[count]);
}
Source

pub fn iter_weeks(&self) -> NaiveDateWeeksIterator

Returns an iterator that steps by weeks across all representable dates.

§Example

let expected = [
    NaiveDate::from_ymd_opt(2016, 2, 27).unwrap(),
    NaiveDate::from_ymd_opt(2016, 3, 5).unwrap(),
    NaiveDate::from_ymd_opt(2016, 3, 12).unwrap(),
    NaiveDate::from_ymd_opt(2016, 3, 19).unwrap(),
];

let mut count = 0;
for (idx, d) in NaiveDate::from_ymd_opt(2016, 2, 27).unwrap().iter_weeks().take(4).enumerate() {
    assert_eq!(d, expected[idx]);
    count += 1;
}
assert_eq!(count, 4);

for d in NaiveDate::from_ymd_opt(2016, 3, 19).unwrap().iter_weeks().rev().take(4) {
    count -= 1;
    assert_eq!(d, expected[count]);
}
Source

pub fn week(&self, start: Weekday) -> NaiveWeek

Returns the NaiveWeek that the date belongs to, starting with the Weekday specified.

Source

pub fn leap_year(&self) -> bool

Returns true if this is a leap year.

assert_eq!(NaiveDate::from_ymd_opt(2000, 1, 1).unwrap().leap_year(), true);
assert_eq!(NaiveDate::from_ymd_opt(2001, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2002, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2003, 1, 1).unwrap().leap_year(), false);
assert_eq!(NaiveDate::from_ymd_opt(2004, 1, 1).unwrap().leap_year(), true);
assert_eq!(NaiveDate::from_ymd_opt(2100, 1, 1).unwrap().leap_year(), false);

Trait Implementations§

Source§

impl Clone for CalendarDate

Source§

fn clone(&self) -> CalendarDate

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 PartialEq for CalendarDate

Source§

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

Source§

type Target = NaiveDate

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Copy for CalendarDate

Source§

impl StructuralPartialEq for CalendarDate

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, dst: *mut u8)

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

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<El, T, Marker> IntoElementMaybeSignal<T, Marker> for El
where El: IntoElementMaybeSignalType<T, Marker>, Marker: ?Sized,

Source§

impl<T, Js> IntoElementMaybeSignalType<T, Element> for Js
where T: From<Js> + Clone,

Source§

impl<El, T, Marker> IntoElementsMaybeSignal<T, Marker> for El
where El: IntoElementsMaybeSignalType<T, Marker>, Marker: ?Sized,

Source§

impl<T, Js> IntoElementsMaybeSignalType<T, Element> for Js
where T: From<Js> + Clone,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> StorageAccess<T> for T

Source§

fn as_borrowed(&self) -> &T

Borrows the value.
Source§

fn into_taken(self) -> T

Takes the value.
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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T