Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ num-bigint = "0.4.4"
num-prime = "0.5.0"
num-traits = "0.2.19"
onig = { version = "~6.5.1", default-features = false }
parse_datetime = "0.14.0"
parse_datetime = "0.15.0"
phf = "0.14.0"
phf_codegen = "0.14.0"
platform-info = "2.0.3"
Expand Down
10 changes: 5 additions & 5 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 57 additions & 4 deletions src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore strtime ; (format) DATEFILE MMDDhhmm ; (vars) datetime datetimes getres AWST ACST AEST foobarbaz
// spell-checker:ignore strtime ; (format) DATEFILE MMDDhhmm ; (vars) datetime datetimes getres AWST ACST AEST foobarbaz unparseable

mod format_modifiers;
mod locale;
Expand Down Expand Up @@ -708,6 +708,44 @@ pub fn uu_app() -> Command {
.arg(Arg::new(OPT_FORMAT).num_args(0..))
}

/// Replace bare `%s` conversion specifiers in `fmt` with the Unix epoch second
/// using floor semantics.
///
/// GNU `date` rounds `%s` toward negative infinity for negative fractional
/// timestamps, whereas jiff's `%s` truncates toward zero. `%%` escapes are
/// preserved and every other specifier is left untouched for jiff to render.
fn substitute_epoch_seconds(fmt: &str, date: &Zoned) -> String {
if !fmt.contains("%s") {
return fmt.to_string();
}

let seconds = parse_datetime::ParsedDateTime::InRange(date.clone())
.unix_epoch_second()
.to_string();

let mut out = String::with_capacity(fmt.len());
let mut chars = fmt.chars().peekable();
while let Some(c) = chars.next() {
if c != '%' {
out.push(c);
continue;
}
match chars.peek() {
Some('s') => {
chars.next();
out.push_str(&seconds);
}
// Keep `%%` intact so jiff still renders it as a literal percent.
Some('%') => {
chars.next();
out.push_str("%%");
}
_ => out.push('%'),
}
}
out
}

