mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
Merge pull request #9688 from xtqqczze/get_clock_resolution
date: remove unsafe
This commit is contained in:
Generated
+1
-1
@@ -3191,7 +3191,7 @@ dependencies = [
|
|||||||
"clap",
|
"clap",
|
||||||
"fluent",
|
"fluent",
|
||||||
"jiff",
|
"jiff",
|
||||||
"libc",
|
"nix",
|
||||||
"parse_datetime",
|
"parse_datetime",
|
||||||
"uucore",
|
"uucore",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
|
|||||||
Generated
+1
-1
@@ -1597,7 +1597,7 @@ dependencies = [
|
|||||||
"clap",
|
"clap",
|
||||||
"fluent",
|
"fluent",
|
||||||
"jiff",
|
"jiff",
|
||||||
"libc",
|
"nix",
|
||||||
"parse_datetime",
|
"parse_datetime",
|
||||||
"uucore",
|
"uucore",
|
||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ parse_datetime = { workspace = true }
|
|||||||
uucore = { workspace = true, features = ["parser"] }
|
uucore = { workspace = true, features = ["parser"] }
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
libc = { workspace = true }
|
nix = { workspace = true, features = ["time"] }
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
windows-sys = { workspace = true, features = [
|
windows-sys = { workspace = true, features = [
|
||||||
|
|||||||
+17
-33
@@ -9,10 +9,6 @@ use clap::{Arg, ArgAction, Command};
|
|||||||
use jiff::fmt::strtime;
|
use jiff::fmt::strtime;
|
||||||
use jiff::tz::{TimeZone, TimeZoneDatabase};
|
use jiff::tz::{TimeZone, TimeZoneDatabase};
|
||||||
use jiff::{Timestamp, Zoned};
|
use jiff::{Timestamp, Zoned};
|
||||||
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))]
|
|
||||||
use libc::clock_settime;
|
|
||||||
#[cfg(all(unix, not(target_os = "redox")))]
|
|
||||||
use libc::{CLOCK_REALTIME, clock_getres, timespec};
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
@@ -700,25 +696,20 @@ fn get_clock_resolution() -> Timestamp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(unix, not(target_os = "redox")))]
|
#[cfg(all(unix, not(target_os = "redox")))]
|
||||||
|
/// Returns the resolution of the system’s realtime clock.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if `clock_getres` fails. On a POSIX-compliant system this should not occur,
|
||||||
|
/// as `CLOCK_REALTIME` is required to be supported.
|
||||||
|
/// Failure would indicate a non-conforming or otherwise broken implementation.
|
||||||
fn get_clock_resolution() -> Timestamp {
|
fn get_clock_resolution() -> Timestamp {
|
||||||
let mut timespec = timespec {
|
use nix::time::{ClockId, clock_getres};
|
||||||
tv_sec: 0,
|
|
||||||
tv_nsec: 0,
|
let timespec = clock_getres(ClockId::CLOCK_REALTIME).unwrap();
|
||||||
};
|
|
||||||
unsafe {
|
|
||||||
// SAFETY: the timespec struct lives for the full duration of this function call.
|
|
||||||
//
|
|
||||||
// The clock_getres function can only fail if the passed clock_id is not
|
|
||||||
// a known clock. All compliant posix implementors must support
|
|
||||||
// CLOCK_REALTIME, therefore this function call cannot fail on any
|
|
||||||
// compliant posix implementation.
|
|
||||||
//
|
|
||||||
// See more here:
|
|
||||||
// https://pubs.opengroup.org/onlinepubs/9799919799/functions/clock_getres.html
|
|
||||||
clock_getres(CLOCK_REALTIME, &raw mut timespec);
|
|
||||||
}
|
|
||||||
#[allow(clippy::unnecessary_cast)] // Cast required on 32-bit platforms
|
#[allow(clippy::unnecessary_cast)] // Cast required on 32-bit platforms
|
||||||
Timestamp::constant(timespec.tv_sec as i64, timespec.tv_nsec as i32)
|
Timestamp::constant(timespec.tv_sec() as _, timespec.tv_nsec() as _)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(unix, target_os = "redox"))]
|
#[cfg(all(unix, target_os = "redox"))]
|
||||||
@@ -766,20 +757,13 @@ fn set_system_datetime(_date: Zoned) -> UResult<()> {
|
|||||||
/// `<https://linux.die.net/man/3/clock_settime>`
|
/// `<https://linux.die.net/man/3/clock_settime>`
|
||||||
/// `<https://www.gnu.org/software/libc/manual/html_node/Time-Types.html>`
|
/// `<https://www.gnu.org/software/libc/manual/html_node/Time-Types.html>`
|
||||||
fn set_system_datetime(date: Zoned) -> UResult<()> {
|
fn set_system_datetime(date: Zoned) -> UResult<()> {
|
||||||
|
use nix::{sys::time::TimeSpec, time::ClockId};
|
||||||
|
|
||||||
let ts = date.timestamp();
|
let ts = date.timestamp();
|
||||||
let timespec = timespec {
|
let timespec = TimeSpec::new(ts.as_second() as _, ts.subsec_nanosecond() as _);
|
||||||
tv_sec: ts.as_second() as _,
|
|
||||||
tv_nsec: ts.subsec_nanosecond() as _,
|
|
||||||
};
|
|
||||||
|
|
||||||
let result = unsafe { clock_settime(CLOCK_REALTIME, &raw const timespec) };
|
nix::time::clock_settime(ClockId::CLOCK_REALTIME, timespec)
|
||||||
|
.map_err_context(|| translate!("date-error-cannot-set-date"))
|
||||||
if result == 0 {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(std::io::Error::last_os_error()
|
|
||||||
.map_err_context(|| translate!("date-error-cannot-set-date")))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
|
|||||||
Reference in New Issue
Block a user