lib: add timespec_diff_ms() and timespec_add_ms() helper functions
This functions will be used by isobusfs. Signed-off-by: Oleksij Rempel <linux@rempel-privat.de>pull/487/head
parent
c41f9cb21b
commit
6004c64f06
18
lib.c
18
lib.c
|
|
@ -704,3 +704,21 @@ void snprintf_can_error_frame(char *buf, size_t len, const struct canfd_frame *c
|
||||||
n += snprintf_error_cnt(buf + n, len - n, cf);
|
n += snprintf_error_cnt(buf + n, len - n, cf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int64_t timespec_diff_ms(struct timespec *ts1,
|
||||||
|
struct timespec *ts2)
|
||||||
|
{
|
||||||
|
int64_t diff = (ts1->tv_sec - ts2->tv_sec) * 1000;
|
||||||
|
|
||||||
|
diff += (ts1->tv_nsec - ts2->tv_nsec) / 1000000;
|
||||||
|
|
||||||
|
return diff;
|
||||||
|
}
|
||||||
|
|
||||||
|
void timespec_add_ms(struct timespec *ts, uint64_t milliseconds)
|
||||||
|
{
|
||||||
|
uint64_t total_ns = ts->tv_nsec + (milliseconds * 1000000);
|
||||||
|
|
||||||
|
ts->tv_sec += total_ns / 1000000000;
|
||||||
|
ts->tv_nsec = total_ns % 1000000000;
|
||||||
|
}
|
||||||
|
|
|
||||||
16
lib.h
16
lib.h
|
|
@ -229,4 +229,20 @@ void snprintf_can_error_frame(char *buf, size_t len, const struct canfd_frame *c
|
||||||
* Creates a CAN error frame output in user readable format.
|
* Creates a CAN error frame output in user readable format.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* timespec_diff_ms - calculate timespec difference in milliseconds
|
||||||
|
* @ts1: first timespec
|
||||||
|
* @ts2: second timespec
|
||||||
|
*
|
||||||
|
* Return negative difference if in the past.
|
||||||
|
*/
|
||||||
|
int64_t timespec_diff_ms(struct timespec *ts1, struct timespec *ts2);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* timespec_add_ms - add milliseconds to timespec
|
||||||
|
* @ts: timespec
|
||||||
|
* @milliseconds: milliseconds to add
|
||||||
|
*/
|
||||||
|
void timespec_add_ms(struct timespec *ts, uint64_t milliseconds);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue