#include <time.h>
#include <stdio.h>
struct timespec *timessub(const struct timespec *A, const struct timespec *B, struct timespec *C)
{
C->tv_sec = A->tv_sec - B->tv_sec;
C->tv_nsec = A->tv_nsec - B->tv_nsec;
if(C->tv_nsec<0){
C->tv_sec--;
C->tv_nsec += 1000000000;
}
return C;
}
struct timespec *timesadd(const struct timespec *A, const struct timespec *B, struct timespec *C)
{
C->tv_sec = A->tv_sec + B->tv_sec;
C->tv_nsec = A->tv_nsec + B->tv_nsec;
if(C->tv_nsec>=1000000000){
C->tv_sec++;
C->tv_nsec -= 1000000000;
}
return C;
}
int main()
{
struct timespec A = { 3, 500000000 };
struct timespec B = { 1, 600000000 };
struct timespec C;
timessub(&A,&B,&C);
printf("tv_sec = %ld\n", C.tv_sec);
printf("tv_nsec = %ld\n", C.tv_nsec);
timesadd(&A,&B,&C);
printf("tv_sec = %ld\n", C.tv_sec);
printf("tv_nsec = %ld\n", C.tv_nsec);
return 0;
}