X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftime%2Ftime.h;fp=ccan%2Ftime%2Ftime.h;h=1ef62fef202502bc89c883e73ebbf861ca2fc23d;hb=2a91b14cf2eea39b32d17a0ab14dd7243f36b79f;hp=6fb622c6224b86d5daeeddcf73fb721398598a99;hpb=b980b856670208893a4ac597c3b2e8dec47f453d;p=ccan diff --git a/ccan/time/time.h b/ccan/time/time.h index 6fb622c6..1ef62fef 100644 --- a/ccan/time/time.h +++ b/ccan/time/time.h @@ -249,4 +249,25 @@ static inline struct timespec timeval_to_timespec(struct timeval tv) ts.tv_nsec = tv.tv_usec * 1000; return ts; } + +/** + * time_check - check if a time is malformed. + * @in: the time to check (returned) + * @abortstr: the string to print to stderr before aborting (if set). + * + * This can be used to make sure a time isn't negative and doesn't + * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL, + * that will be printed and abort() is called. Otherwise, if + * @abortstr is NULL then the returned timespec will be normalized and + * tv_sec set to 0 if it was negative. + * + * Note that if ccan/time is compiled with DEBUG, then it will call this + * for all passed and returned times. + * + * Example: + * printf("Now is %lu seconds since epoch\n", + * (long)time_check(time_now(), "time_now() failed?").tv_sec); + */ +struct timespec time_check(struct timespec in, const char *abortstr); + #endif /* CCAN_TIME_H */