|
| 1 | +/* |
| 2 | + * Parseable Server (C) 2022 - 2024 Parseable, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +use chrono::{DateTime, Utc}; |
| 20 | + |
| 21 | +#[derive(Debug, thiserror::Error)] |
| 22 | +pub enum TimeParseError { |
| 23 | + #[error("Parsing humantime")] |
| 24 | + HumanTime(#[from] humantime::DurationError), |
| 25 | + #[error("Out of Range")] |
| 26 | + OutOfRange(#[from] chrono::OutOfRangeError), |
| 27 | + #[error("Error parsing time: {0}")] |
| 28 | + Chrono(#[from] chrono::ParseError), |
| 29 | + #[error("Start time cannot be greater than the end time")] |
| 30 | + StartTimeAfterEndTime, |
| 31 | +} |
| 32 | + |
| 33 | +/// Represents a range of time with a start and end point. |
| 34 | +#[derive(Debug)] |
| 35 | +pub struct TimeRange { |
| 36 | + pub start: DateTime<Utc>, |
| 37 | + pub end: DateTime<Utc>, |
| 38 | +} |
| 39 | + |
| 40 | +impl TimeRange { |
| 41 | + /// Parses human-readable time strings into a `TimeRange` object. |
| 42 | + /// |
| 43 | + /// # Arguments |
| 44 | + /// - `start_time`: A string representing the start of the time range. This can either be |
| 45 | + /// a human-readable duration (e.g., `"2 hours"`) or an RFC 3339 formatted timestamp. |
| 46 | + /// - `end_time`: A string representing the end of the time range. This can either be |
| 47 | + /// the keyword `"now"` (to represent the current time) or an RFC 3339 formatted timestamp. |
| 48 | + /// |
| 49 | + /// # Errors |
| 50 | + /// - `TimeParseError::StartTimeAfterEndTime`: Returned when the parsed start time is later than the end time. |
| 51 | + /// - Any error that might occur during parsing of durations or RFC 3339 timestamps. |
| 52 | + /// |
| 53 | + /// # Example |
| 54 | + /// ```ignore |
| 55 | + /// let range = TimeRange::parse_human_time("2 hours", "now"); |
| 56 | + /// let range = TimeRange::parse_human_time("2023-01-01T12:00:00Z", "2023-01-01T15:00:00Z"); |
| 57 | + /// ``` |
| 58 | + pub fn parse_human_time(start_time: &str, end_time: &str) -> Result<Self, TimeParseError> { |
| 59 | + let start: DateTime<Utc>; |
| 60 | + let end: DateTime<Utc>; |
| 61 | + |
| 62 | + if end_time == "now" { |
| 63 | + end = Utc::now(); |
| 64 | + start = end - chrono::Duration::from_std(humantime::parse_duration(start_time)?)?; |
| 65 | + } else { |
| 66 | + start = DateTime::parse_from_rfc3339(start_time)?.into(); |
| 67 | + end = DateTime::parse_from_rfc3339(end_time)?.into(); |
| 68 | + }; |
| 69 | + |
| 70 | + if start > end { |
| 71 | + return Err(TimeParseError::StartTimeAfterEndTime); |
| 72 | + } |
| 73 | + |
| 74 | + Ok(Self { start, end }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(test)] |
| 79 | +mod tests { |
| 80 | + use super::*; |
| 81 | + use chrono::{Duration, SecondsFormat, Utc}; |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn valid_rfc3339_timestamps() { |
| 85 | + let start_time = "2023-01-01T12:00:00Z"; |
| 86 | + let end_time = "2023-01-01T13:00:00Z"; |
| 87 | + |
| 88 | + let result = TimeRange::parse_human_time(start_time, end_time); |
| 89 | + let parsed = result.unwrap(); |
| 90 | + |
| 91 | + assert_eq!( |
| 92 | + parsed.start.to_rfc3339_opts(SecondsFormat::Secs, true), |
| 93 | + start_time |
| 94 | + ); |
| 95 | + assert_eq!( |
| 96 | + parsed.end.to_rfc3339_opts(SecondsFormat::Secs, true), |
| 97 | + end_time |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn end_time_now_with_valid_duration() { |
| 103 | + let start_time = "1h"; |
| 104 | + let end_time = "now"; |
| 105 | + |
| 106 | + let result = TimeRange::parse_human_time(start_time, end_time); |
| 107 | + let parsed = result.unwrap(); |
| 108 | + |
| 109 | + assert!(parsed.end <= Utc::now()); |
| 110 | + assert_eq!(parsed.end - parsed.start, Duration::hours(1)); |
| 111 | + |
| 112 | + let start_time = "30 minutes"; |
| 113 | + let end_time = "now"; |
| 114 | + |
| 115 | + let result = TimeRange::parse_human_time(start_time, end_time); |
| 116 | + let parsed = result.unwrap(); |
| 117 | + |
| 118 | + assert!(parsed.end <= Utc::now()); |
| 119 | + assert_eq!(parsed.end - parsed.start, Duration::minutes(30)); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn start_time_after_end_time() { |
| 124 | + let start_time = "2023-01-01T14:00:00Z"; |
| 125 | + let end_time = "2023-01-01T13:00:00Z"; |
| 126 | + |
| 127 | + let result = TimeRange::parse_human_time(start_time, end_time); |
| 128 | + assert!(matches!(result, Err(TimeParseError::StartTimeAfterEndTime))); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn invalid_start_time_format() { |
| 133 | + let start_time = "not-a-valid-time"; |
| 134 | + let end_time = "2023-01-01T13:00:00Z"; |
| 135 | + |
| 136 | + let result = TimeRange::parse_human_time(start_time, end_time); |
| 137 | + assert!(matches!(result, Err(TimeParseError::Chrono(_)))); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn invalid_end_time_format() { |
| 142 | + let start_time = "2023-01-01T12:00:00Z"; |
| 143 | + let end_time = "not-a-valid-time"; |
| 144 | + |
| 145 | + let result = TimeRange::parse_human_time(start_time, end_time); |
| 146 | + assert!(matches!(result, Err(TimeParseError::Chrono(_)))); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn invalid_duration_with_end_time_now() { |
| 151 | + let start_time = "not-a-duration"; |
| 152 | + let end_time = "now"; |
| 153 | + |
| 154 | + let result = TimeRange::parse_human_time(start_time, end_time); |
| 155 | + assert!(matches!(result, Err(TimeParseError::HumanTime(_)))); |
| 156 | + } |
| 157 | +} |
0 commit comments