| 1 |
use std::fmt; |
| 2 |
use std::time::SystemTime; |
| 3 |
|
| 4 |
use crate::fmt::{Formatter, TimestampPrecision}; |
| 5 |
|
| 6 |
impl Formatter { |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
pub fn timestamp(&self) -> Timestamp { |
| 25 |
Timestamp { |
| 26 |
time: SystemTime::now(), |
| 27 |
precision: TimestampPrecision::Seconds, |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
pub fn timestamp_seconds(&self) -> Timestamp { |
| 34 |
Timestamp { |
| 35 |
time: SystemTime::now(), |
| 36 |
precision: TimestampPrecision::Seconds, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
pub fn timestamp_millis(&self) -> Timestamp { |
| 43 |
Timestamp { |
| 44 |
time: SystemTime::now(), |
| 45 |
precision: TimestampPrecision::Millis, |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
pub fn timestamp_micros(&self) -> Timestamp { |
| 52 |
Timestamp { |
| 53 |
time: SystemTime::now(), |
| 54 |
precision: TimestampPrecision::Micros, |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
pub fn timestamp_nanos(&self) -> Timestamp { |
| 61 |
Timestamp { |
| 62 |
time: SystemTime::now(), |
| 63 |
precision: TimestampPrecision::Nanos, |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
pub struct Timestamp { |
| 75 |
time: SystemTime, |
| 76 |
precision: TimestampPrecision, |
| 77 |
} |
| 78 |
|
| 79 |
impl fmt::Debug for Timestamp { |
| 80 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 81 |
|
| 82 |
struct TimestampValue<'a>(&'a Timestamp); |
| 83 |
|
| 84 |
impl fmt::Debug for TimestampValue<'_> { |
| 85 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 86 |
fmt::Display::fmt(&self.0, f) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
f.debug_tuple("Timestamp") |
| 91 |
.field(&TimestampValue(self)) |
| 92 |
.finish() |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
impl fmt::Display for Timestamp { |
| 97 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 98 |
let Ok(ts) = jiff::Timestamp::try_from(self.time) else { |
| 99 |
return Err(fmt::Error); |
| 100 |
}; |
| 101 |
|
| 102 |
match self.precision { |
| 103 |
TimestampPrecision::Seconds => write!(f, "{ts:.0}"), |
| 104 |
TimestampPrecision::Millis => write!(f, "{ts:.3}"), |
| 105 |
TimestampPrecision::Micros => write!(f, "{ts:.6}"), |
| 106 |
TimestampPrecision::Nanos => write!(f, "{ts:.9}"), |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
#[cfg(test)] |
| 112 |
mod tests { |
| 113 |
use super::Timestamp; |
| 114 |
use crate::TimestampPrecision; |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn test_display_timestamp() { |
| 118 |
let mut ts = Timestamp { |
| 119 |
time: std::time::SystemTime::UNIX_EPOCH, |
| 120 |
precision: TimestampPrecision::Nanos, |
| 121 |
}; |
| 122 |
|
| 123 |
assert_eq!("1970-01-01T00:00:00.000000000Z", format!("{ts}")); |
| 124 |
|
| 125 |
ts.precision = TimestampPrecision::Micros; |
| 126 |
assert_eq!("1970-01-01T00:00:00.000000Z", format!("{ts}")); |
| 127 |
|
| 128 |
ts.precision = TimestampPrecision::Millis; |
| 129 |
assert_eq!("1970-01-01T00:00:00.000Z", format!("{ts}")); |
| 130 |
|
| 131 |
ts.precision = TimestampPrecision::Seconds; |
| 132 |
assert_eq!("1970-01-01T00:00:00Z", format!("{ts}")); |
| 133 |
} |
| 134 |
} |