码迷,mamicode.com
首页 > Windows程序 > 详细

跨平台的获取时间戳【win32和linux】

时间:2015-01-08 20:14:30      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

工作需要,就写了个时间戳获取方法,主要针对Win32和linux,理论IOS只要使用code编译出来的静态库即可[未尝试]

直接code

//头文件
#include <time.h>
#include <stdio.h>
#ifdef WIN32
	#include <sys/timeb.h>
#else
	#include <sys/time.h>
#endif


//功能实现
	//get the timestamp
	time_t tt = time(NULL);
	struct tm* ptr;
	ptr = localtime(&tt);
	printf("time: %d \n", tt);
	char str[80];
	strftime(str, sizeof(str),"%Y-%m-%d %H:%M:%S",ptr);
	char sec[50];
#ifdef WIN32
	struct	timeb	tp;
	ftime(&tp);
	printf("now time: %s.%03d \n", str, tp.millitm);
#else
	struct timeval tmv;
	gettimeofday(&tmv, NULL);
	printf("now time: %s.%03d \n", str, tmv.tv_usec/1000);
#endif


时间仓促,不太完善,请高手指导,也方便以后复用



跨平台的获取时间戳【win32和linux】

标签:

原文地址:http://blog.csdn.net/loovejava/article/details/42528425

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!