Improve some timespec functions

This commit is contained in:
Justine Tunney 2022-09-19 16:13:37 -07:00
parent 2395a9eced
commit 6e582d245b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
6 changed files with 16 additions and 10 deletions

View file

@ -19,13 +19,9 @@
#include "libc/assert.h"
#include "libc/calls/struct/timespec.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/consts/nr.h"
#include "libc/time/time.h"
struct timespec _timespec_mono(void) {
int ax, dx;
struct timespec ts;
ax = clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
assert(!ax);
_npassert(!clock_gettime(CLOCK_MONOTONIC_FAST, &ts));
return ts;
}

View file

@ -16,15 +16,12 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/calls/struct/timespec.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/consts/nr.h"
#include "libc/time/time.h"
struct timespec _timespec_real(void) {
int ax, dx;
struct timespec ts;
ax = clock_gettime(CLOCK_REALTIME_FAST, &ts);
if (ax) notpossible;
_npassert(!clock_gettime(CLOCK_REALTIME_FAST, &ts));
return ts;
}

View file

@ -27,6 +27,8 @@ int64_t _timespec_tomicros(struct timespec x) {
if (!__builtin_mul_overflow(x.tv_sec, 1000000ul, &us) &&
!__builtin_add_overflow(us, x.tv_nsec / 1000, &us)) {
return us;
} else if (x.tv_sec < 0) {
return INT64_MIN;
} else {
return INT64_MAX;
}

View file

@ -27,6 +27,8 @@ int64_t _timespec_tomillis(struct timespec x) {
if (!__builtin_mul_overflow(x.tv_sec, 1000ul, &ms) &&
!__builtin_add_overflow(ms, x.tv_nsec / 1000000, &ms)) {
return ms;
} else if (x.tv_sec < 0) {
return INT64_MIN;
} else {
return INT64_MAX;
}

View file

@ -27,6 +27,8 @@ int64_t _timespec_tonanos(struct timespec x) {
if (!__builtin_mul_overflow(x.tv_sec, 1000000000ul, &ns) &&
!__builtin_add_overflow(ns, x.tv_nsec, &ns)) {
return ns;
} else if (x.tv_sec < 0) {
return INT64_MIN;
} else {
return INT64_MAX;
}