Was this page helpful?
Depending on feature flags, three different types can be used to interact with date.
Internally date is represented as number of days since -5877641-06-23 i.e. 2^31 days before unix epoch.
Without any extra features enabled, only frame::value::CqlDate
is available. It’s an
u32
wrapper and it matches the internal date representation.
However, for most use cases other types are more practical. See following sections for chrono
and time
.
use scylla::frame::value::CqlDate;
use futures::TryStreamExt;
// 1970-01-08
let to_insert = CqlDate((1 << 31) + 7);
// Insert date into the table
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read raw Date from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.rows_stream::<(CqlDate,)>()?;
while let Some((date_value,)) = iter.try_next().await? {
// ...
}
If full range is not required and chrono
feature is enabled,
chrono::NaiveDate
can be used.
chrono::NaiveDate
supports dates from
-262145-01-01 to 262143-12-31.
use chrono::NaiveDate;
use futures::TryStreamExt;
// 2021-03-24
let to_insert = NaiveDate::from_ymd_opt(2021, 3, 24).unwrap();
// Insert date into the table
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read NaiveDate from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.rows_stream::<(NaiveDate,)>()?;
while let Some((date_value,)) = iter.try_next().await? {
// ...
}
Alternatively, time
feature can be used to enable support of
time::Date
.
time::Date
’s value range depends on feature flags, see its
documentation to get more info.
use futures::TryStreamExt;
use time::{Date, Month};
// 2021-03-24
let to_insert = Date::from_calendar_date(2021, Month::March, 24).unwrap();
// Insert date into the table
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read Date from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.rows_stream::<(Date,)>()?;
while let Some((date_value,)) = iter.try_next().await? {
// ...
}
Was this page helpful?
On this page