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

📄 tsmain.c

📁 p2p类源代码 kadc协议官方源代码
💻 C
字号:
#include <pthread.h>#ifdef __WIN32__#include <windows.h>#else#include <sys/time.h>#include <errno.h>#endifvoid ts_set(struct timespec *ts)  {#ifndef __WIN32__	struct timeval tv;	gettimeofday(&tv, NULL);	ts->tv_nsec = tv.tv_usec * 1000;	ts->tv_sec = tv.tv_sec;#else	FILETIME ft;	SYSTEMTIME st;#define TS_TO_FT_OFFSET (((LONGLONG)27111902 << 32) + ((LONGLONG)109181 << 15))	/* replacement for _ftime() that also work on on WinCE: see	   http://sources.redhat.com/ml/pthreads-win32/1999/msg00085.html */	GetSystemTime(&st);	SystemTimeToFileTime(&st, &ft);	/* Now convert filetime, where the time is expressed	   in 100 nanoseconds from Jan 1, 1601,to timespec,	   where the time is expressed in seconds and nanoseconds	   from Jan 1, 1970 */	ts->tv_sec = (long int)((*(LONGLONG *)&ft - TS_TO_FT_OFFSET) / 10000000);	ts->tv_nsec = (long int)((*(LONGLONG *)&ft - TS_TO_FT_OFFSET -					((LONGLONG)ts->tv_sec * (LONGLONG)10000000)) * 100);#endif	return;}/* returns ts1 - ts2 in milliseconds */long int millisdiff(struct timespec *ts1, struct timespec *ts2) {	long int diff, ms1, ms2;	ms1 = (ts1->tv_nsec + 500000L)/1000000L;	ms2 = (ts2->tv_nsec + 500000L)/1000000L;	diff = ms1 - ms2 + 1000 * (ts1->tv_sec - ts2->tv_sec);	return diff;}#include <stdio.h>#ifdef __WIN32__#define sleep(n) Sleep(1000*(n))#else#include <unistd.h>#endifint main(int ac, char *av[]){	struct timespec ts1;	struct timespec ts2;	ts_set(&ts1);	printf("ts_set(&ts1) set ts1 = %lu.%09lu s\n",			ts1.tv_sec, ts1.tv_nsec);	sleep(3);	ts_set(&ts2);	printf("ts_set(&ts2) set ts2 = %lu.%09lu s\n",			ts2.tv_sec, ts2.tv_nsec);	printf("millisdiff(&ts1, &ts2) = %ld\n",			millisdiff(&ts1, &ts2));	return 1;}

⌨️ 快捷键说明

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