Skip to content

Commit ba4c536

Browse files
committed
test: Integration tests for date parsing
1 parent f1f2c47 commit ba4c536

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

tests/time.rs

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

0 commit comments

Comments
 (0)