Was this page helpful?
Caution
You're viewing documentation for a deprecated version of Scylla Rust Driver. Switch to the latest stable version.
Depending on feature flags used, three different types can be used to interact with time.
Internally time is represented as number of nanoseconds since
midnight. It can’t be negative or exceed 86399999999999
(23:59:59.999999999).
Without any extra features enabled, only frame::value::CqlTime
is available. It’s an
i64
wrapper and it matches the internal time representation.
However, for most use cases other types are more practical. See following sections for chrono
and time
.
use scylla::frame::value::CqlTime;
use futures::TryStreamExt;
// 64 seconds since midnight
let to_insert = CqlTime(64 * 1_000_000_000);
// Insert time into the table
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read time from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.into_typed::<(CqlTime,)>();
while let Some((value,)) = iter.try_next().await? {
// ...
}
If chrono
feature is enabled, chrono::NaiveTime
can be used to interact with the database. Although chrono can represent leap seconds, they are not supported.
Attempts to convert chrono::NaiveTime
with leap
second to CqlTime
or write it to the database will return an error.
use chrono::NaiveTime;
use futures::TryStreamExt;
// 01:02:03.456,789,012
let to_insert = NaiveTime::from_hms_nano_opt(1, 2, 3, 456_789_012);
// Insert time into the table
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read time from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.into_typed::<(NaiveTime,)>();
while let Some((time_value,)) = iter.try_next().await? {
println!("{:?}", time_value);
}
If time
feature is enabled, time::Time
can be used to interact
with the database.
use futures::TryStreamExt;
use time::Time;
// 01:02:03.456,789,012
let to_insert = Time::from_hms_nano(1, 2, 3, 456_789_012).unwrap();
// Insert time into the table
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read time from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.into_typed::<(Time,)>();
while let Some((time_value,)) = iter.try_next().await? {
println!("{:?}", time_value);
}
Was this page helpful?
On this page