Was this page helpful?
For most use cases Date
can be represented as
chrono::NaiveDate
.NaiveDate
supports dates from -262145-1-1 to 262143-12-31.
For dates outside of this range you can use the raw u32
representation.
chrono::NaiveDate
:¶use scylla::IntoTypedRows;
use chrono::naive::NaiveDate;
// Insert some date into the table
let to_insert: NaiveDate = NaiveDate::from_ymd_opt(2021, 3, 24).unwrap();
session
.query("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read NaiveDate from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows.into_typed::<(NaiveDate,)>() {
let (date_value,): (NaiveDate,) = row?;
}
}
u32
representation¶Internally Date
is represented as number of days since -5877641-06-23 i.e. 2^31 days before unix epoch.
use scylla::frame::value::Date;
use scylla::frame::response::result::CqlValue;
// Insert date using raw u32 representation
let to_insert: Date = Date(2_u32.pow(31)); // 1970-01-01
session
.query("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read raw Date from the table
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows {
for row in rows {
let date_value: u32 = match row.columns[0] {
Some(CqlValue::Date(date_value)) => date_value,
_ => panic!("Should be a date!")
};
}
}
Was this page helpful?