| 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 year from a string. |
| 5 |
//! |
| 6 |
//! The year must be parsed to a string first, this is to handle a specific GNU |
| 7 |
//! compatibility quirk. According to the GNU documentation: "if the year is 68 |
| 8 |
//! or smaller, then 2000 is added to it; otherwise, if year is less than 100, |
| 9 |
//! then 1900 is added to it." This adjustment only applies to two-digit year |
| 10 |
//! strings. For example, `"00"` is interpreted as `2000`, whereas `"0"`, |
| 11 |
//! `"000"`, or `"0000"` are interpreted as `0`. |
| 12 |
|
| 13 |
use winnow::{stream::AsChar, token::take_while, ModalResult, Parser}; |
| 14 |
|
| 15 |
use super::primitive::s; |
| 16 |
|
| 17 |
const GNU_MAX_YEAR: u32 = 2_147_485_547; |
| 18 |
// TODO: Leverage `TryFrom` trait. |
| 19 |
pub(super) fn year_from_str(year_str: &str) -> Result<u32, &'static str> { |
| 20 |
let mut year = year_str |
| 21 |
.parse::<u32>() |
| 22 |
.map_err(|_| "year must be a non-negative integer")?; |
| 23 |
|
| 24 |
// If year is 68 or smaller, then 2000 is added to it; otherwise, if year |
| 25 |
// is less than 100, then 1900 is added to it. |
| 26 |
// |
| 27 |
// GNU quirk: this only applies to two-digit years. For example, |
| 28 |
// "98-01-01" will be parsed as "1998-01-01", whereas "098-01-01" will be |
| 29 |
// parsed as "0098-01-01". |
| 30 |
if year_str.len() == 2 { |
| 31 |
if year <= 68 { |
| 32 |
year += 2000 |
| 33 |
} else { |
| 34 |
year += 1900 |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
if year > GNU_MAX_YEAR { |
| 39 |
return Err("year exceeds GNU maximum"); |
| 40 |
} |
| 41 |
|
| 42 |
Ok(year) |
| 43 |
} |
| 44 |
|
| 45 |
pub(super) fn year_str<'a>(input: &mut &'a str) -> ModalResult<&'a str> { |
| 46 |
s(take_while(1.., AsChar::is_dec_digit)).parse_next(input) |
| 47 |
} |
| 48 |
|
| 49 |
#[cfg(test)] |
| 50 |
mod tests { |
| 51 |
use super::year_from_str; |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn test_year() { |
| 55 |
// 2-characters are converted to 19XX/20XX |
| 56 |
assert_eq!(year_from_str("10").unwrap(), 2010u32); |
| 57 |
assert_eq!(year_from_str("68").unwrap(), 2068u32); |
| 58 |
assert_eq!(year_from_str("69").unwrap(), 1969u32); |
| 59 |
assert_eq!(year_from_str("99").unwrap(), 1999u32); |
| 60 |
|
| 61 |
// 3,4-characters are converted verbatim |
| 62 |
assert_eq!(year_from_str("468").unwrap(), 468u32); |
| 63 |
assert_eq!(year_from_str("469").unwrap(), 469u32); |
| 64 |
assert_eq!(year_from_str("1568").unwrap(), 1568u32); |
| 65 |
assert_eq!(year_from_str("1569").unwrap(), 1569u32); |
| 66 |
|
| 67 |
// very large years are accepted up to GNU's upper bound |
| 68 |
assert_eq!(year_from_str("10000").unwrap(), 10000u32); |
| 69 |
assert_eq!(year_from_str("2147485547").unwrap(), 2_147_485_547u32); |
| 70 |
assert_eq!( |
| 71 |
year_from_str("2147485548").unwrap_err(), |
| 72 |
"year exceeds GNU maximum" |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_year_errors() { |
| 78 |
assert_eq!( |
| 79 |
year_from_str("not-a-year").unwrap_err(), |
| 80 |
"year must be a non-negative integer" |
| 81 |
); |
| 82 |
} |
| 83 |
} |