Skip to content

Commit 870bc29

Browse files
authored
Merge pull request #9022 from sylvestre/test-ignored-date
date: improve compat with GNU
2 parents 0be1ac8 + d87a5b8 commit 870bc29

File tree

2 files changed

+218
-2
lines changed

2 files changed

+218
-2
lines changed

src/uu/date/src/date.rs

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,57 @@ impl From<&str> for Rfc3339Format {
117117
}
118118
}
119119

120+
/// Parse military timezone with optional hour offset.
121+
/// Pattern: single letter (a-z except j) optionally followed by 1-2 digits.
122+
/// Returns Some(total_hours_in_utc) or None if pattern doesn't match.
123+
///
124+
/// Military timezone mappings:
125+
/// - A-I: UTC+1 to UTC+9 (J is skipped for local time)
126+
/// - K-M: UTC+10 to UTC+12
127+
/// - N-Y: UTC-1 to UTC-12
128+
/// - Z: UTC+0
129+
///
130+
/// The hour offset from digits is added to the base military timezone offset.
131+
/// Examples: "m" -> 12 (noon UTC), "m9" -> 21 (9pm UTC), "a5" -> 4 (4am UTC next day)
132+
fn parse_military_timezone_with_offset(s: &str) -> Option<i32> {
133+
if s.is_empty() || s.len() > 3 {
134+
return None;
135+
}
136+
137+
let mut chars = s.chars();
138+
let letter = chars.next()?.to_ascii_lowercase();
139+
140+
// Check if first character is a letter (a-z, except j which is handled separately)
141+
if !letter.is_ascii_lowercase() || letter == 'j' {
142+
return None;
143+
}
144+
145+
// Parse optional digits (1-2 digits for hour offset)
146+
let additional_hours: i32 = if let Some(rest) = chars.as_str().chars().next() {
147+
if !rest.is_ascii_digit() {
148+
return None;
149+
}
150+
chars.as_str().parse().ok()?
151+
} else {
152+
0
153+
};
154+
155+
// Map military timezone letter to UTC offset
156+
let tz_offset = match letter {
157+
'a'..='i' => (letter as i32 - 'a' as i32) + 1, // A=+1, B=+2, ..., I=+9
158+
'k'..='m' => (letter as i32 - 'k' as i32) + 10, // K=+10, L=+11, M=+12
159+
'n'..='y' => -((letter as i32 - 'n' as i32) + 1), // N=-1, O=-2, ..., Y=-12
160+
'z' => 0, // Z=+0
161+
_ => return None,
162+
};
163+
164+
// Calculate total hours: midnight (0) + tz_offset + additional_hours
165+
// Midnight in timezone X converted to UTC
166+
let total_hours = (0 - tz_offset + additional_hours).rem_euclid(24);
167+
168+
Some(total_hours)
169+
}
170+
120171
#[uucore::main]
121172
#[allow(clippy::cognitive_complexity)]
122173
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
@@ -205,15 +256,54 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
205256
// Iterate over all dates - whether it's a single date or a file.
206257
let dates: Box<dyn Iterator<Item = _>> = match settings.date_source {
207258
DateSource::Human(ref input) => {
259+
let input = input.trim();
260+
// GNU compatibility (Empty string):
261+
// An empty string (or whitespace-only) should be treated as midnight today.
262+
let is_empty_or_whitespace = input.is_empty();
263+
264+
// GNU compatibility (Military timezone 'J'):
265+
// 'J' is reserved for local time in military timezones.
266+
// GNU date accepts it and treats it as midnight today (00:00:00).
267+
let is_military_j = input.eq_ignore_ascii_case("j");
268+
269+
// GNU compatibility (Military timezone with optional hour offset):
270+
// Single letter (a-z except j) optionally followed by 1-2 digits.
271+
// Letter represents midnight in that military timezone (UTC offset).
272+
// Digits represent additional hours to add.
273+
// Examples: "m" -> noon UTC (12:00); "m9" -> 21:00 UTC; "a5" -> 04:00 UTC
274+
let military_tz_with_offset = parse_military_timezone_with_offset(input);
275+
208276
// GNU compatibility (Pure numbers in date strings):
209277
// - Manual: https://www.gnu.org/software/coreutils/manual/html_node/Pure-numbers-in-date-strings.html
210-
// - Semantics: a pure decimal number denotes todays time-of-day (HH or HHMM).
278+
// - Semantics: a pure decimal number denotes today's time-of-day (HH or HHMM).
211279
// Examples: "0"/"00" => 00:00 today; "7"/"07" => 07:00 today; "0700" => 07:00 today.
212280
// For all other forms, fall back to the general parser.
213281
let is_pure_digits =
214282
!input.is_empty() && input.len() <= 4 && input.chars().all(|c| c.is_ascii_digit());
215283

216-
let date = if is_pure_digits {
284+
let date = if is_empty_or_whitespace || is_military_j {
285+
// Treat empty string or 'J' as midnight today (00:00:00) in local time
286+
let date_part =
287+
strtime::format("%F", &now).unwrap_or_else(|_| String::from("1970-01-01"));
288+
let offset = if settings.utc {
289+
String::from("+00:00")
290+
} else {
291+
strtime::format("%:z", &now).unwrap_or_default()
292+
};
293+
let composed = if offset.is_empty() {
294+
format!("{date_part} 00:00")
295+
} else {
296+
format!("{date_part} 00:00 {offset}")
297+
};
298+
parse_date(composed)
299+
} else if let Some(total_hours) = military_tz_with_offset {
300+
// Military timezone with optional hour offset
301+
// Convert to UTC time: midnight + military_tz_offset + additional_hours
302+
let date_part =
303+
strtime::format("%F", &now).unwrap_or_else(|_| String::from("1970-01-01"));
304+
let composed = format!("{date_part} {total_hours:02}:00:00 +00:00");
305+
parse_date(composed)
306+
} else if is_pure_digits {
217307
// Derive HH and MM from the input
218308
let (hh_opt, mm_opt) = if input.len() <= 2 {
219309
(input.parse::<u32>().ok(), Some(0u32))
@@ -717,3 +807,24 @@ fn set_system_datetime(date: Zoned) -> UResult<()> {
717807
Ok(())
718808
}
719809
}
810+
811+
#[cfg(test)]
812+
mod tests {
813+
use super::*;
814+
815+
#[test]
816+
fn test_parse_military_timezone_with_offset() {
817+
// Valid cases: letter only, letter + digit, uppercase
818+
assert_eq!(parse_military_timezone_with_offset("m"), Some(12)); // UTC+12 -> 12:00 UTC
819+
assert_eq!(parse_military_timezone_with_offset("m9"), Some(21)); // 12 + 9 = 21
820+
assert_eq!(parse_military_timezone_with_offset("a5"), Some(4)); // 23 + 5 = 28 % 24 = 4
821+
assert_eq!(parse_military_timezone_with_offset("z"), Some(0)); // UTC+0 -> 00:00 UTC
822+
assert_eq!(parse_military_timezone_with_offset("M9"), Some(21)); // Uppercase works
823+
824+
// Invalid cases: 'j' reserved, empty, too long, starts with digit
825+
assert_eq!(parse_military_timezone_with_offset("j"), None); // Reserved for local time
826+
assert_eq!(parse_military_timezone_with_offset(""), None); // Empty
827+
assert_eq!(parse_military_timezone_with_offset("m999"), None); // Too long
828+
assert_eq!(parse_military_timezone_with_offset("9m"), None); // Starts with digit
829+
}
830+
}

tests/by-util/test_date.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,3 +944,108 @@ fn test_date_tz_abbreviation_unknown() {
944944
.fails()
945945
.stderr_contains("invalid date");
946946
}
947+
948+
#[test]
949+
fn test_date_military_timezone_j_variations() {
950+
// Test multiple variations of 'J' input (case insensitive, with whitespace)
951+
// All should produce midnight (00:00:00)
952+
let test_cases = vec!["J", "j", " J ", " j ", "\tJ\t"];
953+
954+
for input in test_cases {
955+
new_ucmd!()
956+
.env("TZ", "UTC")
957+
.arg("-d")
958+
.arg(input)
959+
.arg("+%T")
960+
.succeeds()
961+
.stdout_is("00:00:00\n");
962+
}
963+
964+
// Test with -u flag to verify UTC behavior
965+
new_ucmd!()
966+
.arg("-u")
967+
.arg("-d")
968+
.arg("J")
969+
.arg("+%T %Z")
970+
.succeeds()
971+
.stdout_contains("00:00:00")
972+
.stdout_contains("UTC");
973+
}
974+
975+
#[test]
976+
fn test_date_empty_string() {
977+
// Empty string should be treated as midnight today
978+
new_ucmd!()
979+
.env("TZ", "UTC+1")
980+
.arg("-d")
981+
.arg("")
982+
.succeeds()
983+
.stdout_contains("00:00:00");
984+
}
985+
986+
#[test]
987+
fn test_date_empty_string_variations() {
988+
// Test multiple variations of empty/whitespace strings
989+
// All should produce midnight (00:00:00)
990+
let test_cases = vec!["", " ", " ", "\t", "\n", " \t ", "\t\n\t"];
991+
992+
for input in test_cases {
993+
new_ucmd!()
994+
.env("TZ", "UTC")
995+
.arg("-d")
996+
.arg(input)
997+
.arg("+%T")
998+
.succeeds()
999+
.stdout_is("00:00:00\n");
1000+
}
1001+
1002+
// Test with -u flag to verify UTC behavior
1003+
new_ucmd!()
1004+
.arg("-u")
1005+
.arg("-d")
1006+
.arg("")
1007+
.arg("+%T %Z")
1008+
.succeeds()
1009+
.stdout_contains("00:00:00")
1010+
.stdout_contains("UTC");
1011+
}
1012+
1013+
#[test]
1014+
fn test_date_relative_m9() {
1015+
// Military timezone "m9" should be parsed as noon + 9 hours = 21:00 UTC
1016+
// When displayed in TZ=UTC+9 (which is UTC-9), this shows as 12:00 local time
1017+
new_ucmd!()
1018+
.env("TZ", "UTC+9")
1019+
.arg("-d")
1020+
.arg("m9")
1021+
.succeeds()
1022+
.stdout_contains("12:00:00");
1023+
}
1024+
1025+
#[test]
1026+
fn test_date_military_timezone_with_offset_variations() {
1027+
// Test various military timezone + offset combinations
1028+
// Format: single letter (a-z except j) optionally followed by 1-2 digits
1029+
1030+
// Test cases: (input, expected_time_utc)
1031+
let test_cases = vec![
1032+
("a", "23:00:00"), // A = UTC+1, midnight in UTC+1 = 23:00 UTC
1033+
("m", "12:00:00"), // M = UTC+12, midnight in UTC+12 = 12:00 UTC
1034+
("z", "00:00:00"), // Z = UTC+0, midnight in UTC+0 = 00:00 UTC
1035+
("m9", "21:00:00"), // M + 9 hours = 12 + 9 = 21:00 UTC
1036+
("a5", "04:00:00"), // A + 5 hours = 23 + 5 = 04:00 UTC (next day)
1037+
("z3", "03:00:00"), // Z + 3 hours = 00 + 3 = 03:00 UTC
1038+
("M", "12:00:00"), // Uppercase should work too
1039+
("A5", "04:00:00"), // Uppercase with offset
1040+
];
1041+
1042+
for (input, expected) in test_cases {
1043+
new_ucmd!()
1044+
.env("TZ", "UTC")
1045+
.arg("-d")
1046+
.arg(input)
1047+
.arg("+%T")
1048+
.succeeds()
1049+
.stdout_is(format!("{expected}\n"));
1050+
}
1051+
}

0 commit comments

Comments
 (0)