fn format_date_with_locale_aware_months(
date: &Zoned,
format_string: &str,
Expand All @@ -727,6 +765,13 @@ fn format_date_with_locale_aware_months(
#[cfg(not(feature = "i18n-datetime"))]
let fmt = format_string;

// jiff renders `%s` by truncating toward zero, but GNU `date` floors toward
// negative infinity (e.g. `@-1.5` → `-2`, not `-1`). Every other field jiff
// produces already agrees with GNU, so only `%s` needs correcting; rewrite it
// to the floored epoch second before jiff sees the format string.
let fmt_owned = substitute_epoch_seconds(fmt, date);
let fmt = fmt_owned.as_str();

// Check if format string has GNU modifiers (width/flags) and format if present
if let Some(result) = format_modifiers::format_with_modifiers_if_present(date, fmt, config) {
return result.map_err(|e| e.to_string());
Expand Down Expand Up @@ -880,7 +925,7 @@ fn try_parse_with_abbreviation<S: AsRef<str>>(date_str: S, now: &Zoned) -> Optio

// Parse in the target timezone so "10:30 EDT" means 10:30 in EDT.
let parsed = parse_datetime::parse_datetime_at_date(now.clone(), date_part).ok()?;
let zoned = parsed.datetime().to_zoned(tz).ok()?;
let zoned = parsed.into_zoned()?.datetime().to_zoned(tz).ok()?;

// The trailing abbreviation only describes the *input* timezone. For display,
// re-zone to the system timezone (i.e. `now`'s zone, which is UTC under `-u`).
Expand Down Expand Up @@ -948,8 +993,16 @@ fn parse_date<S: AsRef<str> + Clone>(

match parse_datetime::parse_datetime_at_date(now.clone(), input_str) {
// Convert to system timezone for display
// (parse_datetime 0.13 returns Zoned in the input's timezone)
Ok(date) => {
// (parse_datetime returns a value in the input's timezone)
Ok(parsed) => {
// Out-of-range years (the `Extended` variant) can't be represented
// as a `Zoned`; treat them as an unparseable input.
let Some(date) = parsed.into_zoned() else {
return Err((
input_str.into(),
parse_datetime::ParseDateTimeError::InvalidInput,
));
};
let result = date.timestamp().to_zoned(now.time_zone().clone());
if dbg_opts.debug {
// Show final parsed date and time
Expand Down
6 changes: 4 additions & 2 deletions src/uu/touch/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,10 @@ fn parse_date(ref_zoned: Zoned, s: &str) -> Result<FileTime, TouchError> {
}
}

if let Ok(zoned) = parse_datetime::parse_datetime_at_date(ref_zoned, s) {
return Ok(timestamp_to_filetime(zoned.timestamp()));
if let Ok(parsed) = parse_datetime::parse_datetime_at_date(ref_zoned, s) {
if let Some(zoned) = parsed.into_zoned() {
return Ok(timestamp_to_filetime(zoned.timestamp()));
}
}

Err(TouchError::InvalidDateFormat(s.to_owned()))
Expand Down
47 changes: 32 additions & 15 deletions tests/by-util/test_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,33 +1898,50 @@ fn test_date_strftime_narrow_width_on_wide_default() {
}

#[test]
#[ignore = "https://github.com/uutils/parse_datetime/issues/283 — GNU date floors negative fractional epochs (`@-1.5` -> -2); uutils truncates toward zero (-> -1)."]
fn test_date_negative_fractional_epoch_flooring() {
new_ucmd!()
.env("LC_ALL", "C")
.env("TZ", "UTC")
.arg("-d")
.arg("@-1.5")
.arg("+%s")
.succeeds()
.stdout_is("-2\n");
// GNU date floors `%s` toward negative infinity for negative fractional
// epochs, while jiff truncates toward zero. See parse_datetime issue #283.
for (input, format, expected) in [
("@-1.5", "+%s", "-2\n"),
("@-0.25", "+%s", "-1\n"),
("@-2.75", "+%s.%N", "-3.250000000\n"),
("@-100.5", "+%s", "-101\n"),
// Positive fractions and whole seconds are unaffected.
("@42.9", "+%s", "42\n"),
("@-7", "+%s", "-7\n"),
// `%%s` stays a literal specifier and must not be substituted.
("@-1.5", "+%%s=%s", "%s=-2\n"),
] {
new_ucmd!()
.env("LC_ALL", "C")
.env("TZ", "UTC")
.arg("-d")
.arg(input)
.arg(format)
.succeeds()
.stdout_is(expected);
}
}

#[test]
#[ignore = "https://github.com/uutils/parse_datetime/issues/282 — parse_datetime rejects `HH:MM am/pm` forms (e.g. `2024-06-15 12:00 PM`, `2024-06-15 11:30am`). GNU date accepts them."]
fn test_date_input_hhmm_ampm() {
for input in [
"2024-06-15 12:00 PM",
"2024-06-15 11:30am",
"2024-06-15 3:00 PM",
// GNU date accepts a 12-hour meridiem suffix on a combined date+time.
// Regression test for https://github.com/uutils/parse_datetime/issues/282.
for (input, expected) in [
("2024-06-15 12:00 PM", "12:00\n"),
("2024-06-15 11:30am", "11:30\n"),
("2024-06-15 3:00 PM", "15:00\n"),
("2024-06-15 12:00 AM", "00:00\n"),
("2024-06-15 3:00 p.m.", "15:00\n"),
] {
new_ucmd!()
.env("LC_ALL", "C")
.env("TZ", "UTC")
.arg("-d")
.arg(input)
.arg("+%H:%M")
.succeeds();
.succeeds()
.stdout_is(expected);
}
}

Expand Down
Loading