平台移植的各种情况

  这两天在做平台移植,程序从windows平台移植到linux下运行,着实麻烦哦~

时间获取部分:localtime_s和localtime_r的参数是相反的,囧。。。。

#ifndef linux

			localtime_s(&tm_now,&tNow);

#else

			localtime_r(&tNow,&tm_now);

#endif

  

对于systemtime 和tm 以及time_t 的转换

//	功能:	linux版的GetLocalTime获取本地时间
void GetLocalTime(SYSTEMTIME* lpSystemTime)
{
	struct timeval tv;
	gettimeofday (&tv , NULL);

	tm tm_now ;
	localtime_r(&tv.tv_sec,&tm_now);

	lpSystemTime->wYear = tm_now.tm_year+1900;			//年份
	lpSystemTime->wMonth = tm_now.tm_mon+1;				//月 tm[0-11] sys[1-12]
	lpSystemTime->wDay = tm_now.tm_mday;						//日
	lpSystemTime->wDayOfWeek = (tm_now.tm_wday+1)%7 ;		// tm一星期的日数,从星期一算起,范围为0-6 sys从星期日算起
	lpSystemTime->wHour =tm_now.tm_hour;					//小时
	lpSystemTime->wMinute = tm_now.tm_min;				//分钟
	lpSystemTime->wSecond = tm_now.tm_sec;				//秒
	lpSystemTime->wMilliseconds = tv.tv_usec/1000;			//毫秒
}
//将system转换成time_t 
void SystemTime2Time_T(LPSYSTEMTIME pSysTime,time_t* pTime)
{
	tm tm_tmp = {pSysTime->wSecond, 
pSysTime->wMinute, 
pSysTime->wHour,
 pSysTime->wDay,
 pSysTime->wMonth-1,
 pSysTime->wYear-1900,
 pSysTime->wDayOfWeek, 0, 0};
	*pTime = mktime(&tm_tmp);
}

  

测试代码:

 1 int main (int argc, char* argv[]) {
2
3 SYSTEMTIME systime;
4 time_t t_tmp=time(NULL);
5 printf("org %I64d \r\n",t_tmp);
6 GetLocalTime(&systime);
7
8 printf("after get time %04d-%02d-%02d %02d:%02d:%02d.%d\r\n",systime.wYear,systime.wMonth,
9 systime.wDay,systime.wHour,
10 systime.wMinute,systime.wSecond,systime.wMilliseconds);
11 time_t t_now;
12 SystemTime2Time_T(&systime,&t_now);
13 printf("convent %I64d\r\n",t_now);
14 getchar();
15 }
/* 运行结果
org                                                       1318434216 
after get time 2011-10-12 23:43:36.532
convent                                                       1318434216
*/

  



posted @ 2011-10-13 00:11  Scorpio_逸尘  阅读(1261)  评论(0编辑  收藏  举报