Skip to content

Commit 3571f1c

Browse files
fix: truncate time range to minutes (#1299)
suppress seconds, milliseconds and nanoseconds to make the start and end datetime till minutes this solves the issue with the counts api with human time
1 parent 95343bf commit 3571f1c

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/utils/time.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
use chrono::{DateTime, NaiveDate, NaiveDateTime, TimeDelta, Timelike, Utc};
19+
use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, TimeDelta, TimeZone, Timelike, Utc};
2020

2121
#[derive(Debug, thiserror::Error)]
2222
pub enum TimeParseError {
@@ -78,8 +78,8 @@ impl TimeRange {
7878
/// let range = TimeRange::parse_human_time("2023-01-01T12:00:00Z", "2023-01-01T15:00:00Z");
7979
/// ```
8080
pub fn parse_human_time(start_time: &str, end_time: &str) -> Result<Self, TimeParseError> {
81-
let start: DateTime<Utc>;
82-
let end: DateTime<Utc>;
81+
let mut start: DateTime<Utc>;
82+
let mut end: DateTime<Utc>;
8383

8484
if end_time == "now" {
8585
end = Utc::now();
@@ -89,6 +89,11 @@ impl TimeRange {
8989
end = DateTime::parse_from_rfc3339(end_time)?.into();
9090
};
9191

92+
// Truncate seconds, milliseconds, and nanoseconds to zero
93+
// to ensure that the time range is aligned to the minute
94+
start = truncate_to_minute(start);
95+
end = truncate_to_minute(end);
96+
9297
if start > end {
9398
return Err(TimeParseError::StartTimeAfterEndTime);
9499
}
@@ -287,6 +292,19 @@ impl TimeRange {
287292
}
288293
}
289294

295+
pub fn truncate_to_minute(dt: DateTime<Utc>) -> DateTime<Utc> {
296+
// Get the date and time components we want to keep
297+
let year = dt.year();
298+
let month = dt.month();
299+
let day = dt.day();
300+
let hour = dt.hour();
301+
let minute = dt.minute();
302+
303+
// Create a new DateTime with seconds, milliseconds, and nanoseconds set to 0
304+
Utc.with_ymd_and_hms(year, month, day, hour, minute, 0)
305+
.unwrap() // This should never fail with valid components
306+
}
307+
290308
/// Represents a minute value (0-59) and provides methods for converting it to a slot range.
291309
///
292310
/// # Examples

0 commit comments

Comments
 (0)