X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftime%2Ftime.h;h=e4298d07055d1ab66ad816a042ede0bcf9720b26;hp=5bb1ad5b5cfaafc103cd780fb485fe406e40c820;hb=fe5952874bfb47b8d2c087c93d2149aea6ec2f44;hpb=96a1ebd3354ef6250e94b509b4e0c0f1ea7e67bb diff --git a/ccan/time/time.h b/ccan/time/time.h index 5bb1ad5b..e4298d07 100644 --- a/ccan/time/time.h +++ b/ccan/time/time.h @@ -210,6 +210,24 @@ struct timespec time_divide(struct timespec t, unsigned long div); */ struct timespec time_multiply(struct timespec t, unsigned long mult); +/** + * time_to_sec - return number of seconds + * @t: a time + * + * It's often more convenient to deal with time values as seconds. + * Note that this will fit into an unsigned 32-bit variable if it's a + * time of less than about 136 years. + * + * Example: + * ... + * printf("Forking time is %u sec\n", + * (unsigned)time_to_sec(forking_time())); + */ +static inline uint64_t time_to_sec(struct timespec t) +{ + return t.tv_sec; +} + /** * time_to_msec - return number of milliseconds * @t: a time @@ -275,6 +293,23 @@ static inline uint64_t time_to_nsec(struct timespec t) return nsec; } +/** + * time_from_sec - convert seconds to a timespec + * @msec: time in seconds + * + * Example: + * // 1 minute timeout + * #define TIMEOUT time_from_sec(60) + */ +static inline struct timespec time_from_sec(uint64_t sec) +{ + struct timespec t; + + t.tv_nsec = 0; + t.tv_sec = sec; + return TIME_CHECK(t); +} + /** * time_from_msec - convert milliseconds to a timespec * @msec: time in milliseconds