@@ -9,10 +9,6 @@ use clap::{Arg, ArgAction, Command};
99use jiff:: fmt:: strtime;
1010use jiff:: tz:: { TimeZone , TimeZoneDatabase } ;
1111use jiff:: { Timestamp , Zoned } ;
12- #[ cfg( all( unix, not( target_os = "macos" ) , not( target_os = "redox" ) ) ) ]
13- use libc:: clock_settime;
14- #[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
15- use libc:: { CLOCK_REALTIME , clock_getres, timespec} ;
1612use std:: collections:: HashMap ;
1713use std:: fs:: File ;
1814use std:: io:: { BufRead , BufReader } ;
@@ -700,25 +696,20 @@ fn get_clock_resolution() -> Timestamp {
700696}
701697
702698#[ cfg( all( unix, not( target_os = "redox" ) ) ) ]
699+ /// Returns the resolution of the system’s realtime clock.
700+ ///
701+ /// # Panics
702+ ///
703+ /// Panics if `clock_getres` fails. On a POSIX-compliant system this should not occur,
704+ /// as `CLOCK_REALTIME` is required to be supported.
705+ /// Failure would indicate a non-conforming or otherwise broken implementation.
703706fn get_clock_resolution ( ) -> Timestamp {
704- let mut timespec = timespec {
705- tv_sec : 0 ,
706- tv_nsec : 0 ,
707- } ;
708- unsafe {
709- // SAFETY: the timespec struct lives for the full duration of this function call.
710- //
711- // The clock_getres function can only fail if the passed clock_id is not
712- // a known clock. All compliant posix implementors must support
713- // CLOCK_REALTIME, therefore this function call cannot fail on any
714- // compliant posix implementation.
715- //
716- // See more here:
717- // https://pubs.opengroup.org/onlinepubs/9799919799/functions/clock_getres.html
718- clock_getres ( CLOCK_REALTIME , & raw mut timespec) ;
719- }
707+ use nix:: time:: { ClockId , clock_getres} ;
708+
709+ let timespec = clock_getres ( ClockId :: CLOCK_REALTIME ) . unwrap ( ) ;
710+
720711 #[ allow( clippy:: unnecessary_cast) ] // Cast required on 32-bit platforms
721- Timestamp :: constant ( timespec. tv_sec as i64 , timespec. tv_nsec as i32 )
712+ Timestamp :: constant ( timespec. tv_sec ( ) as _ , timespec. tv_nsec ( ) as _ )
722713}
723714
724715#[ cfg( all( unix, target_os = "redox" ) ) ]
@@ -766,20 +757,13 @@ fn set_system_datetime(_date: Zoned) -> UResult<()> {
766757/// `<https://linux.die.net/man/3/clock_settime>`
767758/// `<https://www.gnu.org/software/libc/manual/html_node/Time-Types.html>`
768759fn set_system_datetime ( date : Zoned ) -> UResult < ( ) > {
769- let ts = date. timestamp ( ) ;
770- let timespec = timespec {
771- tv_sec : ts. as_second ( ) as _ ,
772- tv_nsec : ts. subsec_nanosecond ( ) as _ ,
773- } ;
760+ use nix:: { sys:: time:: TimeSpec , time:: ClockId } ;
774761
775- let result = unsafe { clock_settime ( CLOCK_REALTIME , & raw const timespec) } ;
762+ let ts = date. timestamp ( ) ;
763+ let timespec = TimeSpec :: new ( ts. as_second ( ) as _ , ts. subsec_nanosecond ( ) as _ ) ;
776764
777- if result == 0 {
778- Ok ( ( ) )
779- } else {
780- Err ( std:: io:: Error :: last_os_error ( )
781- . map_err_context ( || translate ! ( "date-error-cannot-set-date" ) ) )
782- }
765+ nix:: time:: clock_settime ( ClockId :: CLOCK_REALTIME , timespec)
766+ . map_err_context ( || translate ! ( "date-error-cannot-set-date" ) )
783767}
784768
785769#[ cfg( windows) ]
0 commit comments