Skip to content

Commit 8f80ea7

Browse files
committed
refactor: port div_ceil from rust stdlib
1 parent 998e5e6 commit 8f80ea7

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/common/time/src/timestamp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use snafu::{OptionExt, ResultExt};
2626

2727
use crate::error;
2828
use crate::error::{ArithmeticOverflowSnafu, Error, ParseTimestampSnafu, TimestampOverflowSnafu};
29+
use crate::util::div_ceil;
2930

3031
#[derive(Debug, Clone, Default, Copy, Serialize, Deserialize)]
3132
pub struct Timestamp {
@@ -143,8 +144,7 @@ impl Timestamp {
143144
Some(Timestamp::new(value, unit))
144145
} else {
145146
let mul = unit.factor() / self.unit().factor();
146-
let new_ts = self.value as f64 / mul as f64;
147-
Some(Timestamp::new(new_ts.ceil() as i64, unit))
147+
Some(Timestamp::new(div_ceil(self.value, mul as i64), unit))
148148
}
149149
}
150150

src/common/time/src/util.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ pub fn current_time_millis() -> i64 {
1717
chrono::Utc::now().timestamp_millis()
1818
}
1919

20+
/// Port of rust unstable features `int_roundings`.
21+
pub(crate) fn div_ceil(this: i64, rhs: i64) -> i64 {
22+
let d = this / rhs;
23+
let r = this % rhs;
24+
if r > 0 && rhs > 0 {
25+
d + 1
26+
} else {
27+
d
28+
}
29+
}
30+
2031
#[cfg(test)]
2132
mod tests {
2233
use std::time::{self, SystemTime};

0 commit comments

Comments
 (0)