-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #745 from Anfid/time-serialization
Improve common time and date crates support
- Loading branch information
Showing
18 changed files
with
2,171 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,117 @@ | ||
# Time | ||
`Time` is represented as [`chrono::Duration`](https://docs.rs/chrono/0.4.19/chrono/struct.Duration.html) | ||
|
||
Internally `Time` is represented as number of nanoseconds since midnight. | ||
It can't be negative or exceed `86399999999999` (24 hours). | ||
Depending on feature flags used, three different types can be used to interact with time. | ||
|
||
When sending in a query it needs to be wrapped in `value::Time` to differentiate from [`Timestamp`](timestamp.md) | ||
Internally [time](https://docs.scylladb.com/stable/cql/types.html#times) is represented as number of nanoseconds since | ||
midnight. It can't be negative or exceed `86399999999999` (23:59:59.999999999). | ||
|
||
## CqlTime | ||
|
||
Without any extra features enabled, only `frame::value::CqlTime` is available. It's an | ||
[`i64`](https://doc.rust-lang.org/std/primitive.i64.html) 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`. | ||
|
||
```rust | ||
# extern crate scylla; | ||
# use scylla::Session; | ||
# use std::error::Error; | ||
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> { | ||
use scylla::frame::value::CqlTime; | ||
use scylla::IntoTypedRows; | ||
|
||
// 64 seconds since midnight | ||
let to_insert = CqlTime(64 * 1_000_000_000); | ||
|
||
// Insert time into the table | ||
session | ||
.query("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,)) | ||
.await?; | ||
|
||
// Read time from the table | ||
if let Some(rows) = session | ||
.query("SELECT a FROM keyspace.table", &[]) | ||
.await? | ||
.rows | ||
{ | ||
for row in rows.into_typed::<(CqlTime,)>() { | ||
let (time_value,): (CqlTime,) = row?; | ||
} | ||
} | ||
# Ok(()) | ||
# } | ||
``` | ||
|
||
## chrono::NaiveTime | ||
|
||
If `chrono` feature is enabled, [`chrono::NaiveTime`](https://docs.rs/chrono/0.4/chrono/naive/struct.NaiveDate.html) | ||
can be used to interact with the database. Although chrono can represent leap seconds, they are not supported. | ||
Attempts to convert [`chrono::NaiveTime`](https://docs.rs/chrono/0.4/chrono/naive/struct.NaiveDate.html) with leap | ||
second to `CqlTime` or write it to the database will return an error. | ||
|
||
```rust | ||
# extern crate chrono; | ||
# extern crate scylla; | ||
# use scylla::Session; | ||
# use std::error::Error; | ||
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> { | ||
use chrono::NaiveTime; | ||
use scylla::IntoTypedRows; | ||
|
||
// 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("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,)) | ||
.await?; | ||
|
||
// Read time from the table | ||
if let Some(rows) = session | ||
.query("SELECT a FROM keyspace.table", &[]) | ||
.await? | ||
.rows | ||
{ | ||
for row in rows.into_typed::<(NaiveTime,)>() { | ||
let (time_value,): (NaiveTime,) = row?; | ||
} | ||
} | ||
# Ok(()) | ||
# } | ||
``` | ||
|
||
## time::Time | ||
|
||
If `time` feature is enabled, [`time::Time`](https://docs.rs/time/0.3/time/struct.Time.html) can be used to interact | ||
with the database. | ||
|
||
```rust | ||
# extern crate scylla; | ||
# extern crate time; | ||
# use scylla::Session; | ||
# use std::error::Error; | ||
# async fn check_only_compiles(session: &Session) -> Result<(), Box<dyn Error>> { | ||
use scylla::IntoTypedRows; | ||
use scylla::frame::value::Time; | ||
use chrono::Duration; | ||
use time::Time; | ||
|
||
// 01:02:03.456,789,012 | ||
let to_insert = Time::from_hms_nano(1, 2, 3, 456_789_012).unwrap(); | ||
|
||
// Insert some time into the table | ||
let to_insert: Duration = Duration::seconds(64); | ||
// Insert time into the table | ||
session | ||
.query("INSERT INTO keyspace.table (a) VALUES(?)", (Time(to_insert),)) | ||
.query("INSERT INTO keyspace.table (a) VALUES(?)", (to_insert,)) | ||
.await?; | ||
|
||
// Read time from the table, no need for a wrapper here | ||
if let Some(rows) = session.query("SELECT a FROM keyspace.table", &[]).await?.rows { | ||
for row in rows.into_typed::<(Duration,)>() { | ||
let (time_value,): (Duration,) = row?; | ||
// Read time from the table | ||
if let Some(rows) = session | ||
.query("SELECT a FROM keyspace.table", &[]) | ||
.await? | ||
.rows | ||
{ | ||
for row in rows.into_typed::<(Time,)>() { | ||
let (time_value,): (Time,) = row?; | ||
} | ||
} | ||
# Ok(()) | ||
# } | ||
``` | ||
``` |
Oops, something went wrong.