Debian Codegraph @

debian/rust-vendor/parse_datetime/tests/time.rs

raw
1 // For the full copyright and license information, please view the LICENSE
2 // file that was distributed with this source code.
3
4 use jiff::{civil::DateTime, tz::TimeZone, Zoned};
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<Zoned>) {
17 std::env::set_var("TZ", "UTC0");
18 let now = base.unwrap_or(Zoned::now());
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 .expect_in_range()
24 .with_time_zone(TimeZone::UTC);
25
26 assert_eq!(
27 format!("{}", parsed.strftime(format)),
28 expected,
29 "Input value: {input}"
30 );
31 }
32
33 #[rstest]
34 #[case::full_time("12:34:56", "12:34:56.000000000")]
35 #[case::full_time_with_spaces("12 : 34 : 56", "12:34:56.000000000")]
36 #[case::full_time_midnight("00:00:00", "00:00:00.000000000")]
37 #[case::full_time_almost_midnight("23:59:59", "23:59:59.000000000")]
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 #[case::without_seconds("12:34", "12:34:00.000000000")]
43 fn test_time_24h_format(#[case] input: &str, #[case] expected: &str) {
44 check_time(input, expected, "%H:%M:%S%.9f", None);
45 }
46
47 #[rstest]
48 #[case::full_time_am("12:34:56am", "00:34:56.000000000")]
49 #[case::full_time_pm("12:34:56pm", "12:34:56.000000000")]
50 #[case::full_time_am_with_dots("12:34:56a.m.", "00:34:56.000000000")]
51 #[case::full_time_pm_with_dots("12:34:56p.m.", "12:34:56.000000000")]
52 #[case::full_time_with_spaces("12 : 34 : 56 am", "00:34:56.000000000")]
53 #[case::full_time_capital("12:34:56pm", "12:34:56.000000000")]
54 #[case::full_time_midnight("00:00:00", "00:00:00.000000000")]
55 #[case::full_time_almost_midnight("23:59:59", "23:59:59.000000000")]
56 #[case::full_time_decimal_seconds("12:34:56.666pm", "12:34:56.666000000")]
57 #[case::full_time_decimal_seconds_after_comma("12:34:56,666pm", "12:34:56.666000000")]
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 #[case::plus_24("12:34:56+24:00", "2022-06-09 12:34:56")]
95 #[case::minus_24("12:34:56-24:00", "2022-06-11 12:34:56")]
96 #[case::plus_13("11:34:56+13:00", "2022-06-09 22:34:56")]
97 #[case::minus_13("12:34:56-13:00", "2022-06-11 01:34:56")]
98 #[case::plus_36("12:34:56 m+24", "2022-06-09 00:34:56")]
99 #[case::minus_36("12:34:56 y-24:00", "2022-06-12 00:34:56")]
100 fn test_time_correction_with_overflow(#[case] input: &str, #[case] expected: &str) {
101 let now = "2022-06-10 00:00:00"
102 .parse::<DateTime>()
103 .unwrap()
104 .to_zoned(TimeZone::UTC)
105 .unwrap();
106 check_time(input, expected, "%Y-%m-%d %H:%M:%S", Some(now));
107 }
108
109 #[rstest]
110 #[case("24:00:00")]
111 #[case("23:60:00")]
112 #[case("23:59:60")]
113 #[case("13:00:00am")]
114 #[case("13:00:00pm")]
115 #[case("00:00:00am")]
116 #[case("00:00:00pm")]
117 #[case("23:59:59 a.m")]
118 #[case("23:59:59 pm.")]
119 #[case("23:59:59+24:01")]
120 #[case("23:59:59-24:01")]
121 #[case("10:59am+01")]
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 }
132
133 #[rstest]
134 #[case::months_ago_0("2026-01-12 0 Months ago", "2026-01-12")]
135 #[case::months_ago_negative_1("2026-01-12 -1 Months ago", "2026-02-12")]
136 #[case::months_ago_1("2026-01-12 1 Months ago", "2025-12-12")]
137 #[case::months_ago_2("2026-01-12 2 Months ago", "2025-11-12")]
138 #[case::months_ago_3("2026-01-12 3 Months ago", "2025-10-12")]
139 #[case::months_ago_4("2026-01-12 4 Months ago", "2025-09-12")]
140 #[case::months_ago_5("2026-01-12 5 Months ago", "2025-08-12")]
141 #[case::months_ago_6("2026-01-12 6 Months ago", "2025-07-12")]
142 #[case::months_ago_7("2026-01-12 7 Months ago", "2025-06-12")]
143 #[case::months_ago_8("2026-01-12 8 Months ago", "2025-05-12")]
144 #[case::months_ago_9("2026-01-12 9 Months ago", "2025-04-12")]
145 #[case::months_ago_10("2026-01-12 10 Months ago", "2025-03-12")]
146 #[case::months_ago_11("2026-01-12 11 Months ago", "2025-02-12")]
147 #[case::months_ago_12("2026-01-12 12 Months ago", "2025-01-12")]
148 #[case::months_ago_24("2026-01-12 24 Months ago", "2024-01-12")]
149 #[case::months_ago_36("2026-01-12 36 Months ago", "2023-01-12")]
150 #[case::months_ago_120("2026-01-12 120 Months ago", "2016-01-12")]
151 #[case::months_ago_240("2026-01-12 240 Months ago", "2006-01-12")]
152 fn test_time_months_ago(#[case] input: &str, #[case] expected: &str) {
153 let now = "2026-01-12"
154 .parse::<DateTime>()
155 .unwrap()
156 .to_zoned(TimeZone::UTC)
157 .unwrap();
158 check_time(input, expected, "%Y-%m-%d", Some(now));
159 }
160
161 #[rstest]
162 #[case::from_28_feb_in_1_month("2026-02-28 1 months", "2026-03-28")]
163 #[case::from_28_feb_1_month_ago("2026-02-28 1 months ago", "2026-01-28")]
164 #[case::from_31_jan_in_1_month("2026-01-31 1 months", "2026-03-03")]
165 #[case::from_31_march_1_month_ago("2026-03-31 1 months ago", "2026-03-03")]
166 #[case::from_15_march_1_month_ago("2026-03-15 1 months ago", "2026-02-15")]
167 #[case::from_15_jan_in_1_month("2026-01-15 1 months", "2026-02-15")]
168 fn test_relative_month_time_non_leap_year(#[case] input: &str, #[case] expected: &str) {
169 let now = "2026-01-12"
170 .parse::<DateTime>()
171 .unwrap()
172 .to_zoned(TimeZone::UTC)
173 .unwrap();
174 check_time(input, expected, "%Y-%m-%d", Some(now));
175 }
176
177 #[rstest]
178 #[case::from_31_dec_1_month_ago("2026-12-31 1 months ago", "2026-12-01")]
179 #[case::from_31_dec_3_month_ago("2026-12-31 3 months ago", "2026-10-01")]
180 #[case::from_31_mar_in_1_month("2026-03-31 1 months", "2026-05-01")]
181 #[case::from_31_mar_in_3_month("2026-03-31 3 months", "2026-07-01")]
182 fn test_relative_month_time_dest_month_does_not_have_the_day(
183 #[case] input: &str,
184 #[case] expected: &str,
185 ) {
186 let now = "2026-01-12"
187 .parse::<DateTime>()
188 .unwrap()
189 .to_zoned(TimeZone::UTC)
190 .unwrap();
191 check_time(input, expected, "%Y-%m-%d", Some(now));
192 }
193
194 #[rstest]
195 #[case::from_29_feb_in_1_year("2024-02-29 1 year", "2025-03-01")]
196 #[case::from_29_feb_1_year_ago("2024-02-29 1 year ago", "2023-03-01")]
197 fn test_relative_year_time_leap_year(#[case] input: &str, #[case] expected: &str) {
198 let now = "2026-01-12"
199 .parse::<DateTime>()
200 .unwrap()
201 .to_zoned(TimeZone::UTC)
202 .unwrap();
203 check_time(input, expected, "%Y-%m-%d", Some(now));
204 }
205
206 #[rstest]
207 #[case::from_29_feb_in_1_month("2024-02-29 1 months", "2024-03-29")]
208 #[case::from_29_feb_1_month_ago("2024-02-29 1 months ago", "2024-01-29")]
209 #[case::from_31_jan_in_1_month("2024-01-31 1 months", "2024-03-02")]
210 #[case::from_31_march_1_month_ago("2024-03-31 1 months ago", "2024-03-02")]
211 #[case::from_15_march_1_month_ago("2024-03-15 1 months ago", "2024-02-15")]
212 #[case::from_15_jan_in_1_month("2024-01-15 1 months", "2024-02-15")]
213 fn test_relative_month_time_leap_year(#[case] input: &str, #[case] expected: &str) {
214 let now = "2026-01-12"
215 .parse::<DateTime>()
216 .unwrap()
217 .to_zoned(TimeZone::UTC)
218 .unwrap();
219 check_time(input, expected, "%Y-%m-%d", Some(now));
220 }
221
222 #[rstest]
223 #[case::months_in_0_months("2026-01-12 0 months", "2026-01-12")]
224 #[case::months_in_negative_1_months("2026-01-12 -1 months", "2025-12-12")]
225 #[case::months_in_1_months("2026-01-12 1 months", "2026-02-12")]
226 #[case::months_in_2_months("2026-01-12 2 months", "2026-03-12")]
227 #[case::months_in_3_months("2026-01-12 3 months", "2026-04-12")]
228 #[case::months_in_4_months("2026-01-12 4 months", "2026-05-12")]
229 #[case::months_in_5_months("2026-01-12 5 months", "2026-06-12")]
230 #[case::months_in_6_months("2026-01-12 6 months", "2026-07-12")]
231 #[case::months_in_7_months("2026-01-12 7 months", "2026-08-12")]
232 #[case::months_in_8_months("2026-01-12 8 months", "2026-09-12")]
233 #[case::months_in_9_months("2026-01-12 9 months", "2026-10-12")]
234 #[case::months_in_10_months("2026-01-12 10 months", "2026-11-12")]
235 #[case::months_in_11_months("2026-01-12 11 months", "2026-12-12")]
236 #[case::months_in_12_months("2026-01-12 12 months", "2027-01-12")]
237 #[case::months_in_24_months("2026-01-12 24 months", "2028-01-12")]
238 #[case::months_in_36_months("2026-01-12 36 months", "2029-01-12")]
239 #[case::months_in_120_months("2026-01-12 120 months", "2036-01-12")]
240 #[case::months_in_240_months("2026-01-12 240 months", "2046-01-12")]
241 fn test_time_in_months(#[case] input: &str, #[case] expected: &str) {
242 let now = "2026-01-12"
243 .parse::<DateTime>()
244 .unwrap()
245 .to_zoned(TimeZone::UTC)
246 .unwrap();
247 check_time(input, expected, "%Y-%m-%d", Some(now));
248 }
249
250 #[rstest]
251 #[case::decimal_1_whole("1.123456789 seconds ago")]
252 #[case::decimal_2_whole("12.123456789 seconds ago")]
253 #[case::decimal_3_whole("123.123456789 seconds ago")]
254 #[case::decimal_4_whole("1234.123456789 seconds ago")]
255 #[case::decimal_5_whole("12345.123456789 seconds ago")]
256 #[case::decimal_6_whole("123456.123456789 seconds ago")]
257 #[case::decimal_7_whole("1234567.123456789 seconds ago")]
258 #[case::decimal_8_whole("12345678.123456789 seconds ago")]
259 #[case::decimal_9_whole("123456789.123456789 seconds ago")]
260 #[case::decimal_10_whole("1234567891.123456789 seconds ago")]
261 #[case::decimal_11_whole("12345678912.123456789 seconds ago")]
262 #[case::decimal_12_whole("123456789123.123456789 seconds ago")]
263 fn test_time_seconds_ago(#[case] input: &str) {
264 let result = parse_datetime::parse_datetime(input);
265 assert!(
266 result.is_ok(),
267 "Input string '{input}', produced {result:?}, instead of Ok(ParsedDateTime)"
268 );
269 }
270
271 #[rstest]
272 #[case::decimal_13_whole("1234567891234.123456789 seconds ago")]
273 #[case::decimal_14_whole("12345678912345.123456789 seconds ago")]
274 #[case::decimal_15_whole("123456789123456.123456789 seconds ago")]
275 fn test_time_seconds_ago_invalid(#[case] input: &str) {
276 let result = parse_datetime::parse_datetime(input);
277 assert_eq!(
278 result,
279 Err(parse_datetime::ParseDateTimeError::InvalidInput),
280 "Input string '{input}' did not produce an error when parsing"
281 );
282 }