Skip to content

Commit 088a4d0

Browse files
committed
fix(wasi): date clock resolution and system datetime support
date implements get_clock_resolution via libc::clock_getres (rustix's excludes WASI) and set_system_datetime for WASI, the latter reporting "not supported" since the sandbox has no wall-clock-set syscall.
1 parent fb4904b commit 088a4d0

4 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/uu/date/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ uucore = { workspace = true, features = ["parser", "i18n-datetime"] }
4343
libc = { workspace = true }
4444
rustix = { workspace = true, features = ["time"] }
4545

46+
[target.'cfg(target_os = "wasi")'.dependencies]
47+
libc = { workspace = true }
48+
4649
[target.'cfg(windows)'.dependencies]
4750
windows-sys = { workspace = true, features = [
4851
"Win32_Foundation",

src/uu/date/locales/en-US.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ date-error-expected-file-got-directory = expected file, got directory {$path}
103103
date-error-date-overflow = date overflow '{$date}'
104104
date-error-setting-date-not-supported-macos = setting the date is not supported by macOS
105105
date-error-setting-date-not-supported-redox = setting the date is not supported by Redox
106+
date-error-setting-date-not-supported-wasi = setting the date is not supported by WASI
106107
date-error-cannot-set-date = cannot set date
107108
date-error-extra-operand = extra operand '{$operand}'
108109
date-error-write = write error: {$error}

src/uu/date/locales/fr-FR.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ date-error-expected-file-got-directory = fichier attendu, répertoire obtenu {$p
9898
date-error-date-overflow = débordement de date '{$date}'
9999
date-error-setting-date-not-supported-macos = la définition de la date n'est pas prise en charge par macOS
100100
date-error-setting-date-not-supported-redox = la définition de la date n'est pas prise en charge par Redox
101+
date-error-setting-date-not-supported-wasi = la définition de la date n'est pas prise en charge par WASI
101102
date-error-cannot-set-date = impossible de définir la date
102103
date-error-extra-operand = opérande supplémentaire '{$operand}'
103104
date-error-write = erreur d'écriture: {$error}

src/uu/date/src/date.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,23 @@ fn parse_date<S: AsRef<str>>(
11731173
}
11741174
}
11751175

1176-
#[cfg(not(any(unix, windows)))]
1176+
#[cfg(target_os = "wasi")]
1177+
/// Returns the resolution of the system's realtime clock.
1178+
///
1179+
/// `rustix::time::clock_getres` excludes WASI, so call `libc::clock_getres`
1180+
/// (available on WASI) directly instead.
1181+
fn get_clock_resolution() -> Timestamp {
1182+
let timespec = unsafe {
1183+
let mut timespec: libc::timespec = std::mem::zeroed();
1184+
libc::clock_getres(libc::CLOCK_REALTIME, &raw mut timespec);
1185+
timespec
1186+
};
1187+
1188+
#[allow(clippy::unnecessary_cast, reason = "needed for 32 bit target")]
1189+
Timestamp::constant(timespec.tv_sec as _, timespec.tv_nsec as _)
1190+
}
1191+
1192+
#[cfg(not(any(unix, windows, target_os = "wasi")))]
11771193
fn get_clock_resolution() -> Timestamp {
11781194
unimplemented!("getting clock resolution not implemented (unsupported target)");
11791195
}
@@ -1212,7 +1228,7 @@ fn get_clock_resolution() -> Timestamp {
12121228
Timestamp::constant(0, 100)
12131229
}
12141230

1215-
#[cfg(not(any(unix, windows)))]
1231+
#[cfg(not(any(unix, windows, target_os = "wasi")))]
12161232
fn set_system_datetime(_date: Zoned) -> UResult<()> {
12171233
unimplemented!("setting date not implemented (unsupported target)");
12181234
}
@@ -1234,6 +1250,15 @@ fn set_system_datetime(_date: Zoned) -> UResult<()> {
12341250
))
12351251
}
12361252

1253+
#[cfg(target_os = "wasi")]
1254+
/// The WASI sandbox has no syscall for setting the wall clock.
1255+
fn set_system_datetime(_date: Zoned) -> UResult<()> {
1256+
Err(USimpleError::new(
1257+
1,
1258+
translate!("date-error-setting-date-not-supported-wasi"),
1259+
))
1260+
}
1261+
12371262
#[cfg(target_os = "redox")]
12381263
fn set_system_datetime(_date: Zoned) -> UResult<()> {
12391264
Err(USimpleError::new(

0 commit comments

Comments
 (0)