Skip to content

Commit b3d7f8f

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 0c8a3c7 commit b3d7f8f

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
@@ -46,6 +46,9 @@ uucore = { workspace = true, features = ["parser", "i18n-datetime"] }
4646
libc = { workspace = true }
4747
rustix = { workspace = true, features = ["time"] }
4848

49+
[target.'cfg(target_os = "wasi")'.dependencies]
50+
libc = { workspace = true }
51+
4952
[target.'cfg(windows)'.dependencies]
5053
windows-sys = { workspace = true, features = [
5154
"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
@@ -1130,7 +1130,23 @@ fn parse_date<S: AsRef<str>>(
11301130
}
11311131
}
11321132

1133-
#[cfg(not(any(unix, windows)))]
1133+
#[cfg(target_os = "wasi")]
1134+
/// Returns the resolution of the system's realtime clock.
1135+
///
1136+
/// `rustix::time::clock_getres` excludes WASI, so call `libc::clock_getres`
1137+
/// (available on WASI) directly instead.
1138+
fn get_clock_resolution() -> Timestamp {
1139+
let timespec = unsafe {
1140+
let mut timespec: libc::timespec = std::mem::zeroed();
1141+
libc::clock_getres(libc::CLOCK_REALTIME, &raw mut timespec);
1142+
timespec
1143+
};
1144+
1145+
#[allow(clippy::unnecessary_cast, reason = "needed for 32 bit target")]
1146+
Timestamp::constant(timespec.tv_sec as _, timespec.tv_nsec as _)
1147+
}
1148+
1149+
#[cfg(not(any(unix, windows, target_os = "wasi")))]
11341150
fn get_clock_resolution() -> Timestamp {
11351151
unimplemented!("getting clock resolution not implemented (unsupported target)");
11361152
}
@@ -1169,7 +1185,7 @@ fn get_clock_resolution() -> Timestamp {
11691185
Timestamp::constant(0, 100)
11701186
}
11711187

1172-
#[cfg(not(any(unix, windows)))]
1188+
#[cfg(not(any(unix, windows, target_os = "wasi")))]
11731189
fn set_system_datetime(_date: Zoned) -> UResult<()> {
11741190
unimplemented!("setting date not implemented (unsupported target)");
11751191
}
@@ -1191,6 +1207,15 @@ fn set_system_datetime(_date: Zoned) -> UResult<()> {
11911207
))
11921208
}
11931209

1210+
#[cfg(target_os = "wasi")]
1211+
/// The WASI sandbox has no syscall for setting the wall clock.
1212+
fn set_system_datetime(_date: Zoned) -> UResult<()> {
1213+
Err(USimpleError::new(
1214+
1,
1215+
translate!("date-error-setting-date-not-supported-wasi"),
1216+
))
1217+
}
1218+
11941219
#[cfg(target_os = "redox")]
11951220
fn set_system_datetime(_date: Zoned) -> UResult<()> {
11961221
Err(USimpleError::new(

0 commit comments

Comments
 (0)