获取系统时间GetSystemTime、GetLocalTime、GetTickTime

在说时间之前先说一个经常要用的时间结构体,Linux中与windows一样
SYSTEMTIME 结构体

typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;

GetSystemTime是获取系统时间,UTC时间,其实是格林威治时间
GetLocalTime是获取当地时间,在格林威治时间上加上了东八区,就是增加了8小时的中国时区
SetLocalTime(LPSYSTEMTIME lpSystemTime)是设置系统时间

VOID GetLocalTime(LPSYSTEMTIME lpSystemTime);   //本地时间
VOID GetSystemTime(LPSYSTEMTIME lpSystemTime); //格林威治时间

参数说明:
lpSystemTime: 指向一个用户自定义包含日期和时间信息的类型为 SYSTEMTIME 的变量,该变量用来保存函数获取的时间信息。

GetTickTime()获取系统开机到现在的时间,以毫秒计算 2^32-1 = 42,9496,7295ms /1000ms/3600s/24h=49.710days=1.6month
如果时间超过50天,那么时间就要重新开始
GetTickTime64()是64位的
返回值是ULONGLONG表示的时间会很大

下过是我做的一个例子

#include 
#include 
#include 
#define TOTAL 100000

int compare(const void* a,const void* b)
{
	return *(int*)a - *(int*)b;
}

int main()
{
	DWORD time1 = 0;
	DWORD time2 = 0;

	SYSTEMTIME st;
	GetLocalTime(&st);

	printf("Local time:%d-%d-%d %d:%d:%d %dms\n",
		st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);

	GetSystemTime(&st);
	printf("system time:%d-%d-%d %d:%d:%d %dms\n",
		st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
	
	//Sleep(1000);

	st.wHour += 1;
	SetSystemTime(&st);
	
	time2 = GetTickCount();
	printf("start time:%d\n",time2);

	int nums[TOTAL];

	srand(GetTickCount());
	int index;
	for(index = 0; index < TOTAL;index++)
	{
		nums[index] = rand() % TOTAL;
		//printf("%d->>%d\n",index,nums[index]);
	}

	qsort(nums,TOTAL,sizeof(nums[0]),compare);

	//for(index = 0; index < TOTAL;index++)
	//{
		//nums[index] = rand() % TOTAL;
		//printf("%d->>%d\n",index,nums[index]);
	//}

	time1 = GetTickCount();
	printf("end time:%d\n",time1);

	printf("running time:%d ms\n",time1 - time2);
	

	return 0;
	
}

你可能感兴趣的:(win32,API)