| 1 |
// For the full copyright and license information, please view the LICENSE |
| 2 |
// file that was distributed with this source code. |
| 3 |
|
| 4 |
//! Parse a date item (without time component) |
| 5 |
//! |
| 6 |
//! The GNU docs say: |
| 7 |
//! |
| 8 |
//! > A calendar date item specifies a day of the year. It is specified |
| 9 |
//! > differently, depending on whether the month is specified numerically |
| 10 |
//! > or literally. |
| 11 |
//! > |
| 12 |
//! > ... |
| 13 |
//! > |
| 14 |
//! > For numeric months, the ISO 8601 format ‘year-month-day’ is allowed, |
| 15 |
//! > where year is any positive number, month is a number between 01 and |
| 16 |
//! > 12, and day is a number between 01 and 31. A leading zero must be |
| 17 |
//! > present if a number is less than ten. If year is 68 or smaller, then |
| 18 |
//! > 2000 is added to it; otherwise, if year is less than 100, then 1900 |
| 19 |
//! > is added to it. The construct ‘month/day/year’, popular in the United |
| 20 |
//! > States, is accepted. Also ‘month/day’, omitting the year. |
| 21 |
//! > |
| 22 |
//! > Literal months may be spelled out in full: ‘January’, ‘February’, |
| 23 |
//! > ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, |
| 24 |
//! > ‘October’, ‘November’ or ‘December’. Literal months may be |
| 25 |
//! > abbreviated to their first three letters, possibly followed by an |
| 26 |
//! > abbreviating dot. It is also permitted to write ‘Sept’ instead of |
| 27 |
//! > ‘September’. |
| 28 |
|
| 29 |
use winnow::{ |
| 30 |
ascii::alpha1, |
| 31 |
combinator::{alt, eof, opt, preceded, terminated}, |
| 32 |
error::ErrMode, |
| 33 |
stream::AsChar, |
| 34 |
token::take_while, |
| 35 |
ModalResult, Parser, |
| 36 |
}; |
| 37 |
|
| 38 |
use super::{ |
| 39 |
primitive::{ctx_err, dec_uint, multispace1, s}, |
| 40 |
year::{year_from_str, year_str}, |
| 41 |
}; |
| 42 |
|
| 43 |
#[derive(PartialEq, Eq, Clone, Debug, Default)] |
| 44 |
pub(crate) struct Date { |
| 45 |
pub(crate) day: u8, |
| 46 |
pub(crate) month: u8, |
| 47 |
pub(crate) year: Option<u32>, |
| 48 |
} |
| 49 |
|
| 50 |
impl Date { |
| 51 |
pub(super) fn with_year(self, year: u32) -> Self { |
| 52 |
Date { |
| 53 |
day: self.day, |
| 54 |
month: self.month, |
| 55 |
year: Some(year), |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl TryFrom<(&str, u8, u8)> for Date { |
| 61 |
type Error = &'static str; |
| 62 |
|
| 63 |
/// Create a `Date` from a tuple of `(year, month, day)`. |
| 64 |
/// |
| 65 |
/// Note: The `year` is represented as a `&str` to handle a specific GNU |
| 66 |
/// compatibility quirk. See the comment in [`year`](super::year) for more |
| 67 |
/// details. |
| 68 |
fn try_from(value: (&str, u8, u8)) -> Result<Self, Self::Error> { |
| 69 |
let (year_str, month, day) = value; |
| 70 |
let year = year_from_str(year_str)?; |
| 71 |
|
| 72 |
if !(1..=12).contains(&month) { |
| 73 |
return Err("month must be between 1 and 12"); |
| 74 |
} |
| 75 |
|
| 76 |
let is_leap_year = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); |
| 77 |
|
| 78 |
if !(1..=31).contains(&day) |
| 79 |
|| (month == 2 && day > (if is_leap_year { 29 } else { 28 })) |
| 80 |
|| ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) |
| 81 |
{ |
| 82 |
return Err("day is not valid for the given month"); |
| 83 |
} |
| 84 |
|
| 85 |
Ok(Date { |
| 86 |
day, |
| 87 |
month, |
| 88 |
year: Some(year), |
| 89 |
}) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
impl TryFrom<(u8, u8)> for Date { |
| 94 |
type Error = &'static str; |
| 95 |
|
| 96 |
/// Create a `Date` from a tuple of `(month, day)`. |
| 97 |
fn try_from((month, day): (u8, u8)) -> Result<Self, Self::Error> { |
| 98 |
if !(1..=12).contains(&month) { |
| 99 |
return Err("month must be between 1 and 12"); |
| 100 |
} |
| 101 |
|
| 102 |
if !(1..=31).contains(&day) |
| 103 |
|| (month == 2 && day > 29) |
| 104 |
|| ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) |
| 105 |
{ |
| 106 |
return Err("day is not valid for the given month"); |
| 107 |
} |
| 108 |
|
| 109 |
Ok(Date { |
| 110 |
day, |
| 111 |
month, |
| 112 |
year: None, |
| 113 |
}) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
impl TryFrom<Date> for jiff::civil::Date { |
| 118 |
type Error = &'static str; |
| 119 |
|
| 120 |
fn try_from(date: Date) -> Result<Self, Self::Error> { |
| 121 |
let year = date.year.unwrap_or(0); |
| 122 |
let year: i16 = year |
| 123 |
.try_into() |
| 124 |
.map_err(|_| "date year is outside the supported range")?; |
| 125 |
jiff::civil::Date::new(year, date.month as i8, date.day as i8) |
| 126 |
.map_err(|_| "date is not valid") |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
pub(super) fn parse(input: &mut &str) -> ModalResult<Date> { |
| 131 |
alt((iso1, iso2, us, literal1, literal2, literal3)).parse_next(input) |
| 132 |
} |
| 133 |
|
| 134 |
/// Parse `[year]-[month]-[day]` |
| 135 |
/// |
| 136 |
/// This is also used by [`combined`](super::combined). |
| 137 |
pub(super) fn iso1(input: &mut &str) -> ModalResult<Date> { |
| 138 |
let (year, _, month, _, day) = |
| 139 |
(year_str, s('-'), s(dec_uint), s('-'), s(dec_uint)).parse_next(input)?; |
| 140 |
|
| 141 |
// Map err to Backtrack instead of Cut to avoid early termination of parsing |
| 142 |
(year, month, day) |
| 143 |
.try_into() |
| 144 |
.map_err(|e| ErrMode::Backtrack(ctx_err(e))) |
| 145 |
} |
| 146 |
|
| 147 |
/// Parse `[year][month][day]` |
| 148 |
/// |
| 149 |
/// This is also used by [`combined`](super::combined). |
| 150 |
pub(super) fn iso2(input: &mut &str) -> ModalResult<Date> { |
| 151 |
let date_str = take_while(5.., AsChar::is_dec_digit).parse_next(input)?; |
| 152 |
let len = date_str.len(); |
| 153 |
|
| 154 |
let year = &date_str[..len - 4]; |
| 155 |
let month = month_from_str(&date_str[len - 4..len - 2])?; |
| 156 |
let day = day_from_str(&date_str[len - 2..])?; |
| 157 |
|
| 158 |
(year, month, day) |
| 159 |
.try_into() |
| 160 |
.map_err(|e| ErrMode::Backtrack(ctx_err(e))) |
| 161 |
} |
| 162 |
|
| 163 |
/// Parse `[year]/[month]/[day]` or `[month]/[day]/[year]` or `[month]/[day]`. |
| 164 |
fn us(input: &mut &str) -> ModalResult<Date> { |
| 165 |
let (s1, _, n, s2) = ( |
| 166 |
s(take_while(1.., AsChar::is_dec_digit)), |
| 167 |
s('/'), |
| 168 |
s(dec_uint), |
| 169 |
opt(preceded(s('/'), s(take_while(1.., AsChar::is_dec_digit)))), |
| 170 |
) |
| 171 |
.parse_next(input)?; |
| 172 |
|
| 173 |
match s2 { |
| 174 |
Some(s2) if s1.len() >= 4 => { |
| 175 |
// [year]/[month]/[day] |
| 176 |
// |
| 177 |
// GNU quirk: interpret as [year]/[month]/[day] if the first part is at |
| 178 |
// least 4 characters long. |
| 179 |
let day = day_from_str(s2)?; |
| 180 |
(s1, n, day) |
| 181 |
.try_into() |
| 182 |
.map_err(|e| ErrMode::Backtrack(ctx_err(e))) |
| 183 |
} |
| 184 |
Some(s2) => { |
| 185 |
// [month]/[day]/[year] |
| 186 |
let month = month_from_str(s1)?; |
| 187 |
(s2, month, n) |
| 188 |
.try_into() |
| 189 |
.map_err(|e| ErrMode::Backtrack(ctx_err(e))) |
| 190 |
} |
| 191 |
None => { |
| 192 |
// [month]/[day] |
| 193 |
let month = month_from_str(s1)?; |
| 194 |
(month, n) |
| 195 |
.try_into() |
| 196 |
.map_err(|e| ErrMode::Backtrack(ctx_err(e))) |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/// Parse `14 November 2022`, `14 Nov 2022`, "14nov2022", "14-nov-2022", |
| 202 |
/// "14-nov2022", "14nov-2022". |
| 203 |
fn literal1(input: &mut &str) -> ModalResult<Date> { |
| 204 |
let (day, _, month, year) = ( |
| 205 |
s(dec_uint), |
| 206 |
opt(s('-')), |
| 207 |
s(literal_month), |
| 208 |
opt(terminated( |
| 209 |
preceded(opt(s('-')), year_str), |
| 210 |
// The year must be followed by a space or end of input. |
| 211 |
alt((multispace1, eof)), |
| 212 |
)), |
| 213 |
) |
| 214 |
.parse_next(input)?; |
| 215 |
|
| 216 |
match year { |
| 217 |
Some(year) => (year, month, day) |
| 218 |
.try_into() |
| 219 |
.map_err(|e| ErrMode::Backtrack(ctx_err(e))), |
| 220 |
None => (month, day) |
| 221 |
.try_into() |
| 222 |
.map_err(|e| ErrMode::Backtrack(ctx_err(e))), |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/// Parse `November 14, 2022`, `Nov 14, 2022`, and `Nov 14 2022`. |
| 227 |
fn literal2(input: &mut &str) -> ModalResult<Date> { |
| 228 |
let (month, day, year) = ( |
| 229 |
s(literal_month), |
| 230 |
s(dec_uint), |
| 231 |
opt(terminated( |
| 232 |
preceded( |
| 233 |
// GNU quirk: for formats like `Nov 14, 2022`, there must be some |
| 234 |
// space between the comma and the year. This is probably to |
| 235 |
// distinguish with floats. |
| 236 |
opt(s(terminated(',', multispace1))), |
| 237 |
year_str, |
| 238 |
), |
| 239 |
// The year must be followed by a space or end of input. |
| 240 |
alt((multispace1, eof)), |
| 241 |
)), |
| 242 |
) |
| 243 |
.parse_next(input)?; |
| 244 |
|
| 245 |
match year { |
| 246 |
Some(year) => (year, month, day) |
| 247 |
.try_into() |
| 248 |
.map_err(|e| ErrMode::Backtrack(ctx_err(e))), |
| 249 |
None => (month, day) |
| 250 |
.try_into() |
| 251 |
.map_err(|e| ErrMode::Backtrack(ctx_err(e))), |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/// Parse `November-14-2022` and `Nov-14-2022`. Unlike `literal2`, the year is mandatory |
| 256 |
/// to match the behavior of GNU `date`. |
| 257 |
fn literal3(input: &mut &str) -> ModalResult<Date> { |
| 258 |
let (month, _, day, _, year) = |
| 259 |
(s(literal_month), s('-'), s(dec_uint), s('-'), year_str).parse_next(input)?; |
| 260 |
|
| 261 |
// Map err to Backtrack instead of Cut to avoid early termination of parsing |
| 262 |
(year, month, day) |
| 263 |
.try_into() |
| 264 |
.map_err(|e| ErrMode::Backtrack(ctx_err(e))) |
| 265 |
} |
| 266 |
|
| 267 |
/// Parse the name of a month (case-insensitive) |
| 268 |
fn literal_month(input: &mut &str) -> ModalResult<u8> { |
| 269 |
s(alpha1) |
| 270 |
.verify_map(|s: &str| { |
| 271 |
Some(match s { |
| 272 |
"january" | "jan" => 1, |
| 273 |
"february" | "feb" => 2, |
| 274 |
"march" | "mar" => 3, |
| 275 |
"april" | "apr" => 4, |
| 276 |
"may" => 5, |
| 277 |
"june" | "jun" => 6, |
| 278 |
"july" | "jul" => 7, |
| 279 |
"august" | "aug" => 8, |
| 280 |
"september" | "sep" | "sept" => 9, |
| 281 |
"october" | "oct" => 10, |
| 282 |
"november" | "nov" => 11, |
| 283 |
"december" | "dec" => 12, |
| 284 |
_ => return None, |
| 285 |
}) |
| 286 |
}) |
| 287 |
.parse_next(input) |
| 288 |
} |
| 289 |
|
| 290 |
fn month_from_str(s: &str) -> ModalResult<u8> { |
| 291 |
s.parse::<u8>() |
| 292 |
.map_err(|_| ErrMode::Backtrack(ctx_err("month must be a valid u8 number"))) |
| 293 |
} |
| 294 |
|
| 295 |
fn day_from_str(s: &str) -> ModalResult<u8> { |
| 296 |
s.parse::<u8>() |
| 297 |
.map_err(|_| ErrMode::Backtrack(ctx_err("day must be a valid u8 number"))) |
| 298 |
} |
| 299 |
|
| 300 |
#[cfg(test)] |
| 301 |
mod tests { |
| 302 |
use super::{parse, Date}; |
| 303 |
|
| 304 |
// Test cases from the GNU docs: |
| 305 |
// |
| 306 |
// ``` |
| 307 |
// 2022-11-14 # ISO 8601. |
| 308 |
// 22-11-14 # Assume 19xx for 69 through 99, |
| 309 |
// # 20xx for 00 through 68 (not recommended). |
| 310 |
// 11/14/2022 # Common U.S. writing. |
| 311 |
// 14 November 2022 |
| 312 |
// 14 Nov 2022 # Three-letter abbreviations always allowed. |
| 313 |
// November 14, 2022 |
| 314 |
// 14-nov-2022 |
| 315 |
// 14nov2022 |
| 316 |
// ``` |
| 317 |
|
| 318 |
#[test] |
| 319 |
fn iso1() { |
| 320 |
let reference = Date { |
| 321 |
year: Some(1), |
| 322 |
month: 2, |
| 323 |
day: 3, |
| 324 |
}; |
| 325 |
|
| 326 |
for mut s in ["1-2-3", "1 - 2 - 3", "1-02-03", "1-002-003", "001-02-03"] { |
| 327 |
let old_s = s.to_owned(); |
| 328 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 329 |
} |
| 330 |
|
| 331 |
// GNU quirk: when year string is 2 characters long and year is 68 or |
| 332 |
// smaller, 2000 is added to it. |
| 333 |
let reference = Date { |
| 334 |
year: Some(2001), |
| 335 |
month: 2, |
| 336 |
day: 3, |
| 337 |
}; |
| 338 |
|
| 339 |
for mut s in ["01-2-3", "01-02-03"] { |
| 340 |
let old_s = s.to_owned(); |
| 341 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 342 |
} |
| 343 |
|
| 344 |
// GNU quirk: when year string is 2 characters long and year is less |
| 345 |
// than 100, 1900 is added to it. |
| 346 |
let reference = Date { |
| 347 |
year: Some(1970), |
| 348 |
month: 2, |
| 349 |
day: 3, |
| 350 |
}; |
| 351 |
|
| 352 |
for mut s in ["70-2-3", "70-02-03"] { |
| 353 |
let old_s = s.to_owned(); |
| 354 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 355 |
} |
| 356 |
|
| 357 |
for mut s in ["01-00-01", "01-13-01", "01-01-32", "01-02-29", "01-04-31"] { |
| 358 |
let old_s = s.to_owned(); |
| 359 |
assert!(parse(&mut s).is_err(), "Format string: {old_s}"); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
#[test] |
| 364 |
fn iso2() { |
| 365 |
let reference = Date { |
| 366 |
year: Some(1), |
| 367 |
month: 2, |
| 368 |
day: 3, |
| 369 |
}; |
| 370 |
|
| 371 |
for mut s in ["10203", "0010203", "00010203", "000010203"] { |
| 372 |
let old_s = s.to_owned(); |
| 373 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 374 |
} |
| 375 |
|
| 376 |
// GNU quirk: when year string is 2 characters long and year is 68 or |
| 377 |
// smaller, 2000 is added to it. |
| 378 |
let reference = Date { |
| 379 |
year: Some(2001), |
| 380 |
month: 2, |
| 381 |
day: 3, |
| 382 |
}; |
| 383 |
|
| 384 |
let mut s = "010203"; |
| 385 |
let old_s = s.to_owned(); |
| 386 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 387 |
|
| 388 |
// GNU quirk: when year string is 2 characters long and year is less |
| 389 |
// than 100, 1900 is added to it. |
| 390 |
let reference = Date { |
| 391 |
year: Some(1970), |
| 392 |
month: 2, |
| 393 |
day: 3, |
| 394 |
}; |
| 395 |
|
| 396 |
let mut s = "700203"; |
| 397 |
let old_s = s.to_owned(); |
| 398 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 399 |
|
| 400 |
for mut s in ["010001", "011301", "010132", "010229", "010431"] { |
| 401 |
let old_s = s.to_owned(); |
| 402 |
assert!(parse(&mut s).is_err(), "Format string: {old_s}"); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
#[test] |
| 407 |
fn us() { |
| 408 |
let reference = Date { |
| 409 |
year: Some(1), |
| 410 |
month: 2, |
| 411 |
day: 3, |
| 412 |
}; |
| 413 |
|
| 414 |
for mut s in ["2/3/1", "2 / 3 / 1", "02/03/ 001", "0001/2/3"] { |
| 415 |
let old_s = s.to_owned(); |
| 416 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 417 |
} |
| 418 |
|
| 419 |
let reference = Date { |
| 420 |
year: None, |
| 421 |
month: 2, |
| 422 |
day: 3, |
| 423 |
}; |
| 424 |
|
| 425 |
for mut s in ["2/3", "2 / 3"] { |
| 426 |
let old_s = s.to_owned(); |
| 427 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 428 |
} |
| 429 |
|
| 430 |
// GNU quirk: when year string is 2 characters long and year is 68 or |
| 431 |
// smaller, 2000 is added to it. |
| 432 |
let reference = Date { |
| 433 |
year: Some(2001), |
| 434 |
month: 2, |
| 435 |
day: 3, |
| 436 |
}; |
| 437 |
|
| 438 |
let mut s = "2/3/01"; |
| 439 |
let old_s = s.to_owned(); |
| 440 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 441 |
|
| 442 |
// GNU quirk: when year string is 2 characters long and year is less |
| 443 |
// than 100, 1900 is added to it. |
| 444 |
let reference = Date { |
| 445 |
year: Some(1970), |
| 446 |
month: 2, |
| 447 |
day: 3, |
| 448 |
}; |
| 449 |
|
| 450 |
let mut s = "2/3/70"; |
| 451 |
let old_s = s.to_owned(); |
| 452 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 453 |
|
| 454 |
for mut s in ["00/01/01", "13/01/01", "01/32/01", "02/30/01", "04/31/01"] { |
| 455 |
let old_s = s.to_owned(); |
| 456 |
assert!(parse(&mut s).is_err(), "Format string: {old_s}"); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
#[test] |
| 461 |
fn literal1() { |
| 462 |
let reference = Date { |
| 463 |
year: Some(2022), |
| 464 |
month: 11, |
| 465 |
day: 14, |
| 466 |
}; |
| 467 |
|
| 468 |
for mut s in [ |
| 469 |
"14 november 2022", |
| 470 |
"14 nov 2022", |
| 471 |
"14-nov-2022", |
| 472 |
"14-nov2022", |
| 473 |
"14nov2022", |
| 474 |
"14nov 2022", |
| 475 |
] { |
| 476 |
let old_s = s.to_owned(); |
| 477 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 478 |
} |
| 479 |
|
| 480 |
let reference = Date { |
| 481 |
year: None, |
| 482 |
month: 11, |
| 483 |
day: 14, |
| 484 |
}; |
| 485 |
|
| 486 |
for mut s in ["14 november", "14 nov", "14-nov", "14nov"] { |
| 487 |
let old_s = s.to_owned(); |
| 488 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 489 |
} |
| 490 |
|
| 491 |
let reference = Date { |
| 492 |
year: None, |
| 493 |
month: 11, |
| 494 |
day: 14, |
| 495 |
}; |
| 496 |
|
| 497 |
// Year must be followed by a space or end of input. |
| 498 |
let mut s = "14 nov 2022a"; |
| 499 |
let old_s = s.to_owned(); |
| 500 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 501 |
assert_eq!(s, " 2022a"); |
| 502 |
|
| 503 |
let mut s = "14 nov-2022a"; |
| 504 |
let old_s = s.to_owned(); |
| 505 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 506 |
assert_eq!(s, "-2022a"); |
| 507 |
} |
| 508 |
|
| 509 |
#[test] |
| 510 |
fn literal2() { |
| 511 |
let reference = Date { |
| 512 |
year: Some(2022), |
| 513 |
month: 11, |
| 514 |
day: 14, |
| 515 |
}; |
| 516 |
|
| 517 |
for mut s in [ |
| 518 |
"november 14 2022", |
| 519 |
"november 14, 2022", |
| 520 |
"november 14 , 2022", |
| 521 |
"nov 14 2022", |
| 522 |
"nov14 2022", |
| 523 |
"nov14, 2022", |
| 524 |
] { |
| 525 |
let old_s = s.to_owned(); |
| 526 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 527 |
} |
| 528 |
|
| 529 |
let reference = Date { |
| 530 |
year: None, |
| 531 |
month: 11, |
| 532 |
day: 14, |
| 533 |
}; |
| 534 |
|
| 535 |
for mut s in ["november 14", "nov 14", "nov14"] { |
| 536 |
let old_s = s.to_owned(); |
| 537 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 538 |
} |
| 539 |
|
| 540 |
let reference = Date { |
| 541 |
year: None, |
| 542 |
month: 11, |
| 543 |
day: 14, |
| 544 |
}; |
| 545 |
|
| 546 |
// There must be some space between the comma and the year. |
| 547 |
let mut s = "november 14,2022"; |
| 548 |
let old_s = s.to_owned(); |
| 549 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 550 |
assert_eq!(s, ",2022"); |
| 551 |
|
| 552 |
// Year must be followed by a space or end of input. |
| 553 |
let mut s = "november 14 2022a"; |
| 554 |
let old_s = s.to_owned(); |
| 555 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 556 |
assert_eq!(s, " 2022a"); |
| 557 |
|
| 558 |
let mut s = "november 14, 2022a"; |
| 559 |
let old_s = s.to_owned(); |
| 560 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 561 |
assert_eq!(s, ", 2022a"); |
| 562 |
} |
| 563 |
|
| 564 |
#[test] |
| 565 |
fn literal3() { |
| 566 |
let reference = Date { |
| 567 |
year: Some(2022), |
| 568 |
month: 11, |
| 569 |
day: 14, |
| 570 |
}; |
| 571 |
|
| 572 |
for mut s in [ |
| 573 |
"november-14-2022", |
| 574 |
"november----14----2022", |
| 575 |
"november - 14 - 2022", |
| 576 |
"nov-14-2022", |
| 577 |
"nov---14---2022", |
| 578 |
"nov - 14 - 2022", |
| 579 |
] { |
| 580 |
let old_s = s.to_owned(); |
| 581 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
#[test] |
| 586 |
fn with_year() { |
| 587 |
let reference = Date { |
| 588 |
year: Some(2022), |
| 589 |
month: 11, |
| 590 |
day: 14, |
| 591 |
}; |
| 592 |
|
| 593 |
for mut s in [ |
| 594 |
"2022-11-14", |
| 595 |
"2022 - 11 - 14", |
| 596 |
"22-11-14", |
| 597 |
"2022---11----14", |
| 598 |
"22(comment 1)-11(comment 2)-14", |
| 599 |
"11/14/2022", |
| 600 |
"11--/14--/2022", |
| 601 |
"11(comment 1)/(comment 2)14(comment 3)/(comment 4)2022", |
| 602 |
"11 / 14 / 2022", |
| 603 |
"11/14/22", |
| 604 |
"14 november 2022", |
| 605 |
"14 nov 2022", |
| 606 |
"november 14, 2022", |
| 607 |
"november 14 , 2022", |
| 608 |
"nov 14, 2022", |
| 609 |
"14-nov-2022", |
| 610 |
"14nov2022", |
| 611 |
"14nov 2022", |
| 612 |
] { |
| 613 |
let old_s = s.to_owned(); |
| 614 |
assert_eq!(parse(&mut s).unwrap(), reference, "Format string: {old_s}"); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
#[test] |
| 619 |
fn no_year() { |
| 620 |
let reference = Date { |
| 621 |
year: None, |
| 622 |
month: 11, |
| 623 |
day: 14, |
| 624 |
}; |
| 625 |
for mut s in [ |
| 626 |
"11/14", |
| 627 |
"14 november", |
| 628 |
"14 nov", |
| 629 |
"14(comment!)nov", |
| 630 |
"november 14", |
| 631 |
"november(comment!)14", |
| 632 |
"nov 14", |
| 633 |
"14-nov", |
| 634 |
"14nov", |
| 635 |
"14(comment????)nov", |
| 636 |
] { |
| 637 |
assert_eq!(parse(&mut s).unwrap(), reference); |
| 638 |
} |
| 639 |
} |
| 640 |
} |