| 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}; |
| 5 |
use rstest::rstest; |
| 6 |
|
| 7 |
mod common; |
| 8 |
use common::{check_absolute, check_relative}; |
| 9 |
|
| 10 |
// The expected values are produced by GNU date version 8.32 |
| 11 |
// export LC_TIME=en_US.UTF-8 |
| 12 |
// export TZ=UTC |
| 13 |
// date --rfc-3339=seconds --date="2022-11-14" |
| 14 |
// |
| 15 |
// Documentation for the date format can be found at: |
| 16 |
// https://www.gnu.org/software/coreutils/manual/html_node/Calendar-date-items.html |
| 17 |
|
| 18 |
#[rstest] |
| 19 |
#[case::iso8601("2022-11-14", "2022-11-14 00:00:00+00:00")] |
| 20 |
#[case::short_year_22("22-11-14", "2022-11-14 00:00:00+00:00")] |
| 21 |
#[case::short_year_68("68-11-14", "2068-11-14 00:00:00+00:00")] |
| 22 |
#[case::short_year_00("00-11-14", "2000-11-14 00:00:00+00:00")] |
| 23 |
#[case::short_year_69("69-11-14", "1969-11-14 00:00:00+00:00")] |
| 24 |
#[case::short_year_99("99-11-14", "1999-11-14 00:00:00+00:00")] |
| 25 |
#[case::us_style("11/14/2022", "2022-11-14 00:00:00+00:00")] |
| 26 |
#[case::us_style_short_year("11/14/22", "2022-11-14 00:00:00+00:00")] |
| 27 |
#[case::year_zero("0000-01-01", "0000-01-01 00:00:00+00:00")] |
| 28 |
#[case::year_001("001-11-14", "0001-11-14 00:00:00+00:00")] |
| 29 |
#[case::year_100("100-11-14", "0100-11-14 00:00:00+00:00")] |
| 30 |
#[case::year_999("999-11-14", "0999-11-14 00:00:00+00:00")] |
| 31 |
#[case::year_9999("9999-11-14", "9999-11-14 00:00:00+00:00")] |
| 32 |
/** TODO: https://github.com/uutils/parse_datetime/issues/160 |
| 33 |
#[case::year_10000("10000-12-31", "10000-12-31 00:00:00+00:00")] |
| 34 |
#[case::year_100000("100000-12-31", "100000-12-31 00:00:00+00:00")] |
| 35 |
#[case::year_1000000("1000000-12-31", "1000000-12-31 00:00:00+00:00")] |
| 36 |
#[case::year_10000000("10000000-12-31", "10000000-12-31 00:00:00+00:00")] |
| 37 |
#[case::max_date("2147485547-12-31", "2147485547-12-31 00:00:00+00:00")] |
| 38 |
**/ |
| 39 |
#[case::long_month_in_the_middle("14 November 2022", "2022-11-14 00:00:00+00:00")] |
| 40 |
#[case::long_month_in_the_middle_lowercase("14 november 2022", "2022-11-14 00:00:00+00:00")] |
| 41 |
#[case::long_month_in_the_middle_uppercase("14 NOVEMBER 2022", "2022-11-14 00:00:00+00:00")] |
| 42 |
#[case::short_month_in_the_middle("14 nov 2022", "2022-11-14 00:00:00+00:00")] |
| 43 |
#[case::short_month_in_the_uppercase("14 NOV 2022", "2022-11-14 00:00:00+00:00")] |
| 44 |
#[case::long_month_in_the_middle_hyphened("14-november-2022", "2022-11-14 00:00:00+00:00")] |
| 45 |
#[case::long_month_in_the_middle_nospace("14november2022", "2022-11-14 00:00:00+00:00")] |
| 46 |
#[case::short_month_in_the_middle_hyphened("14-nov-2022", "2022-11-14 00:00:00+00:00")] |
| 47 |
#[case::short_month_in_the_middle_nospace("14nov2022", "2022-11-14 00:00:00+00:00")] |
| 48 |
#[case::long_month_at_start("November 14 2022", "2022-11-14 00:00:00+00:00")] |
| 49 |
#[case::long_month_at_start_with_comma("November 14, 2022", "2022-11-14 00:00:00+00:00")] |
| 50 |
#[case::short_month_at_start("nov 14 2022", "2022-11-14 00:00:00+00:00")] |
| 51 |
#[case::long_month_in_the_middle_jan("14 January 2022", "2022-01-14 00:00:00+00:00")] |
| 52 |
#[case::long_month_in_the_middle_feb("14 February 2022", "2022-02-14 00:00:00+00:00")] |
| 53 |
#[case::long_month_in_the_middle_mar("14 March 2022", "2022-03-14 00:00:00+00:00")] |
| 54 |
#[case::long_month_in_the_middle_apr("14 April 2022", "2022-04-14 00:00:00+00:00")] |
| 55 |
#[case::long_month_in_the_middle_may("14 May 2022", "2022-05-14 00:00:00+00:00")] |
| 56 |
#[case::long_month_in_the_middle_jun("14 June 2022", "2022-06-14 00:00:00+00:00")] |
| 57 |
#[case::long_month_in_the_middle_jul("14 July 2022", "2022-07-14 00:00:00+00:00")] |
| 58 |
#[case::long_month_in_the_middle_aug("14 August 2022", "2022-08-14 00:00:00+00:00")] |
| 59 |
#[case::long_month_in_the_middle_sep("14 September 2022", "2022-09-14 00:00:00+00:00")] |
| 60 |
#[case::long_month_in_the_middle_oct("14 October 2022", "2022-10-14 00:00:00+00:00")] |
| 61 |
#[case::long_month_in_the_middle_dec("14 December 2022", "2022-12-14 00:00:00+00:00")] |
| 62 |
#[case::short_month_in_the_middle_jan("14 jan 2022", "2022-01-14 00:00:00+00:00")] |
| 63 |
#[case::short_month_in_the_middle_feb("14 feb 2022", "2022-02-14 00:00:00+00:00")] |
| 64 |
#[case::short_month_in_the_middle_mar("14 mar 2022", "2022-03-14 00:00:00+00:00")] |
| 65 |
#[case::short_month_in_the_middle_apr("14 apr 2022", "2022-04-14 00:00:00+00:00")] |
| 66 |
#[case::short_month_in_the_middle_may("14 may 2022", "2022-05-14 00:00:00+00:00")] |
| 67 |
#[case::short_month_in_the_middle_jun("14 jun 2022", "2022-06-14 00:00:00+00:00")] |
| 68 |
#[case::short_month_in_the_middle_jul("14 jul 2022", "2022-07-14 00:00:00+00:00")] |
| 69 |
#[case::short_month_in_the_middle_aug("14 aug 2022", "2022-08-14 00:00:00+00:00")] |
| 70 |
#[case::short_month_in_the_middle_sep("14 sep 2022", "2022-09-14 00:00:00+00:00")] |
| 71 |
#[case::short_month_in_the_middle_sept("14 sept 2022", "2022-09-14 00:00:00+00:00")] |
| 72 |
#[case::short_month_in_the_middle_oct("14 oct 2022", "2022-10-14 00:00:00+00:00")] |
| 73 |
#[case::short_month_in_the_middle_dec("14 dec 2022", "2022-12-14 00:00:00+00:00")] |
| 74 |
fn test_absolute_date_numeric(#[case] input: &str, #[case] expected: &str) { |
| 75 |
check_absolute(input, expected); |
| 76 |
} |
| 77 |
|
| 78 |
// Any run of ASCII whitespace may separate items, like in GNU date. The |
| 79 |
// expected values were verified with GNU date (`date -d 'input'`). |
| 80 |
#[rstest] |
| 81 |
#[case::vertical_tab_between_date_and_year("Jan 23\x0B2026 1:00AM", "2026-01-23 01:00:00+00:00")] |
| 82 |
#[case::form_feed_between_date_and_year("Jan 23\x0C2026 1:00AM", "2026-01-23 01:00:00+00:00")] |
| 83 |
#[case::vertical_tab_after_comma("Jan 23,\x0B2026", "2026-01-23 00:00:00+00:00")] |
| 84 |
#[case::vertical_tab_before_time("14 nov 2026\x0B1:00AM", "2026-11-14 01:00:00+00:00")] |
| 85 |
#[case::vertical_tab_as_iso_separator("2026-01-23\x0B01:00:00", "2026-01-23 01:00:00+00:00")] |
| 86 |
fn test_whitespace_between_items(#[case] input: &str, #[case] expected: &str) { |
| 87 |
check_absolute(input, expected); |
| 88 |
} |
| 89 |
|
| 90 |
#[rstest] |
| 91 |
#[case::us_style("11/14", 2022, "2022-11-14 00:00:00+00:00")] |
| 92 |
#[case::alphabetical_full_month_in_front("november 14", 2022, "2022-11-14 00:00:00+00:00")] |
| 93 |
#[case::alphabetical_full_month_at_back("14 november", 2022, "2022-11-14 00:00:00+00:00")] |
| 94 |
#[case::alphabetical_short_month_in_front("nov 14", 2022, "2022-11-14 00:00:00+00:00")] |
| 95 |
#[case::alphabetical_short_month_at_back("14 nov", 2022, "2022-11-14 00:00:00+00:00")] |
| 96 |
#[case::alphabetical_full_month_in_front("november 14", 2022, "2022-11-14 00:00:00+00:00")] |
| 97 |
#[case::alphabetical_full_month_at_back("14 november", 2022, "2022-11-14 00:00:00+00:00")] |
| 98 |
#[case::alphabetical_short_month_in_front("nov 14", 2022, "2022-11-14 00:00:00+00:00")] |
| 99 |
#[case::alphabetical_short_month_at_back("14 nov", 2022, "2022-11-14 00:00:00+00:00")] |
| 100 |
#[case::alphabetical_long_month_at_back_hyphen("14-november", 2022, "2022-11-14 00:00:00+00:00")] |
| 101 |
#[case::alphabetical_short_month_at_back_hyphen("14-nov", 2022, "2022-11-14 00:00:00+00:00")] |
| 102 |
fn test_date_omitting_year(#[case] input: &str, #[case] year: u32, #[case] expected: &str) { |
| 103 |
let now = format!("{year}-06-01 00:00:00") |
| 104 |
.parse::<DateTime>() |
| 105 |
.unwrap() |
| 106 |
.to_zoned(TimeZone::UTC) |
| 107 |
.unwrap(); |
| 108 |
check_relative(now, input, expected); |
| 109 |
} |
| 110 |
|
| 111 |
#[rstest] |
| 112 |
#[case::tz_prefix_est5("TZ=\"EST5\" 1970-01-01 00:00", "1970-01-01 00:00:00-05:00")] |
| 113 |
#[case::tz_prefix_pst8("TZ=\"PST8\" 1970-01-01 00:00", "1970-01-01 00:00:00-08:00")] |
| 114 |
#[case::tz_prefix_utc("TZ=\"UTC\" 1970-01-01 12:00", "1970-01-01 12:00:00+00:00")] |
| 115 |
#[case::tz_prefix_europe_paris( |
| 116 |
r#"TZ="Europe/Paris" 2025-01-02 03:04:05"#, |
| 117 |
"2025-01-02 03:04:05+01:00" |
| 118 |
)] |
| 119 |
fn test_tz_prefix_with_base_date(#[case] input: &str, #[case] expected: &str) { |
| 120 |
let base = "2020-06-15 12:00:00" |
| 121 |
.parse::<DateTime>() |
| 122 |
.unwrap() |
| 123 |
.to_zoned(TimeZone::UTC) |
| 124 |
.unwrap(); |
| 125 |
check_relative(base, input, expected); |
| 126 |
} |
| 127 |
|
| 128 |
// Test leap year overflow: Feb 29 + years → non-leap year should overflow to March 1 |
| 129 |
// This matches GNU date behavior |
| 130 |
#[rstest] |
| 131 |
#[case::feb29_1996_plus_1year("1996-02-29 00:00:00", "1 year", "1997-03-01 00:00:00+00:00")] |
| 132 |
#[case::feb29_2020_plus_1year("2020-02-29 00:00:00", "1 year", "2021-03-01 00:00:00+00:00")] |
| 133 |
#[case::feb29_2000_plus_1year("2000-02-29 00:00:00", "1 year", "2001-03-01 00:00:00+00:00")] |
| 134 |
// Edge case: 0 years should return the same date |
| 135 |
#[case::zero_years("2024-01-15 12:30:45", "0 years", "2024-01-15 12:30:45+00:00")] |
| 136 |
#[case::zero_years_feb29("2020-02-29 00:00:00", "0 years", "2020-02-29 00:00:00+00:00")] |
| 137 |
fn test_leap_year_overflow(#[case] base: &str, #[case] input: &str, #[case] expected: &str) { |
| 138 |
let now = base |
| 139 |
.parse::<DateTime>() |
| 140 |
.unwrap() |
| 141 |
.to_zoned(TimeZone::UTC) |
| 142 |
.unwrap(); |
| 143 |
check_relative(now, input, expected); |
| 144 |
} |
| 145 |
|
| 146 |
// Test month arithmetic with day overflow |
| 147 |
// Matches GNU date behavior: when adding months causes day clamping, |
| 148 |
// overflow to next month (e.g., Jan 31 + 1 month = March 2/3, not Feb 28/29) |
| 149 |
#[rstest] |
| 150 |
#[case::jan31_plus_1month_leap("2024-01-31 00:00:00", "1 month", "2024-03-02 00:00:00+00:00")] |
| 151 |
#[case::jan31_plus_1month_nonleap("2023-01-31 00:00:00", "1 month", "2023-03-03 00:00:00+00:00")] |
| 152 |
#[case::mar31_plus_1month("2024-03-31 00:00:00", "1 month", "2024-05-01 00:00:00+00:00")] |
| 153 |
#[case::may31_plus_1month("2024-05-31 00:00:00", "1 month", "2024-07-01 00:00:00+00:00")] |
| 154 |
#[case::rel_2b("1997-01-19 08:17:48", "7 months ago", "1996-06-19 08:17:48+00:00")] |
| 155 |
// Edge case: 0 months should return the same date |
| 156 |
#[case::zero_months("2024-01-31 12:30:45", "0 months", "2024-01-31 12:30:45+00:00")] |
| 157 |
#[case::zero_months_feb29("2020-02-29 00:00:00", "0 months", "2020-02-29 00:00:00+00:00")] |
| 158 |
fn test_month_overflow(#[case] base: &str, #[case] input: &str, #[case] expected: &str) { |
| 159 |
let now = base |
| 160 |
.parse::<DateTime>() |
| 161 |
.unwrap() |
| 162 |
.to_zoned(TimeZone::UTC) |
| 163 |
.unwrap(); |
| 164 |
check_relative(now, input, expected); |
| 165 |
} |
| 166 |
|
| 167 |
// Test negative year operations with leap year edge cases |
| 168 |
#[rstest] |
| 169 |
#[case::feb29_minus_1year("2020-02-29 00:00:00", "1 year ago", "2019-03-01 00:00:00+00:00")] |
| 170 |
#[case::feb29_minus_4years("2020-02-29 00:00:00", "4 years ago", "2016-02-29 00:00:00+00:00")] |
| 171 |
#[case::march1_minus_1year("2021-03-01 00:00:00", "1 year ago", "2020-03-01 00:00:00+00:00")] |
| 172 |
fn test_negative_year_operations(#[case] base: &str, #[case] input: &str, #[case] expected: &str) { |
| 173 |
let now = base |
| 174 |
.parse::<DateTime>() |
| 175 |
.unwrap() |
| 176 |
.to_zoned(TimeZone::UTC) |
| 177 |
.unwrap(); |
| 178 |
check_relative(now, input, expected); |
| 179 |
} |
| 180 |
|
| 181 |
// Test negative month operations with day overflow |
| 182 |
#[rstest] |
| 183 |
#[case::march31_minus_1month("2024-03-31 00:00:00", "1 month ago", "2024-03-02 00:00:00+00:00")] |
| 184 |
#[case::march31_minus_1month_nonleap( |
| 185 |
"2023-03-31 00:00:00", |
| 186 |
"1 month ago", |
| 187 |
"2023-03-03 00:00:00+00:00" |
| 188 |
)] |
| 189 |
#[case::may31_minus_1month("2024-05-31 00:00:00", "1 month ago", "2024-05-01 00:00:00+00:00")] |
| 190 |
#[case::jan31_minus_1month("2024-01-31 00:00:00", "1 month ago", "2023-12-31 00:00:00+00:00")] |
| 191 |
fn test_negative_month_operations(#[case] base: &str, #[case] input: &str, #[case] expected: &str) { |
| 192 |
let now = base |
| 193 |
.parse::<DateTime>() |
| 194 |
.unwrap() |
| 195 |
.to_zoned(TimeZone::UTC) |
| 196 |
.unwrap(); |
| 197 |
check_relative(now, input, expected); |
| 198 |
} |
| 199 |
|
| 200 |
// Test chained operations (multiple relative adjustments in one parse) |
| 201 |
// These ensure that year and month overflow logic works correctly when combined |
| 202 |
#[rstest] |
| 203 |
// Feb 29, 2020 + 1 year = March 1, 2021; + 1 month = April 1, 2021 |
| 204 |
#[case::feb29_plus_year_plus_month( |
| 205 |
"2020-02-29 00:00:00", |
| 206 |
"1 year 1 month", |
| 207 |
"2021-04-01 00:00:00+00:00" |
| 208 |
)] |
| 209 |
// Jan 31, 2024 + 1 month = March 2, 2024; + 1 year = March 2, 2025 |
| 210 |
#[case::jan31_plus_month_plus_year( |
| 211 |
"2024-01-31 00:00:00", |
| 212 |
"1 month 1 year", |
| 213 |
"2025-03-02 00:00:00+00:00" |
| 214 |
)] |
| 215 |
// Jan 31 + 2 months + 1 day |
| 216 |
#[case::jan31_plus_2months_1day( |
| 217 |
"2024-01-31 00:00:00", |
| 218 |
"2 months 1 day", |
| 219 |
"2024-04-01 00:00:00+00:00" |
| 220 |
)] |
| 221 |
// Feb 29 - 1 year + 1 month (March 1, 2019 + 1 month = April 1, 2019) |
| 222 |
#[case::feb29_minus_year_plus_month( |
| 223 |
"2020-02-29 00:00:00", |
| 224 |
"1 year ago 1 month", |
| 225 |
"2019-04-01 00:00:00+00:00" |
| 226 |
)] |
| 227 |
// Multiple operations with days |
| 228 |
#[case::complex_chain( |
| 229 |
"2024-01-31 12:30:45", |
| 230 |
"1 year 2 months 3 days 4 hours", |
| 231 |
"2025-04-03 16:30:45+00:00" |
| 232 |
)] |
| 233 |
fn test_chained_operations(#[case] base: &str, #[case] input: &str, #[case] expected: &str) { |
| 234 |
let now = base |
| 235 |
.parse::<DateTime>() |
| 236 |
.unwrap() |
| 237 |
.to_zoned(TimeZone::UTC) |
| 238 |
.unwrap(); |
| 239 |
check_relative(now, input, expected); |
| 240 |
} |
| 241 |
|
| 242 |
// Test multiple month additions |
| 243 |
// Verifies correct handling when adding multiple months at once |
| 244 |
#[rstest] |
| 245 |
// Jan 31 + 2 months: Jan 31 -> March 31 (no clamping, month has 31 days) |
| 246 |
#[case::jan31_plus_2months("2024-01-31 00:00:00", "2 months", "2024-03-31 00:00:00+00:00")] |
| 247 |
// Jan 31 + 3 months: Jan 31 -> April 30 (clamps), overflow to May 1 |
| 248 |
#[case::jan31_plus_3months("2024-01-31 00:00:00", "3 months", "2024-05-01 00:00:00+00:00")] |
| 249 |
// Jan 31 + 6 months: Jan 31 -> July 31 (no overflow) |
| 250 |
#[case::jan31_plus_6months("2024-01-31 00:00:00", "6 months", "2024-07-31 00:00:00+00:00")] |
| 251 |
// Jan 31 + 7 months: Jan 31 -> Aug 31 (no overflow) |
| 252 |
#[case::jan31_plus_7months("2024-01-31 00:00:00", "7 months", "2024-08-31 00:00:00+00:00")] |
| 253 |
// Aug 31 + 6 months: Aug 31 -> Feb 28 (2025 non-leap), overflow to March 3 |
| 254 |
#[case::aug31_plus_6months("2024-08-31 00:00:00", "6 months", "2025-03-03 00:00:00+00:00")] |
| 255 |
// May 31 - 3 months: May 31 -> Feb 29 (2024 leap), overflow to March 2 |
| 256 |
#[case::may31_minus_3months_leap( |
| 257 |
"2024-05-31 00:00:00", |
| 258 |
"3 months ago", |
| 259 |
"2024-03-02 00:00:00+00:00" |
| 260 |
)] |
| 261 |
// Oct 31 - 8 months: Oct 31 -> Feb 29 (2024 leap), overflow to March 2 |
| 262 |
#[case::oct31_minus_8months_leap( |
| 263 |
"2024-10-31 00:00:00", |
| 264 |
"8 months ago", |
| 265 |
"2024-03-02 00:00:00+00:00" |
| 266 |
)] |
| 267 |
fn test_multiple_month_skip(#[case] base: &str, #[case] input: &str, #[case] expected: &str) { |
| 268 |
let now = base |
| 269 |
.parse::<DateTime>() |
| 270 |
.unwrap() |
| 271 |
.to_zoned(TimeZone::UTC) |
| 272 |
.unwrap(); |
| 273 |
check_relative(now, input, expected); |
| 274 |
} |
| 275 |
|
| 276 |
// Test embedded timezone handling (cross-TZ-mishandled) |
| 277 |
// When TZ="..." is specified in input with a base date, apply the timezone to the base |
| 278 |
// https://bugs.debian.org/851934#10 |
| 279 |
// |
| 280 |
// NOTE: These tests were added without implementation changes. |
| 281 |
// The timezone handling was already working correctly from previous commits. |
| 282 |
// These tests document and verify the expected behavior for this edge case. |
| 283 |
#[rstest] |
| 284 |
#[case::utc_explicit(r#"TZ="UTC0" 1970-01-01 00:00"#, "1970-01-01 00:00:00+00:00")] |
| 285 |
#[case::with_time(r#"TZ="EST5" 1970-01-01 12:30:45"#, "1970-01-01 12:30:45-05:00")] |
| 286 |
#[case::iana_timezone( |
| 287 |
r#"TZ="America/New_York" 1970-01-01 00:00"#, |
| 288 |
"1970-01-01 00:00:00-05:00" |
| 289 |
)] |
| 290 |
// Bug #851934: timezone conversion case |
| 291 |
// Parse date in Australia/Perth (AWST, UTC+8) and output should reflect that timezone |
| 292 |
// Input: 2016-08-15 07:00 in Australia/Perth -> expected: 2016-08-15 07:00:00+08:00 |
| 293 |
#[case::perth_to_london( |
| 294 |
r#"TZ="Australia/Perth" 2016-08-15 07:00"#, |
| 295 |
"2016-08-15 07:00:00+08:00" |
| 296 |
)] |
| 297 |
fn test_embedded_timezone(#[case] input: &str, #[case] expected: &str) { |
| 298 |
check_absolute(input, expected); |
| 299 |
} |
| 300 |
|
| 301 |
// Regression test for uutils/coreutils#12555. |
| 302 |
// A fixed offset (e.g. the "UTC" keyword) must anchor the instant *before* |
| 303 |
// relative adjustments are applied. Otherwise, when the base zone observes DST, |
| 304 |
// adding a large relative offset drifts the wall-clock across a DST boundary |
| 305 |
// and re-anchoring at the fixed offset bakes in the extra hour. |
| 306 |
// "1970-01-01 UTC + N seconds" must equal exactly N seconds after the epoch, |
| 307 |
// regardless of the base zone's DST rules. |
| 308 |
#[test] |
| 309 |
fn test_utc_keyword_plus_relative_seconds_across_dst() { |
| 310 |
// Base in a DST-observing zone (Europe/Berlin); the value only matters as |
| 311 |
// the default zone for interpreting the date, not the result. |
| 312 |
let base = "2020-06-15 12:00:00" |
| 313 |
.parse::<DateTime>() |
| 314 |
.unwrap() |
| 315 |
.to_zoned(TimeZone::get("Europe/Berlin").unwrap()) |
| 316 |
.unwrap(); |
| 317 |
|
| 318 |
// A timestamp landing in summer (CEST, UTC+2) while the epoch base is in |
| 319 |
// winter (CET, UTC+1), so the DST offset differs between base and result. |
| 320 |
let seconds = 1_780_318_971_i64; |
| 321 |
let input = format!("1970-01-01 UTC {seconds} seconds"); |
| 322 |
|
| 323 |
let parsed = parse_datetime::parse_datetime_at_date(base, input) |
| 324 |
.unwrap() |
| 325 |
.expect_in_range(); |
| 326 |
assert_eq!(parsed.timestamp().as_second(), seconds); |
| 327 |
} |
| 328 |
|
| 329 |
// The military time zone letter `j`. |
| 330 |
// |
| 331 |
// Every other military letter (`a`-`i`, `k`-`y`, and `z` for UTC) denotes a |
| 332 |
// fixed offset from UTC. GNU `date` defines `j` as *local* time instead, so it |
| 333 |
// follows the base zone's DST rules rather than pinning an offset: |
| 334 |
// |
| 335 |
// $ TZ=America/New_York date -d '8j' # 2026-08-25 08:00 -0400 |
| 336 |
// $ TZ=America/New_York date -d '2026-01-01 j' # 2026-01-01 00:00 -0500 |
| 337 |
// $ TZ=America/New_York date -d '8z' # 2026-08-25 04:00 -0400 |
| 338 |
// |
| 339 |
// Verified against GNU coreutils 9.4. |
| 340 |
#[rstest] |
| 341 |
// Winter: New York is on EST (UTC-5). |
| 342 |
#[case::est("2026-01-15 12:00:00", -5 * 3600)] |
| 343 |
// Summer: the same zone is on EDT (UTC-4). A fixed offset could not do this. |
| 344 |
#[case::edt("2026-07-15 12:00:00", -4 * 3600)] |
| 345 |
fn test_military_j_is_local_time(#[case] base: &str, #[case] expected_offset: i32) { |
| 346 |
let base = base |
| 347 |
.parse::<DateTime>() |
| 348 |
.unwrap() |
| 349 |
.to_zoned(TimeZone::get("America/New_York").unwrap()) |
| 350 |
.unwrap(); |
| 351 |
|
| 352 |
let parsed = parse_datetime::parse_datetime_at_date(base.clone(), "8j") |
| 353 |
.unwrap() |
| 354 |
.expect_in_range(); |
| 355 |
|
| 356 |
assert_eq!(parsed.hour(), 8, "`8j` should be 08:00 local"); |
| 357 |
assert_eq!( |
| 358 |
parsed.offset().seconds(), |
| 359 |
expected_offset, |
| 360 |
"`8j` should take the base zone's offset" |
| 361 |
); |
| 362 |
|
| 363 |
// `z` is UTC, so it must *not* follow the base zone. This is what |
| 364 |
// distinguishes `j` from every other military letter. |
| 365 |
let utc = parse_datetime::parse_datetime_at_date(base, "8z") |
| 366 |
.unwrap() |
| 367 |
.expect_in_range(); |
| 368 |
assert_eq!(utc.offset().seconds(), 0, "`8z` should be UTC"); |
| 369 |
} |
| 370 |
|
| 371 |
#[rstest] |
| 372 |
#[case::bare("j")] |
| 373 |
#[case::attached("8j")] |
| 374 |
#[case::uppercase("8J")] |
| 375 |
#[case::spaced("8 j")] |
| 376 |
#[case::leading("j 8")] |
| 377 |
fn test_military_j_accepted(#[case] input: &str) { |
| 378 |
assert!( |
| 379 |
parse_datetime::parse_datetime(input).is_ok(), |
| 380 |
"`{input}` should parse" |
| 381 |
); |
| 382 |
} |
| 383 |
|
| 384 |
#[rstest] |
| 385 |
// `j` is a time zone item, so a second one is a repeated zone, as in GNU. |
| 386 |
#[case::j_then_utc("8j utc")] |
| 387 |
#[case::utc_then_j("8 utc j")] |
| 388 |
#[case::j_twice("8 j j")] |
| 389 |
#[case::j_then_numeric("j +05:00")] |
| 390 |
// The whole alphabetic word is matched, so `j` never steals a prefix. |
| 391 |
#[case::doubled_letter("jj")] |
| 392 |
#[case::doubled_letter_after_number("8 jj")] |
| 393 |
fn test_military_j_rejected(#[case] input: &str) { |
| 394 |
assert!( |
| 395 |
parse_datetime::parse_datetime(input).is_err(), |
| 396 |
"`{input}` should be rejected, as GNU date does" |
| 397 |
); |
| 398 |
} |
| 399 |
|
| 400 |
// A relative item keeps the base time of day, even when combined with a time |
| 401 |
// zone item. Only a date, a weekday, or a lone zone item resets it to |
| 402 |
// midnight: |
| 403 |
// |
| 404 |
// $ TZ=America/New_York date -d 'j 1 day' # tomorrow, current time of day |
| 405 |
// $ TZ=America/New_York date -d 'utc 1 day' # ditto, in UTC |
| 406 |
// $ TZ=America/New_York date -d 'j' # today at 00:00 |
| 407 |
// $ TZ=America/New_York date -d 'j monday' # next monday at 00:00 |
| 408 |
// |
| 409 |
// Verified against GNU coreutils 9.7. |
| 410 |
#[rstest] |
| 411 |
#[case::local_zone_and_relative("j 1 day", "2026-08-30 11:30:45")] |
| 412 |
#[case::relative_then_local_zone("5 minutes j", "2026-08-29 11:35:45")] |
| 413 |
#[case::offset_and_relative("utc 1 day", "2026-08-30 11:30:45")] |
| 414 |
#[case::relative_then_offset("5 minutes utc", "2026-08-29 11:35:45")] |
| 415 |
#[case::timezone_rule_and_relative("TZ=\"Europe/Paris\" 1 day", "2026-08-30 17:30:45")] |
| 416 |
// Without a relative item, a zone item still truncates to midnight. |
| 417 |
#[case::local_zone_alone("j", "2026-08-29 00:00:00")] |
| 418 |
#[case::local_zone_and_weekday("j monday", "2026-08-31 00:00:00")] |
| 419 |
#[case::offset_alone("utc", "2026-08-29 00:00:00")] |
| 420 |
fn test_zone_item_keeps_time_of_day_with_relative(#[case] input: &str, #[case] expected: &str) { |
| 421 |
let base = "2026-08-29 11:30:45" |
| 422 |
.parse::<DateTime>() |
| 423 |
.unwrap() |
| 424 |
.to_zoned(TimeZone::get("America/New_York").unwrap()) |
| 425 |
.unwrap(); |
| 426 |
|
| 427 |
let parsed = parse_datetime::parse_datetime_at_date(base, input) |
| 428 |
.unwrap() |
| 429 |
.expect_in_range(); |
| 430 |
|
| 431 |
assert_eq!( |
| 432 |
parsed.datetime().to_string(), |
| 433 |
expected.parse::<DateTime>().unwrap().to_string(), |
| 434 |
"`{input}` should resolve to {expected} in its own zone" |
| 435 |
); |
| 436 |
} |