[][src]Struct relativedelta::relativedelta::RelativeDelta

pub struct RelativeDelta { /* fields omitted */ }

RelativeDelta holding all data about the relative delta datetime.

If the relative delta date time is simple e.g. manipulating only a sigle time parameter, use one of the convenience methods to create a builder, and then call new to get the final RelativeDelta.

The builder is convenient for an ongoing and more complex construction of RelativeDelta, as all time parameters are normalized and only calculated once.

After creation the RelativeDelta can be added or substracted with itself or a chrono::DateTime object. Multiplication with f64 is possible as well. All operators are commutative.

Examples

Simple construction

This code runs with edition 2018
use chrono::{Utc, TimeZone, Datelike};
use relativedelta::RelativeDelta;

let years1 = RelativeDelta::with_years(1).new();

let months12 = RelativeDelta::with_months(12).new();
assert_eq!(years1, months12);

// date and time parameters are cleverly put within meaning full boundaries on creation where possible.
let months47 = RelativeDelta::with_months(47).new();
assert_eq!(months47.years(), 3);
assert_eq!(months47.months(), 11);

// This also eases comparison of two RelativeDeltas.
assert_eq!(RelativeDelta::with_months(47).new(), RelativeDelta::with_years(3).and_months(11).new());

More complex constructions

This code runs with edition 2018

// The and_parm methods should be prefered when possible as it works on mutable references and updates the Builder
// in place, where as the with_param methods creates copies and works on immutable references.
let years1 = RelativeDelta::with_years(1).and_days(32).new();

// If same parameter is specified twice, only the latest is applied.
let months6 = RelativeDelta::with_months(12).with_months(6).new();
assert_eq!(months6, RelativeDelta::with_months(6).new());

// Below is identical to: RelativeDelta::yysmmsdds(Some(2020), 1, Some(1), 3, None, 12).new();
let rddt = RelativeDelta::with_year(2020).and_years(1).and_month(Some(1)).and_months(3).and_days(12).new();

Implemented operators

This code runs with edition 2018

// Two or more RelativeDeltas can be added and substracted. However, note that constants are lost in the process.
let lhs = RelativeDelta::yysmmsdds(Some(2020), -4, Some(1), 3, None, 0).new();
let rhs = RelativeDelta::yysmmsdds(Some(2020), 1, Some(1), 42, None, 0).new();

assert_eq!(lhs + rhs, RelativeDelta::with_years(-3).and_months(45).new());
assert_eq!(lhs - rhs, RelativeDelta::with_years(-5).and_months(-39).new());
assert_eq!(-lhs + rhs, RelativeDelta::with_years(5).and_months(39).new());

// The RelativeDelta can be multiplied with a f64.
assert_eq!(rhs * 0.5, RelativeDelta::with_years(2).and_year(Some(2020)).and_months(3).and_month(Some(1)).new());

// This crates party piece is the ability to calculate dates based on already existing chrono::DateTime
// If one would like to get the last day of the month that one is currently in, it could be done with:
println!("{}", Utc::now() + RelativeDelta::with_months(1).and_day(Some(1)).and_days(-1).new());
// Above first sets the day of the month to the 1st, then adds a month and subtracts a day.

// If one were to get all quarters for the current year, one could do so by:
let dt = Utc.ymd(2020, 1, 1).and_hms(0,0,0);
let quarters : Vec<DateTime<Utc>> = (3..=12).step_by(3).map(|month| dt + RelativeDelta::with_day(1).and_month(Some(month)).new()).collect();
assert_eq!(quarters.len(), 4);
assert_eq!(quarters[0], Utc.ymd(2020, 3, 1).and_hms(0,0,0));
assert_eq!(quarters[1], Utc.ymd(2020, 6, 1).and_hms(0,0,0));
assert_eq!(quarters[2], Utc.ymd(2020, 9, 1).and_hms(0,0,0));
assert_eq!(quarters[3], Utc.ymd(2020, 12, 1).and_hms(0,0,0));

// One could also request the first monday after one year by
let first_monday_after_one_year = RelativeDelta::with_years(1).and_weekday(Some((Weekday::Mon, 1))).new();
let d = dt + first_monday_after_one_year;
assert_eq!(d, Utc.ymd(2021, 1, 4).and_hms(0,0,0));

Implementations

impl RelativeDelta[src]

pub fn ysmsdshsmsssns_f(
    years: f64,
    months: f64,
    days: f64,
    hours: f64,
    minutes: f64,
    seconds: f64,
    nanoseconds: i64
) -> Builder
[src]

Convenience construction of a RelativeDelta (Builder) with float paramters

Takes only relative date and time parameters, years, months, days, hours, minutes, seconds and nanoseconds Parameters will be normalized to ints wherever possible

pub fn yysmmsdds(
    year: Option<i32>,
    years: i32,
    month: Option<u32>,
    months: i64,
    day: Option<u32>,
    days: i64
) -> Builder
[src]

Convenience construction of a RelativeDelta (Builder) with only date parameters

pub fn hhsmmssss(
    hour: Option<u32>,
    hours: i64,
    minute: Option<u32>,
    minutes: i64,
    second: Option<u32>,
    seconds: i64
) -> Builder
[src]

Convenience construction of a RelativeDelta (Builder) with only time parameters

pub fn with_years(years: i32) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only relative years parameter

pub fn with_months(months: i64) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only relative months parameter

pub fn with_days(days: i64) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only relative days parameter

pub fn with_hours(hours: i64) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only relative hours parameter

pub fn with_minutes(minutes: i64) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only relative minutes parameter

pub fn with_seconds(seconds: i64) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only relative seconds parameter

