Skip to content

Commit af98cb6

Browse files
sunng87paomian
authored andcommitted
refactor: port div_ceil from stdlib to avoid unstable features (GreptimeTeam#1191)
* refactor: use float div&ceil to avoid unstable features * refactor: port div_ceil from rust stdlib
1 parent 5e00684 commit af98cb6

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/common/time/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(int_roundings)]
21
// Copyright 2023 Greptime Team
32
//
43
// Licensed under the Apache License, Version 2.0 (the "License");

src/common/time/src/timestamp.rs

Lines changed: 2 additions & 1 deletion
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,7 +144,7 @@ impl Timestamp {
143144
Some(Timestamp::new(value, unit))
144145
} else {
145146
let mul = unit.factor() / self.unit().factor();
146-
Some(Timestamp::new(self.value.div_ceil(mul as i64), unit))
147+
Some(Timestamp::new(div_ceil(self.value, mul as i64), unit))
147148
}
148149
}
149150

src/common/time/src/util.rs

Lines changed: 17 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};
@@ -42,4 +53,10 @@ mod tests {
4253
assert_eq!(datetime_std.hour(), datetime_now.hour());
4354
assert_eq!(datetime_std.minute(), datetime_now.minute());
4455
}
56+
57+
#[test]
58+
fn test_div_ceil() {
59+
let v0 = 9223372036854676001;
60+
assert_eq!(9223372036854677, div_ceil(v0, 1000));
61+
}
4562
}

0 commit comments

Comments
 (0)