cassandra_cpp/cassandra/time.rs
1use crate::cassandra::util::{Protected, ProtectedInner};
2
3use crate::cassandra_sys::cass_time_from_epoch;
4use crate::cassandra_sys::cass_timestamp_gen_free;
5use crate::cassandra_sys::cass_timestamp_gen_monotonic_new;
6use crate::cassandra_sys::cass_timestamp_gen_server_side_new;
7use crate::cassandra_sys::CassTimestampGen as _TimestampGen;
8// use cassandra_sys::cass_date_from_epoch;
9// use cassandra_sys::cass_date_time_to_epoch;
10
11use std::time::Duration;
12
13/// Generators of client-side, microsecond-precision timestamps.
14/// <b>Note:</b> This generator is thread-safe and can be shared by multiple sessions.
15#[derive(Debug)]
16pub struct TimestampGen(*mut _TimestampGen);
17unsafe impl Send for TimestampGen {}
18unsafe impl Sync for TimestampGen {}
19
20impl ProtectedInner<*mut _TimestampGen> for TimestampGen {
21 fn inner(&self) -> *mut _TimestampGen {
22 self.0
23 }
24}
25
26impl Protected<*mut _TimestampGen> for TimestampGen {
27 fn build(inner: *mut _TimestampGen) -> Self {
28 if inner.is_null() {
29 panic!("Unexpected null pointer")
30 };
31 TimestampGen(inner)
32 }
33}
34
35// ///Cassandra representation of the number of days since epoch
36// pub struct Date(u32);
37
38/// Converts a unix timestamp (in seconds) to the Cassandra "time" type. The "time" type
39/// represents the number of nanoseconds since midnight (range 0 to 86399999999999).
40#[derive(Debug)]
41pub struct Time(i64);
42
43impl TimestampGen {
44 /// Converts a unix timestamp (in seconds) to the Cassandra "time" type. The "time" type
45 /// represents the number of nanoseconds since midnight (range 0 to 86399999999999).
46 pub fn time_from_epoch(epoch_seconds: Duration) -> Time {
47 unsafe { Time(cass_time_from_epoch(epoch_seconds.as_secs() as _)) }
48 }
49
50 /// Creates a new monotonically increasing timestamp generator. This generates
51 /// microsecond timestamps with the sub-millisecond part generated using a counter.
52 /// The implementation guarantees that no more than 1000 timestamps will be generated
53 /// for a given clock tick even if shared by multiple session objects. If that rate is
54 /// exceeded then a warning is logged and timestamps stop incrementing until the next
55 /// clock tick.
56 pub fn gen_monotonic_new() -> Self {
57 unsafe { TimestampGen(cass_timestamp_gen_monotonic_new()) }
58 }
59
60 /// Creates a new server-side timestamp generator. This generator allows Cassandra
61 /// to assign timestamps server-side.
62 ///
63 /// <b>Note:</b> This is the default timestamp generator.
64 pub fn gen_server_side_new() -> Self {
65 unsafe { TimestampGen(cass_timestamp_gen_server_side_new()) }
66 }
67
68 // pub fn from_epoch() -> Self {
69 // unsafe { TimestampGen(cass_timestamp_gen_monotonic_new()) }
70 // }
71}
72
73// Converts a unix timestamp (in seconds) to the Cassandra "date" type. The "date" type
74// represents the number of days since the Epoch (1970-01-01) with the Epoch centered at
75// the value 2^31.
76// fn date_from_epoch(epoch_secs: Duration) -> Date {
77// unsafe { Date(cass_date_from_epoch(epoch_secs.num_days())) }
78// }
79
80// Combines the Cassandra "date" and "time" types to Epoch time in seconds.
81// fn date_time_to_epoch(date: Date, time: Time) -> Duration {
82// unsafe { Duration::seconds(cass_date_time_to_epoch(date.0, time.0)) }
83// }
84
85impl Drop for TimestampGen {
86 fn drop(&mut self) {
87 unsafe { cass_timestamp_gen_free(self.0) }
88 }
89}