Was this page helpful?
Caution
You're viewing documentation for an unstable version of Scylla Rust Driver. Switch to the latest stable version.
Depending on feature flags, three different types can be used to interact with timestamps.
Internally timestamp is represented as
i64
describing number of milliseconds since unix epoch.
Without any extra features enabled, only frame::value::CqlTimestamp
is available. It’s an
i64
wrapper and it matches the internal time representation. It’s
the only type that supports full range of values that database accepts.
However, for most use cases other types are more practical. See following sections for chrono
and time
.
use scylla::frame::value::CqlTimestamp;
use futures::TryStreamExt;
// 64 seconds since unix epoch, 1970-01-01 00:01:04
let to_insert = CqlTimestamp(64 * 1000);
// Write timestamp to the table
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read timestamp from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.into_typed::<(CqlTimestamp,)>();
while let Some((value,)) = iter.try_next().await? {
// ...
}
If full value range is not required, chrono
feature can be used to enable support of
chrono::DateTime
. All values are expected to be converted
to UTC timezone explicitly, as timestamp doesn’t store
timezone information. Any precision finer than 1ms will be lost.
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use futures::TryStreamExt;
// 64.123 seconds since unix epoch, 1970-01-01 00:01:04.123
let to_insert = NaiveDateTime::new(
NaiveDate::from_ymd_opt(1970, 1, 1).unwrap(),
NaiveTime::from_hms_milli_opt(0, 1, 4, 123).unwrap(),
)
.and_utc();
// Write timestamp to the table
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read timestamp from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.into_typed::<(DateTime<Utc>,)>();
while let Some((timestamp_value,)) = iter.try_next().await? {
println!("{:?}", timestamp_value);
}
Alternatively, time
feature can be used to enable support of
time::OffsetDateTime
. As
timestamp doesn’t support timezone information, time will
be corrected to UTC and timezone info will be erased on write. On read, UTC timestamp is returned. Any precision finer
than 1ms will also be lost.
use futures::TryStreamExt;
use time::{Date, Month, OffsetDateTime, PrimitiveDateTime, Time};
// 64.123 seconds since unix epoch, 1970-01-01 00:01:04.123
let to_insert = PrimitiveDateTime::new(
Date::from_calendar_date(1970, Month::January, 1).unwrap(),
Time::from_hms_milli(0, 1, 4, 123).unwrap(),
)
.assume_utc();
// Write timestamp to the table
session
.query_unpaged("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,))
.await?;
// Read timestamp from the table
let mut iter = session.query_iter("SELECT a FROM keyspace.table", &[])
.await?
.into_typed::<(OffsetDateTime,)>();
while let Some((timestamp_value,)) = iter.try_next().await? {
println!("{:?}", timestamp_value);
}
Was this page helpful?