Skip to content

Commit 9b11935

Browse files
committed
Add subsec_nanos() method to Timestamp
1 parent 9899785 commit 9b11935

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

packages/std/src/timestamp.rs

+16
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ impl Timestamp {
5252
pub fn seconds(&self) -> u64 {
5353
self.0.u64() / 1_000_000_000
5454
}
55+
56+
/// Returns seconds since epoch (truncate nanoseconds)
57+
#[inline]
58+
pub fn subsec_nanos(&self) -> u64 {
59+
self.0.u64() % 1_000_000_000
60+
}
5561
}
5662

5763
impl fmt::Display for Timestamp {
@@ -136,6 +142,8 @@ mod tests {
136142
assert_eq!(sum.nanos(), 123);
137143
let sum = Timestamp::from_nanos(0);
138144
assert_eq!(sum.nanos(), 0);
145+
let sum = Timestamp::from_nanos(987654321000);
146+
assert_eq!(sum.nanos(), 987654321000);
139147
}
140148

141149
#[test]
@@ -146,6 +154,14 @@ mod tests {
146154
assert_eq!(sum.seconds(), 1234567);
147155
}
148156

157+
#[test]
158+
fn timestamp_subsec_nanos() {
159+
let sum = Timestamp::from_nanos(987654321000);
160+
assert_eq!(sum.subsec_nanos(), 654321000);
161+
let sum = Timestamp::from_seconds(1234567).plus_nanos(8765436);
162+
assert_eq!(sum.subsec_nanos(), 8765436);
163+
}
164+
149165
#[test]
150166
fn timestamp_implements_display() {
151167
let embedded = format!("Time: {}", Timestamp::from_nanos(0));

0 commit comments

Comments
 (0)