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