1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
use crate::error;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
const TIMESTAMP_MAX: i64 = i64::MAX - 1;
/// A `i64` timestamp expressed as microseconds since the UNIX epoch (UTC).
///
/// The number can't be negative (i.e. can't be before 1970-01-01 00:00:00).
///
/// # Examples
///
/// ```
/// # use questdb::Result;
/// use questdb::ingress::TimestampMicros;
///
/// # fn main() -> Result<()> {
/// let ts = TimestampMicros::new(1659548204354448)?;
/// # Ok(())
/// # }
/// ```
///
/// or
///
/// ```
/// # use questdb::Result;
/// use questdb::ingress::TimestampMicros;
/// use std::convert::TryInto;
///
/// # fn main() -> Result<()> {
/// let ts: TimestampMicros = std::time::SystemTime::now().try_into()?;
/// # Ok(())
/// # }
/// ```
#[derive(Copy, Clone, Debug)]
pub struct TimestampMicros(i64);
impl TimestampMicros {
/// Create a new timestamp from the given number of microseconds
/// since the UNIX epoch (UTC).
pub fn new(micros: i64) -> crate::Result<Self> {
if micros >= 0 {
Ok(Self(micros))
} else {
Err(error::fmt!(
InvalidTimestamp,
"Timestamp {} is negative. It must be >= 0.",
micros))
}
}
/// Get the numeric value of the timestamp.
pub fn as_i64(&self) -> i64 {
self.0
}
}
impl TryFrom<SystemTime> for TimestampMicros {
type Error = crate::Error;
fn try_from(time: SystemTime) -> crate::Result<Self> {
let duration = time.duration_since(UNIX_EPOCH)
.map_err(|e| error::fmt!(
// This will fail if before UNIX_EPOCH,
// so this check also guarantees that the
// eventual micros result will be >= 0.
InvalidTimestamp,
concat!(
"Could not calulate duration since UNIX_EPOCH",
" for timestamp {:?}: {}"),
time,
e))?;
let micros: u128 = duration.as_micros();
if micros <= (TIMESTAMP_MAX as u128) {
Ok(Self(micros as i64))
} else {
Err(error::fmt!(
InvalidTimestamp,
concat!(
"Timestamp {:?} is too large to fit in a ",
"64-bit signed integer."),
time))
}
}
}
impl From<TimestampMicros> for SystemTime {
fn from(timestamp: TimestampMicros) -> Self {
UNIX_EPOCH + Duration::from_micros(timestamp.0 as u64)
}
}
/// A `i64` timestamp expressed as nanoseconds since the UNIX epoch (UTC).
///
/// The number can't be negative (i.e. can't be before 1970-01-01 00:00:00).
///
/// # Examples
///
/// ```
/// # use questdb::Result;
/// use questdb::ingress::TimestampNanos;
///
/// # fn main() -> Result<()> {
/// let ts = TimestampNanos::new(1659548315647406592)?;
/// # Ok(())
/// # }
/// ```
///
/// or
///
/// ```
/// # use questdb::Result;
/// use questdb::ingress::TimestampNanos;
/// use std::convert::TryInto;
///
/// # fn main() -> Result<()> {
/// let ts: TimestampNanos = std::time::SystemTime::now().try_into()?;
/// # Ok(())
/// # }
/// ```
#[derive(Copy, Clone, Debug)]
pub struct TimestampNanos(i64);
impl TimestampNanos {
/// Create a new timestamp from the given number of nanoseconds
/// since the UNIX epoch (UTC).
pub fn new(nanos: i64) -> crate::Result<Self> {
if nanos >= 0 {
Ok(Self(nanos))
} else {
Err(error::fmt!(
InvalidTimestamp,
"Timestamp {} is negative. It must be >= 0.",
nanos))
}
}
/// Get the numeric value of the timestamp.
pub fn as_i64(&self) -> i64 {
self.0
}
}
impl TryFrom<SystemTime> for TimestampNanos {
type Error = crate::Error;
fn try_from(time: SystemTime) -> crate::Result<Self> {
let duration = time.duration_since(UNIX_EPOCH)
.map_err(|e| error::fmt!(
// This will fail if before UNIX_EPOCH,
// so this check also guarantees that the
// eventual nanos result will be >= 0.
InvalidTimestamp,
concat!(
"Could not calulate duration since UNIX_EPOCH",
" for timestamp {:?}: {}"),
time,
e))?;
let nanos: u128 = duration.as_nanos();
if nanos <= (TIMESTAMP_MAX as u128) {
Ok(Self(nanos as i64))
} else {
Err(error::fmt!(
InvalidTimestamp,
concat!(
"Timestamp {:?} is too large to fit in a ",
"64-bit signed integer."),
time))
}
}
}