Skip to content

Commit 5cae9ae

Browse files
committed
date: -d empty string should be treated as midnight today
1 parent 2845546 commit 5cae9ae

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/uu/date/src/date.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
205205
// Iterate over all dates - whether it's a single date or a file.
206206
let dates: Box<dyn Iterator<Item = _>> = match settings.date_source {
207207
DateSource::Human(ref input) => {
208+
let input = input.trim();
209+
// GNU compatibility (Empty string):
210+
// An empty string (or whitespace-only) should be treated as midnight today.
211+
let is_empty_or_whitespace = input.is_empty();
212+
208213
// GNU compatibility (Military timezone 'J'):
209214
// 'J' is reserved for local time in military timezones.
210215
// GNU date accepts it and treats it as midnight today (00:00:00).
@@ -218,8 +223,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
218223
let is_pure_digits =
219224
!input.is_empty() && input.len() <= 4 && input.chars().all(|c| c.is_ascii_digit());
220225

221-
let date = if is_military_j {
222-
// Treat 'J' as midnight today (00:00:00) in local time
226+
let date = if is_empty_or_whitespace || is_military_j {
227+
// Treat empty string or 'J' as midnight today (00:00:00) in local time
223228
let date_part =
224229
strtime::format("%F", &now).unwrap_or_else(|_| String::from("1970-01-01"));
225230
let offset = if settings.utc {

tests/by-util/test_date.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,7 @@ fn test_date_military_timezone_j_variations() {
973973
}
974974

975975
#[test]
976-
#[ignore = "we use current time, GNU uses midnight"]
977-
fn test_date_fuzz_empty_string() {
976+
fn test_date_empty_string() {
978977
// Empty string should be treated as midnight today
979978
new_ucmd!()
980979
.env("TZ", "UTC+1")
@@ -984,6 +983,33 @@ fn test_date_fuzz_empty_string() {
984983
.stdout_contains("00:00:00");
985984
}
986985

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+
9871013
#[test]
9881014
#[ignore = "we produce year 0008, GNU gives today 12:00"]
9891015
fn test_date_fuzz_relative_m9() {

0 commit comments

Comments
 (0)