| 1 |
use chrono::{Duration, NaiveDate, NaiveDateTime, NaiveTime}; |
| 2 |
use once_cell::sync::Lazy; |
| 3 |
use regex::Regex; |
| 4 |
use std::fmt::Write; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
pub fn increment(selected_text: &str, amount: i64) -> Option<String> { |
| 11 |
if selected_text.is_empty() { |
| 12 |
return None; |
| 13 |
} |
| 14 |
|
| 15 |
FORMATS.iter().find_map(|format| { |
| 16 |
let captures = format.regex.captures(selected_text)?; |
| 17 |
if captures.len() - 1 != format.fields.len() { |
| 18 |
return None; |
| 19 |
} |
| 20 |
|
| 21 |
let date_time = captures.get(0)?; |
| 22 |
let has_date = format.fields.iter().any(|f| f.unit.is_date()); |
| 23 |
let has_time = format.fields.iter().any(|f| f.unit.is_time()); |
| 24 |
let date_time = &selected_text[date_time.start()..date_time.end()]; |
| 25 |
match (has_date, has_time) { |
| 26 |
(true, true) => { |
| 27 |
let date_time = NaiveDateTime::parse_from_str(date_time, format.fmt).ok()?; |
| 28 |
Some( |
| 29 |
date_time |
| 30 |
.checked_add_signed(Duration::try_minutes(amount)?)? |
| 31 |
.format(format.fmt) |
| 32 |
.to_string(), |
| 33 |
) |
| 34 |
} |
| 35 |
(true, false) => { |
| 36 |
let date = NaiveDate::parse_from_str(date_time, format.fmt).ok()?; |
| 37 |
Some( |
| 38 |
date.checked_add_signed(Duration::try_days(amount)?)? |
| 39 |
.format(format.fmt) |
| 40 |
.to_string(), |
| 41 |
) |
| 42 |
} |
| 43 |
(false, true) => { |
| 44 |
let time = NaiveTime::parse_from_str(date_time, format.fmt).ok()?; |
| 45 |
let (adjusted_time, _) = |
| 46 |
time.overflowing_add_signed(Duration::try_minutes(amount)?); |
| 47 |
Some(adjusted_time.format(format.fmt).to_string()) |
| 48 |
} |
| 49 |
(false, false) => None, |
| 50 |
} |
| 51 |
}) |
| 52 |
} |
| 53 |
|
| 54 |
static FORMATS: Lazy<Vec<Format>> = Lazy::new(|| { |
| 55 |
vec![ |
| 56 |
Format::new("%Y-%m-%d %H:%M:%S"), |
| 57 |
Format::new("%Y/%m/%d %H:%M:%S"), |
| 58 |
Format::new("%Y-%m-%d %H:%M"), |
| 59 |
Format::new("%Y/%m/%d %H:%M"), |
| 60 |
Format::new("%Y-%m-%d"), |
| 61 |
Format::new("%Y/%m/%d"), |
| 62 |
Format::new("%a %b %d %Y"), |
| 63 |
Format::new("%d-%b-%Y"), |
| 64 |
Format::new("%Y %b %d"), |
| 65 |
Format::new("%b %d, %Y"), |
| 66 |
Format::new("%-I:%M:%S %P"), |
| 67 |
Format::new("%-I:%M %P"), |
| 68 |
Format::new("%-I:%M:%S %p"), |
| 69 |
Format::new("%-I:%M %p"), |
| 70 |
Format::new("%H:%M:%S"), |
| 71 |
Format::new("%H:%M"), |
| 72 |
] |
| 73 |
}); |
| 74 |
|
| 75 |
#[derive(Debug)] |
| 76 |
struct Format { |
| 77 |
fmt: &'static str, |
| 78 |
fields: Vec<DateField>, |
| 79 |
regex: Regex, |
| 80 |
max_len: usize, |
| 81 |
} |
| 82 |
|
| 83 |
impl Format { |
| 84 |
fn new(fmt: &'static str) -> Self { |
| 85 |
let mut remaining = fmt; |
| 86 |
let mut fields = Vec::new(); |
| 87 |
let mut regex = "^".to_string(); |
| 88 |
let mut max_len = 0; |
| 89 |
|
| 90 |
while let Some(i) = remaining.find('%') { |
| 91 |
let after = &remaining[i + 1..]; |
| 92 |
let mut chars = after.chars(); |
| 93 |
let c = chars.next().unwrap(); |
| 94 |
|
| 95 |
let spec_len = if c == '-' { |
| 96 |
1 + chars.next().unwrap().len_utf8() |
| 97 |
} else { |
| 98 |
c.len_utf8() |
| 99 |
}; |
| 100 |
|
| 101 |
let specifier = &after[..spec_len]; |
| 102 |
let field = DateField::from_specifier(specifier).unwrap(); |
| 103 |
fields.push(field); |
| 104 |
max_len += field.max_len + remaining[..i].len(); |
| 105 |
regex += &remaining[..i]; |
| 106 |
write!(regex, "({})", field.regex).unwrap(); |
| 107 |
remaining = &after[spec_len..]; |
| 108 |
} |
| 109 |
regex += "$"; |
| 110 |
|
| 111 |
let regex = Regex::new(®ex).unwrap(); |
| 112 |
|
| 113 |
Self { |
| 114 |
fmt, |
| 115 |
fields, |
| 116 |
regex, |
| 117 |
max_len, |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
impl PartialEq for Format { |
| 123 |
fn eq(&self, other: &Self) -> bool { |
| 124 |
self.fmt == other.fmt && self.fields == other.fields && self.max_len == other.max_len |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
impl Eq for Format {} |
| 129 |
|
| 130 |
#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 131 |
struct DateField { |
| 132 |
regex: &'static str, |
| 133 |
unit: DateUnit, |
| 134 |
max_len: usize, |
| 135 |
} |
| 136 |
|
| 137 |
impl DateField { |
| 138 |
fn from_specifier(specifier: &str) -> Option<Self> { |
| 139 |
match specifier { |
| 140 |
"Y" => Some(Self { |
| 141 |
regex: r"\d{4}", |
| 142 |
unit: DateUnit::Years, |
| 143 |
max_len: 5, |
| 144 |
}), |
| 145 |
"y" => Some(Self { |
| 146 |
regex: r"\d\d", |
| 147 |
unit: DateUnit::Years, |
| 148 |
max_len: 2, |
| 149 |
}), |
| 150 |
"m" => Some(Self { |
| 151 |
regex: r"[0-1]\d", |
| 152 |
unit: DateUnit::Months, |
| 153 |
max_len: 2, |
| 154 |
}), |
| 155 |
"d" => Some(Self { |
| 156 |
regex: r"[0-3]\d", |
| 157 |
unit: DateUnit::Days, |
| 158 |
max_len: 2, |
| 159 |
}), |
| 160 |
"-d" => Some(Self { |
| 161 |
regex: r"[1-3]?\d", |
| 162 |
unit: DateUnit::Days, |
| 163 |
max_len: 2, |
| 164 |
}), |
| 165 |
"a" => Some(Self { |
| 166 |
regex: r"Sun|Mon|Tue|Wed|Thu|Fri|Sat", |
| 167 |
unit: DateUnit::Days, |
| 168 |
max_len: 3, |
| 169 |
}), |
| 170 |
"A" => Some(Self { |
| 171 |
regex: r"Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday", |
| 172 |
unit: DateUnit::Days, |
| 173 |
max_len: 9, |
| 174 |
}), |
| 175 |
"b" | "h" => Some(Self { |
| 176 |
regex: r"Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", |
| 177 |
unit: DateUnit::Months, |
| 178 |
max_len: 3, |
| 179 |
}), |
| 180 |
"B" => Some(Self { |
| 181 |
regex: r"January|February|March|April|May|June|July|August|September|October|November|December", |
| 182 |
unit: DateUnit::Months, |
| 183 |
max_len: 9, |
| 184 |
}), |
| 185 |
"H" => Some(Self { |
| 186 |
regex: r"[0-2]\d", |
| 187 |
unit: DateUnit::Hours, |
| 188 |
max_len: 2, |
| 189 |
}), |
| 190 |
"M" => Some(Self { |
| 191 |
regex: r"[0-5]\d", |
| 192 |
unit: DateUnit::Minutes, |
| 193 |
max_len: 2, |
| 194 |
}), |
| 195 |
"S" => Some(Self { |
| 196 |
regex: r"[0-5]\d", |
| 197 |
unit: DateUnit::Seconds, |
| 198 |
max_len: 2, |
| 199 |
}), |
| 200 |
"I" => Some(Self { |
| 201 |
regex: r"[0-1]\d", |
| 202 |
unit: DateUnit::Hours, |
| 203 |
max_len: 2, |
| 204 |
}), |
| 205 |
"-I" => Some(Self { |
| 206 |
regex: r"1?\d", |
| 207 |
unit: DateUnit::Hours, |
| 208 |
max_len: 2, |
| 209 |
}), |
| 210 |
"P" => Some(Self { |
| 211 |
regex: r"am|pm", |
| 212 |
unit: DateUnit::AmPm, |
| 213 |
max_len: 2, |
| 214 |
}), |
| 215 |
"p" => Some(Self { |
| 216 |
regex: r"AM|PM", |
| 217 |
unit: DateUnit::AmPm, |
| 218 |
max_len: 2, |
| 219 |
}), |
| 220 |
_ => None, |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
#[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 226 |
enum DateUnit { |
| 227 |
Years, |
| 228 |
Months, |
| 229 |
Days, |
| 230 |
Hours, |
| 231 |
Minutes, |
| 232 |
Seconds, |
| 233 |
AmPm, |
| 234 |
} |
| 235 |
|
| 236 |
impl DateUnit { |
| 237 |
fn is_date(self) -> bool { |
| 238 |
matches!(self, DateUnit::Years | DateUnit::Months | DateUnit::Days) |
| 239 |
} |
| 240 |
|
| 241 |
fn is_time(self) -> bool { |
| 242 |
matches!( |
| 243 |
self, |
| 244 |
DateUnit::Hours | DateUnit::Minutes | DateUnit::Seconds |
| 245 |
) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
#[cfg(test)] |
| 250 |
mod test { |
| 251 |
use super::*; |
| 252 |
|
| 253 |
#[test] |
| 254 |
fn test_increment_date_times() { |
| 255 |
let tests = [ |
| 256 |
|
| 257 |
("2020-02-28", 1, "2020-02-29"), |
| 258 |
("2020-02-29", 1, "2020-03-01"), |
| 259 |
("2020-01-31", 1, "2020-02-01"), |
| 260 |
("2020-01-20", 1, "2020-01-21"), |
| 261 |
("2021-01-01", -1, "2020-12-31"), |
| 262 |
("2021-01-31", -2, "2021-01-29"), |
| 263 |
("2020-02-28", 1, "2020-02-29"), |
| 264 |
("2021-02-28", 1, "2021-03-01"), |
| 265 |
("2021-03-01", -1, "2021-02-28"), |
| 266 |
("2020-02-29", -1, "2020-02-28"), |
| 267 |
("2020-02-20", -1, "2020-02-19"), |
| 268 |
("2021-03-01", -1, "2021-02-28"), |
| 269 |
("1980/12/21", 100, "1981/03/31"), |
| 270 |
("1980/12/21", -100, "1980/09/12"), |
| 271 |
("1980/12/21", 1000, "1983/09/17"), |
| 272 |
("1980/12/21", -1000, "1978/03/27"), |
| 273 |
("2021-11-24 07:12:23", 1, "2021-11-24 07:13:23"), |
| 274 |
("2021-11-24 07:12", 1, "2021-11-24 07:13"), |
| 275 |
("Wed Nov 24 2021", 1, "Thu Nov 25 2021"), |
| 276 |
("24-Nov-2021", 1, "25-Nov-2021"), |
| 277 |
("2021 Nov 24", 1, "2021 Nov 25"), |
| 278 |
("Nov 24, 2021", 1, "Nov 25, 2021"), |
| 279 |
("7:21:53 am", 1, "7:22:53 am"), |
| 280 |
("7:21:53 AM", 1, "7:22:53 AM"), |
| 281 |
("7:21 am", 1, "7:22 am"), |
| 282 |
("23:24:23", 1, "23:25:23"), |
| 283 |
("23:24", 1, "23:25"), |
| 284 |
("23:59", 1, "00:00"), |
| 285 |
("23:59:59", 1, "00:00:59"), |
| 286 |
]; |
| 287 |
|
| 288 |
for (original, amount, expected) in tests { |
| 289 |
assert_eq!(increment(original, amount).unwrap(), expected); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
#[test] |
| 294 |
fn test_invalid_date_times() { |
| 295 |
let tests = [ |
| 296 |
"0000-00-00", |
| 297 |
"1980-2-21", |
| 298 |
"1980-12-1", |
| 299 |
"12345", |
| 300 |
"2020-02-30", |
| 301 |
"1999-12-32", |
| 302 |
"19-12-32", |
| 303 |
"1-2-3", |
| 304 |
"0000/00/00", |
| 305 |
"1980/2/21", |
| 306 |
"1980/12/1", |
| 307 |
"12345", |
| 308 |
"2020/02/30", |
| 309 |
"1999/12/32", |
| 310 |
"19/12/32", |
| 311 |
"1/2/3", |
| 312 |
"123:456:789", |
| 313 |
"11:61", |
| 314 |
"2021-55-12 08:12:54", |
| 315 |
]; |
| 316 |
|
| 317 |
for invalid in tests { |
| 318 |
assert_eq!(increment(invalid, 1), None) |
| 319 |
} |
| 320 |
} |
| 321 |
} |