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
8 changes: 5 additions & 3 deletions src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use parse_datetime::{ExtendedDateTime, ParsedDateTime};
use std::borrow::Cow;
use std::collections::HashMap;
use std::ffi::OsString;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Read, Write, stderr};
use std::path::PathBuf;
Expand All @@ -32,9 +33,9 @@

use uucore::parser::shortcut_value_parser::ShortcutValueParser;

/// OHOS helper: pass through the system time zone ID returned by

Check warning on line 36 in src/uu/date/src/date.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'OHOS' (file:'src/uu/date/src/date.rs', line:36)
/// TimeService (OH_TimeService_GetTimeZone, e.g. "Asia/Shanghai") and
/// resolve it against the embedded IANA tzdata (jiff-tzdb) so that

Check warning on line 38 in src/uu/date/src/date.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'tzdb' (file:'src/uu/date/src/date.rs', line:38)

Check warning on line 38 in src/uu/date/src/date.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'tzdata' (file:'src/uu/date/src/date.rs', line:38)
/// historial DST rules and transitions are preserved. jiff's
/// `try_system()` is useless on OHOS because both `/etc/localtime` and
/// the zoneinfo dirs are absent.
Expand Down Expand Up @@ -343,7 +344,7 @@
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uucore::clap_localization::handle_clap_result(uu_app(), args)?;

let date_source = if let Some(date_os) = matches.get_one::<std::ffi::OsString>(OPT_DATE) {
let date_source = if let Some(date_os) = matches.get_one::<OsString>(OPT_DATE) {
// Convert OsString to String, handling invalid UTF-8 with GNU-compatible error
let date = date_os.to_str().ok_or_else(|| {
let bytes = date_os.as_encoded_bytes();
Expand All @@ -356,7 +357,7 @@
"-" => DateSource::Stdin,
_ => DateSource::File(file.into()),
}
} else if let Some(file) = matches.get_one::<String>(OPT_REFERENCE) {
} else if let Some(file) = matches.get_one::<OsString>(OPT_REFERENCE) {
DateSource::FileMtime(file.into())
} else if matches.get_flag(OPT_RESOLUTION) {
DateSource::Resolution
Expand Down Expand Up @@ -691,7 +692,7 @@
.value_name("STRING")
.allow_hyphen_values(true)
.overrides_with(OPT_DATE)
.value_parser(clap::value_parser!(std::ffi::OsString))
.value_parser(clap::value_parser!(OsString))
.help(translate!("date-help-date")),
)
.arg(
Expand Down Expand Up @@ -752,6 +753,7 @@
.long(OPT_REFERENCE)
.value_name("FILE")
.value_hint(clap::ValueHint::AnyPath)
.value_parser(clap::value_parser!(OsString))
.conflicts_with_all([OPT_DATE, OPT_FILE, OPT_RESOLUTION])
.overrides_with(OPT_REFERENCE)
.help(translate!("date-help-reference")),
Expand Down
21 changes: 21 additions & 0 deletions tests/by-util/test_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,27 @@ fn test_date_for_file_mtime() {
.stdout_only("1234\n");
}

#[test]
#[cfg(target_os = "linux")]
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
fn test_date_reference_is_non_utf8_path() {
use std::os::unix::ffi::OsStrExt;
use std::time::{Duration, UNIX_EPOCH};

let (at, mut ucmd) = at_and_ucmd!();

let reference_file = std::ffi::OsStr::from_bytes(b"reference_\xFF\xFE.txt");
let f = std::fs::File::create(at.plus(reference_file)).unwrap();
let modification_date = UNIX_EPOCH.checked_add(Duration::from_secs(1234)).unwrap();
f.set_modified(modification_date).unwrap();

ucmd.arg("--reference")
.arg(reference_file)
.arg("+%s")
.succeeds()
.stdout_only("1234\n");
}

#[test]
fn test_date_multiple_references() {
use std::time::{Duration, UNIX_EPOCH};
Expand Down
Loading