pub fn with_nanoseconds(nanoseconds: i64) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only relative nanoseconds parameter

pub fn with_year(year: i32) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only constant year parameter

pub fn with_month(month: u32) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only constant month parameter

pub fn with_day(day: u32) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only constant day parameter

pub fn with_hour(hour: u32) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only constant hour parameter

pub fn with_minute(minute: u32) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only constant minute parameter

pub fn with_second(second: u32) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only constant second parameter

pub fn with_nanosecond(nanosecond: u32) -> Builder[src]

Convenience construction of a RelativeDelta (Builder) with only constant nanosecond parameter

pub fn with_weekday(weekday: Weekday, nth: i64) -> Builder[src]

pub fn years(&self) -> i32[src]

pub fn year(&self) -> Option<i32>[src]

pub fn months(&self) -> i64[src]

pub fn month(&self) -> Option<u32>[src]

pub fn days(&self) -> i64[src]

pub fn day(&self) -> Option<u32>[src]

pub fn hours(&self) -> i64[src]

pub fn hour(&self) -> Option<u32>[src]

pub fn minutes(&self) -> i64[src]

pub fn minute(&self) -> Option<u32>[src]

pub fn seconds(&self) -> i64[src]

pub fn second(&self) -> Option<u32>[src]

pub fn nanoseconds(&self) -> i64[src]

pub fn nanosecond(&self) -> Option<u32>[src]

pub fn weekday(&self) -> Option<(Weekday, i64)>[src]

pub fn total_months(&self) -> i64[src]

Calculate total months given the current months and years

Trait Implementations

impl<Tz: TimeZone, '_, '_> Add<&'_ DateTime<Tz>> for &'_ RelativeDelta[src]

type Output = DateTime<Tz>

The resulting type after applying the + operator.

impl<Tz: TimeZone, '_> Add<&'_ DateTime<Tz>> for RelativeDelta[src]

type Output = DateTime<Tz>

The resulting type after applying the + operator.

impl<Tz: TimeZone, '_, '_> Add<&'_ RelativeDelta> for &'_ DateTime<Tz>[src]

type Output = DateTime<Tz>

The resulting type after applying the + operator.

impl<Tz: TimeZone, '_> Add<&'_ RelativeDelta> for DateTime<Tz>[src]

type Output = DateTime<Tz>

The resulting type after applying the + operator.

impl<'a, 'b> Add<&'a RelativeDelta> for &'b RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the + operator.

impl<'a> Add<&'a RelativeDelta> for RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the + operator.

impl<Tz: TimeZone, '_> Add<DateTime<Tz>> for &'_ RelativeDelta[src]

type Output = DateTime<Tz>

The resulting type after applying the + operator.

impl<Tz: TimeZone> Add<DateTime<Tz>> for RelativeDelta[src]

type Output = DateTime<Tz>

The resulting type after applying the + operator.

impl<'a> Add<RelativeDelta> for &'a RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the + operator.

impl Add<RelativeDelta> for RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the + operator.

impl<Tz: TimeZone, '_> Add<RelativeDelta> for &'_ DateTime<Tz>[src]

type Output = DateTime<Tz>

The resulting type after applying the + operator.

impl<Tz: TimeZone> Add<RelativeDelta> for DateTime<Tz>[src]

type Output = DateTime<Tz>

The resulting type after applying the + operator.

impl Clone for RelativeDelta[src]

impl Copy for RelativeDelta[src]

impl Debug for RelativeDelta[src]

impl Default for RelativeDelta[src]

impl<'a> Div<f32> for &'a RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the / operator.

impl Div<f32> for RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the / operator.

impl<'a> Div<f64> for &'a RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the / operator.

impl Div<f64> for RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the / operator.

impl<'a> Div<usize> for &'a RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the / operator.

impl Div<usize> for RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the / operator.

impl From<RelativeDelta> for Option<NaiveDateTime>[src]

impl<'a> Mul<&'a RelativeDelta> for f64[src]

type Output = RelativeDelta

The resulting type after applying the * operator.

impl Mul<RelativeDelta> for f64[src]

type Output = RelativeDelta

The resulting type after applying the * operator.

impl<'a> Mul<f64> for &'a RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the * operator.

impl Mul<f64> for RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the * operator.

impl<'a> Neg for &'a RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the - operator.

impl Neg for RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the - operator.

impl PartialEq<RelativeDelta> for RelativeDelta[src]

impl StructuralPartialEq for RelativeDelta[src]

impl<Tz: TimeZone, '_, '_> Sub<&'_ RelativeDelta> for &'_ DateTime<Tz>[src]

Sub (non commutative)

type Output = DateTime<Tz>

The resulting type after applying the - operator.

impl<Tz: TimeZone, '_> Sub<&'_ RelativeDelta> for DateTime<Tz>[src]

type Output = DateTime<Tz>

The resulting type after applying the - operator.

impl<'a, 'b> Sub<&'a RelativeDelta> for &'b RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the - operator.

impl<'a> Sub<&'a RelativeDelta> for RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the - operator.

impl<'a> Sub<RelativeDelta> for &'a RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the - operator.

impl Sub<RelativeDelta> for RelativeDelta[src]

type Output = RelativeDelta

The resulting type after applying the - operator.

impl<Tz: TimeZone, '_> Sub<RelativeDelta> for &'_ DateTime<Tz>[src]

type Output = DateTime<Tz>

The resulting type after applying the - operator.

impl<Tz: TimeZone> Sub<RelativeDelta> for DateTime<Tz>[src]

type Output = DateTime<Tz>

The resulting type after applying the - operator.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.