⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 time.c

📁 linux环境下用纯c写的RTP协议的通用开发库
💻 C
字号:
/* time.c - timeadd, timesub, timeflatten, timeunflatten, timecmp */#include <util.h>#include <time.h>/*----------------------------------------------------------------------- * timeadd - add two struct timespecs *----------------------------------------------------------------------- */struct timespectimeadd(struct timespec a, struct timespec b){	b.tv_sec = -b.tv_sec;	b.tv_nsec = -b.tv_nsec;  	return timesub(a, b);}/*----------------------------------------------------------------------- * timesub - subtract struct timespec b from a *----------------------------------------------------------------------- */struct timespectimesub(struct timespec a, struct timespec b){	struct timespec c;  	c.tv_sec = a.tv_sec - b.tv_sec;	c.tv_nsec = a.tv_nsec - b.tv_nsec;  	/* borrow if necessary */	if (c.tv_nsec < 0) {		c.tv_nsec += 1000000000;		c.tv_sec--;	}	else if (c.tv_nsec > 1000000000) {		c.tv_nsec -= 1000000000;		c.tv_sec++;	}	return c;}/*----------------------------------------------------------------------- * timeflatten - convert a struct timespec to a flat time value *----------------------------------------------------------------------- */mediatime_t timeflatten(struct timespec t, int clkrt){	int rv;	double d;	rv = t.tv_sec * clkrt;	d = (double) t.tv_nsec * .000000001;	d *= (double) clkrt;	rv += (int) d;	return rv;}/*------------------------------------------------------------------------ * timeunflatten - convert a flat time value to a struct timespec *------------------------------------------------------------------------ */struct timespectimeunflatten(mediatime_t t, int clkrt){	struct timespec	ts;	double	d;	ts.tv_sec = t / clkrt;	d = ((double) (t % clkrt)) / ((double) clkrt);	ts.tv_nsec = (int) (d * (double) 1000000000);	return ts;}/*------------------------------------------------------------------------ * timecmp - compare two strucxt timespecs. Return -1 if a is after b,  * 1 if b is after a, or 0 if equal. Yes, this is backward wrt to  * convention. *------------------------------------------------------------------------ */inttimecmp(struct timespec a, struct timespec b){  	if (a.tv_sec < b.tv_sec)		return 1;  	if (a.tv_sec > b.tv_sec)		return -1;    	if (a.tv_nsec < b.tv_nsec)		return 1;  	if (a.tv_nsec > b.tv_nsec)		return -1;	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -