/* ntime.c PUBILC DOMAIN 2006,2007,2008,2010,2018 t.yabaplib@purposeful.co.uk */ /* http:///www.purposeful.co.uk/software/yabaplib */ /* I, Tom Vajzovic, am the author of this software and its documentation. I permanently abandon all intellectual property rights in them, including copyright, trademarks, design rights, database right, patents, and the right to be identified as the author. I am fairly certain that the software does what the documentation says it does, but I do not guarantee that it does, or that it does what you think it should. I do not guarantee that it will not have undesirable side effects. You are free to use, modify and distribute this software as you please, but you do so at your own risk. If you do not pass on this warning then you may be responsible for any problems encountered by those who obtain the software through you. */ #include #include #include #include #include #include "ntime.h" #define THOUSAND 1000u #define MILLION 1000000u #define MILLIARD 1000000000u void nsleep(uint64_t nanoseconds) { struct timespec request = {(nanoseconds / MILLIARD), (nanoseconds % MILLIARD)}; struct timespec remainder; while (nanosleep(&request, &remainder)) { request = remainder; } } int64_t ntime( void ){ #if _POSIX_TIMERS { struct timespec tp; if( ! clock_gettime( CLOCK_REALTIME, & tp )) return ((int64_t)MILLIARD * tp. tv_sec) + tp. tv_nsec; } #endif /* _POSIX_TIMERS */ { struct timeval tv; if( ! gettimeofday( & tv, NULL )) return (((int64_t)MILLION * tv. tv_sec) + tv. tv_usec) * THOUSAND; } { int save_errno= errno; time_t t; errno= 0; t= time( NULL ); if( -1 == t && errno ) return -1; errno= save_errno; return (int64_t)MILLIARD * t; } } #ifdef _POSIX_MONOTONIC_CLOCK uint64_t ntime_mono( void ){ struct timespec tp; if( clock_gettime( CLOCK_MONOTONIC, & tp )) return 0; return MILLIARD * (uint64_t) tp. tv_sec + (uint64_t) tp. tv_nsec; } #else /* _POSIX_MONOTONIC_CLOCK */ uint64_t ntime_mono( void ){ int64_t t= ntime(); return 0 > t ? 0 : t; } #endif /* _POSIX_MONOTONIC_CLOCK */