Struct elastic_types::date::prelude::DateExpr
[−]
pub struct DateExpr<F> { /* fields omitted */ }
A date math expression.
Date math expressions start from an anchor date, like the literal now
or 2017-05-06
and apply math operations to produce a new date value.
Examples
A date expression for now
plus 2 days:
let expr: DateExpr<BasicDateTime> = DateExpr::now().add_days(2);
Which serialises to:
"now+2d"
A date expression using a concrete date value plus 2 days:
let expr: DateExpr<BasicDateTime> = DateExpr::value(DateValue::now()).add_days(2);
Which serialises to:
"20150301T145500.000Z||+2d"
Methods
impl<F> DateExpr<F> where
F: DateFormat,
[src]
F: DateFormat,
fn now() -> Self
Create a new date expression for now
.
This value is different from Date::now()
because it doesn't calculate the current date from the system clock.
It serialises to the literal string "now"
, which is interpreted by Elasticsearch when indexing.
fn value<D>(date: D) -> Self where
D: Into<FormattableDateValue<F>>,
D: Into<FormattableDateValue<F>>,
Create a new date expression from a concrete date value.
If the input is a DateValue
, then it'll use any format specified on the DateExpr
.
If the input is a Date
or chrono::DateTime
, then the format on its mapping must match the format expected by the DateExpr
.
Examples
Create a date expression from a chrono::DateTime
:
let date = DateValue::now(); // The format annotation `EpochMillis` is required let expr: DateExpr<EpochMillis> = DateExpr::value(date);
Create a date expression from a Date
:
let date: Date<DefaultDateMapping<EpochMillis>> = Date::now(); // The format `EpochMillis` is inferred let expr = DateExpr::value(date);
Attempting to create a date expression from a Date
with a different format will fail to compile:
let date: Date<DefaultDateMapping<BasicDateTime>> = Date::now(); // Error: expected struct `EpochMillis`, found struct `BasicDateTime` let expr: DateExpr<EpochMillis> = DateExpr::value(date);
This is to ensure formats aren't silently converted when they shouldn't be, leading to runtime errors in Elasticsearch.
Convert the date into a DateValue
first:
let date: Date<DefaultDateMapping<BasicDateTime>> = Date::now(); let expr: DateExpr<EpochMillis> = DateExpr::value(DateValue::from(date));
fn add_years(self, value: usize) -> Self
Add to the anchored date.
fn sub_years(self, value: usize) -> Self
Subtract from the anchored date.
fn round_year(self) -> Self
Round the anchored date.
fn add_months(self, value: usize) -> Self
Add to the anchored date.
fn sub_months(self, value: usize) -> Self
Subtract from the anchored date.
fn round_month(self) -> Self
Round the anchored date.
fn add_weeks(self, value: usize) -> Self
Add to the anchored date.
fn sub_weeks(self, value: usize) -> Self
Subtract from the anchored date.
fn round_week(self) -> Self
Round the anchored date.
fn add_days(self, value: usize) -> Self
Add to the anchored date.
fn sub_days(self, value: usize) -> Self
Subtract from the anchored date.
fn round_day(self) -> Self
Round the anchored date.
fn add_hours(self, value: usize) -> Self
Add to the anchored date.
fn sub_hours(self, value: usize) -> Self
Subtract from the anchored date.
fn round_hour(self) -> Self
Round the anchored date.
fn add_minutes(self, value: usize) -> Self
Add to the anchored date.
fn sub_minutes(self, value: usize) -> Self
Subtract from the anchored date.
fn round_minute(self) -> Self
Round the anchored date.
fn add_seconds(self, value: usize) -> Self
Add to the anchored date.
fn sub_seconds(self, value: usize) -> Self
Subtract from the anchored date.
fn round_second(self) -> Self
Round the anchored date.
Trait Implementations
impl<F: Debug> Debug for DateExpr<F>
[src]
impl<F: Clone> Clone for DateExpr<F>
[src]
fn clone(&self) -> DateExpr<F>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<F: PartialEq> PartialEq for DateExpr<F>
[src]
fn eq(&self, __arg_0: &DateExpr<F>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &DateExpr<F>) -> bool
This method tests for !=
.
impl<F> Display for DateExpr<F> where
F: DateFormat,
[src]
F: DateFormat,
fn fmt(&self, f: &mut Formatter) -> FmtResult
Formats the value using the given formatter. Read more
impl<F> Serialize for DateExpr<F> where
F: DateFormat,
[src]
F: DateFormat,