|
| 1 | +// For the full copyright and license information, please view the LICENSE |
| 2 | +// file that was distributed with this source code. |
| 3 | + |
| 4 | +use chrono::{DateTime, Local}; |
| 5 | +use parse_datetime::parse_datetime_at_date; |
| 6 | +use rstest::rstest; |
| 7 | + |
| 8 | +// The expected values are produced by GNU date version 8.32 |
| 9 | +// export LC_TIME=en_US.UTF-8 |
| 10 | +// export TZ=UTC |
| 11 | +// date date --date="12:34:56+09:00" +"%H:%M:%S.%N" |
| 12 | +// |
| 13 | +// Documentation for the date format can be found at: |
| 14 | +// https://www.gnu.org/software/coreutils/manual/html_node/Time-of-day-items.html |
| 15 | + |
| 16 | +pub fn check_time(input: &str, expected: &str, format: &str, base: Option<DateTime<Local>>) { |
| 17 | + std::env::set_var("TZ", "UTC0"); |
| 18 | + let now = base.unwrap_or_else(|| std::time::SystemTime::now().into()); |
| 19 | + let parsed = match parse_datetime_at_date(now, input) { |
| 20 | + Ok(v) => v, |
| 21 | + Err(e) => panic!("Failed to parse time from value '{input}': {e}"), |
| 22 | + } |
| 23 | + .to_utc(); |
| 24 | + |
| 25 | + assert_eq!( |
| 26 | + &format!("{}", parsed.format(format)), |
| 27 | + expected, |
| 28 | + "Input value: {input}" |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +#[rstest] |
| 33 | +#[case::full_time("12:34:56", "12:34:56.000000000")] |
| 34 | +#[case::full_time_with_spaces("12 : 34 : 56", "12:34:56.000000000")] |
| 35 | +#[case::full_time_midnight("00:00:00", "00:00:00.000000000")] |
| 36 | +#[case::full_time_almost_midnight("23:59:59", "23:59:59.000000000")] |
| 37 | +/* TODO: https://github.com/uutils/parse_datetime/issues/165 |
| 38 | +#[case::full_time_decimal_seconds("12:34:56.666", "12:34:56.666000000")] |
| 39 | +#[case::full_time_decimal_seconds("12:34:56.999999999", "12:34:56.999999999")] |
| 40 | +#[case::full_time_decimal_seconds("12:34:56.9999999999", "12:34:56.999999999")] |
| 41 | +#[case::full_time_decimal_seconds_after_comma("12:34:56,666", "12:34:56.666000000")] |
| 42 | +*/ |
| 43 | +#[case::without_seconds("12:34", "12:34:00.000000000")] |
| 44 | +fn test_time_24h_format(#[case] input: &str, #[case] expected: &str) { |
| 45 | + check_time(input, expected, "%H:%M:%S%.9f", None); |
| 46 | +} |
| 47 | + |
| 48 | +#[rstest] |
| 49 | +#[case::full_time_am("12:34:56am", "00:34:56.000000000")] |
| 50 | +#[case::full_time_pm("12:34:56pm", "12:34:56.000000000")] |
| 51 | +#[case::full_time_am_with_dots("12:34:56a.m.", "00:34:56.000000000")] |
| 52 | +#[case::full_time_pm_with_dots("12:34:56p.m.", "12:34:56.000000000")] |
| 53 | +#[case::full_time_with_spaces("12 : 34 : 56 am", "00:34:56.000000000")] |
| 54 | +#[case::full_time_capital("12:34:56pm", "12:34:56.000000000")] |
| 55 | +#[case::full_time_midnight("00:00:00", "00:00:00.000000000")] |
| 56 | +#[case::full_time_almost_midnight("23:59:59", "23:59:59.000000000")] |
| 57 | +/* TODO: https://github.com/uutils/parse_datetime/issues/165 |
| 58 | +#[case::full_time_decimal_seconds("12:34:56.666pm", "12:34:56.666000000")] |
| 59 | +#[case::full_time_decimal_seconds_after_comma("12:34:56,666pm", "12:34:56.666000000")] |
| 60 | +*/ |
| 61 | +#[case::without_seconds("12:34pm", "12:34:00.000000000")] |
| 62 | +fn test_time_12h_format(#[case] input: &str, #[case] expected: &str) { |
| 63 | + check_time(input, expected, "%H:%M:%S%.9f", None); |
| 64 | +} |
| 65 | + |
| 66 | +#[rstest] |
| 67 | +#[case::utc("12:34:56+00:00", "12:34:56.000000000")] |
| 68 | +#[case::utc_with_minus("12:34:56-00:00", "12:34:56.000000000")] |
| 69 | +#[case::corrected_plus("12:34:56+09:00", "03:34:56.000000000")] |
| 70 | +#[case::corrected_minus("12:34:56-09:00", "21:34:56.000000000")] |
| 71 | +#[case::corrected_no_colon("12:34:56+0900", "03:34:56.000000000")] |
| 72 | +#[case::corrected_plus_hours_only("12:34:56+09", "03:34:56.000000000")] |
| 73 | +#[case::corrected_minus_hours_only("12:34:56-09", "21:34:56.000000000")] |
| 74 | +#[case::corrected_plus_minutes("12:34:56+09:12", "03:22:56.000000000")] |
| 75 | +#[case::corrected_minus_minutes("12:34:56-09:26", "22:00:56.000000000")] |
| 76 | +#[case::corrected_plus_single_digit("12:34:56+9", "03:34:56.000000000")] |
| 77 | +#[case::corrected_minus_single_digit("12:34:56-9", "21:34:56.000000000")] |
| 78 | +#[case::with_space("12:34:56 -09:00", "21:34:56.000000000")] |
| 79 | +#[case::with_space("12:34:56 - 09:00", "21:34:56.000000000")] |
| 80 | +#[case::with_space_only_hours("12:34:56 -09", "21:34:56.000000000")] |
| 81 | +#[case::with_space_one_digit("12:34:56 -9", "21:34:56.000000000")] |
| 82 | +#[case::gnu_compatibility("12:34:56+", "12:34:56.000000000")] |
| 83 | +#[case::gnu_compatibility("12:34:56+-", "12:34:56.000000000")] |
| 84 | +#[case::gnu_compatibility("12:34:56+-01", "13:34:56.000000000")] |
| 85 | +#[case::gnu_compatibility("12:34:56+-+++---++", "12:34:56.000000000")] |
| 86 | +#[case::gnu_compatibility("12:34:56+1-", "11:34:56.000000000")] |
| 87 | +#[case::gnu_compatibility("12:34:56+--+1-+-", "11:34:56.000000000")] |
| 88 | +fn test_time_correction(#[case] input: &str, #[case] expected: &str) { |
| 89 | + check_time(input, expected, "%H:%M:%S%.9f", None); |
| 90 | +} |
| 91 | + |
| 92 | +#[rstest] |
| 93 | +#[case::plus_12("11:34:56+12:00", "2022-06-09 23:34:56")] |
| 94 | +#[case::minus_12("12:34:56-12:00", "2022-06-11 00:34:56")] |
| 95 | +#[case::plus_1259("12:34:56+12:59", "2022-06-09 23:35:56")] |
| 96 | +#[case::minus_1259("12:34:56-12:59", "2022-06-11 01:33:56")] |
| 97 | +/* TODO: https://github.com/uutils/parse_datetime/issues/149 |
| 98 | +#[case::plus_24("12:34:56+24:00", "2022-06-09 12:34:56")] |
| 99 | +#[case::minus_24("12:34:56-24:00", "2022-06-11 12:34:56")] |
| 100 | +#[case::plus_13("11:34:56+13:00", "2022-06-09 22:34:56")] |
| 101 | +#[case::minus_13("12:34:56-13:00", "2022-06-11 01:34:56")] |
| 102 | +*/ |
| 103 | +fn test_time_correction_with_overflow(#[case] input: &str, #[case] expected: &str) { |
| 104 | + let now = DateTime::parse_from_rfc3339("2022-06-10T00:00:00+00:00").unwrap(); |
| 105 | + check_time(input, expected, "%Y-%m-%d %H:%M:%S", Some(now.into())); |
| 106 | +} |
| 107 | + |
| 108 | +#[rstest] |
| 109 | +#[case("24:00:00")] |
| 110 | +#[case("23:60:00")] |
| 111 | +#[case("23:59:60")] |
| 112 | +#[case("13:00:00am")] |
| 113 | +#[case("13:00:00pm")] |
| 114 | +/* TODO: https://github.com/uutils/parse_datetime/issues/166 |
| 115 | +#[case("00:00:00am")] |
| 116 | +#[case("00:00:00pm")] |
| 117 | +*/ |
| 118 | +#[case("23:59:59 a.m")] |
| 119 | +#[case("23:59:59 pm.")] |
| 120 | +#[case("23:59:59+24:01")] |
| 121 | +#[case("23:59:59-24:01")] |
| 122 | +/* TODO: https://github.com/uutils/parse_datetime/issues/166 |
| 123 | +#[case("10:59am+01")] |
| 124 | +*/ |
| 125 | +#[case("10:59+01pm")] |
| 126 | +#[case("23:59:59+00:00:00")] |
| 127 | +fn test_time_invalid(#[case] input: &str) { |
| 128 | + let result = parse_datetime::parse_datetime(input); |
| 129 | + assert_eq!( |
| 130 | + result, |
| 131 | + Err(parse_datetime::ParseDateTimeError::InvalidInput), |
| 132 | + "Input string '{input}' did not produce an error when parsing" |
| 133 | + ); |
| 134 | +} |
0 commit comments