C 获取时间

time()提供了秒级的精确度  

1、头文件 <time.h>  

2、函数原型  

time_t time(time_t * timer)   

函数返回从UTC1970-1-1 0:0:0开始到现在的秒数  

用time()函数结合其他函数(如:localtime)可以获得当前系统时间或是标准时间。  

C 函数 struct tm *localtime(const time_t *timer) 使用 timer 来填充 tm timer 被分解 tm 构,并用本地区表示

 

_ftime()提供毫秒的精确度 

1文件 <sys/types.h> and <sys/timeb.h>   

2、函数原型  

void _ftime(struct _timeb *timeptr);   

参数明:  

    struct _timeb   

    {  

        time_t time;  

        unsigned short millitm;  

        short timezone;  

        short dstflag;  

    };   

GetLocalTime是一个Windows API 函数,用来取当地的当前系日期和时间

代码:

void testTimeFmt()
{
	//-------------------------------------------------------------
	char	strCurrTimeFmt[256];
	time_t 	nTime;
	struct 	tm  *stTm;
	struct _timeb timebuffer;	
	//-------------------------------------------------------------
	char	strCurrTime[256];
	SYSTEMTIME currentTime;
	//-------------------------------------------------------------
	char strCurrDateFmt[256];
	char* pAddr = NULL;
	//-------------------------------------------------------------
	printf("--------- funcname = %s ---------\n", __func__);

	//init
	memset(strCurrTimeFmt, 0, sizeof(strCurrTimeFmt));
	memset(strCurrTime, 0, sizeof(strCurrTime));
	memset(strCurrDateFmt, 0, sizeof(strCurrDateFmt));

	GetLocalTime(&currentTime);

	nTime = time(NULL); //获取系统时间,单位为秒;这一句也可以改成time(&timer);  
	stTm = localtime((const time_t *)&nTime);//转换成tm类型的结构体;
	_ftime(&timebuffer);		

	sprintf(strCurrTimeFmt, "%04d/%02d/%02d %02d:%02d:%02d.%03d",
		1900 + stTm->tm_year,		stTm->tm_mon + 1,		stTm->tm_mday,
		stTm->tm_hour,		stTm->tm_min,		stTm->tm_sec,
		timebuffer.millitm);

	sprintf(strCurrTime, "%u/%02u/%02u %02u:%02u:%02u.%03u",
		currentTime.wYear,		currentTime.wMonth,		currentTime.wDay,
		currentTime.wHour,		currentTime.wMinute,		currentTime.wSecond,
		currentTime.wMilliseconds);

	memcpy(strCurrDateFmt, strCurrTime, sizeof(strCurrDateFmt));
	pAddr = strtok(strCurrDateFmt, " ");

	printf("%s\n", strCurrTimeFmt);
	printf("%s\n", strCurrTime);
	printf("%s\n", pAddr);

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值