File: timespec.cpp

package info (click to toggle)
cubemap 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 428 kB
  • sloc: cpp: 4,431; sh: 114; perl: 102; makefile: 60
file content (28 lines) | stat: -rw-r--r-- 561 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <assert.h>
#include <sys/time.h>

#include "timespec.h"

timespec clock_diff(const timespec &a, const timespec &b)
{
	timespec ret;
	ret.tv_sec = b.tv_sec - a.tv_sec;
	ret.tv_nsec = b.tv_nsec - a.tv_nsec;
	if (ret.tv_nsec < 0) {
		ret.tv_sec--;
		ret.tv_nsec += 1000000000;
	}
	assert(ret.tv_nsec >= 0);
	return ret;
}

timespec clock_add(const timespec &a, const timespec &b)
{
	timespec ret;
	ret.tv_sec = a.tv_sec + b.tv_sec;
	ret.tv_nsec = b.tv_nsec + b.tv_nsec;

	ret.tv_sec += ret.tv_nsec / 1000000000;
	ret.tv_nsec %= 1000000000;
	return ret;
}