首页 > C/C++ > C++ UTC时间字符串转换成本地时间字符串
2013
03-18

C++ UTC时间字符串转换成本地时间字符串

/************************************************************************/
/*   UTC时间转换成本地时间  
	Wed Feb 27 13:10:33 2013    -> 2013/2/27 13:10:33*/
/************************************************************************/
std::string ConvertUTCToLocal(std::string & s)
{
	if (s.empty()){
		return std::string("");
	}

	//char input[] = "Tue Feb 19 21:09:19.234 2013 (UTC + 8:00)";
	unsigned int nYear,nDay,nHour,nMin,nSec,nMilli;
	char sWeek[5];
	char chMonth[5];

	//定义月字符串
	static const char *abmon[12] = {
		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
	};

	sscanf(s.c_str(),"%s %s %d %d:%d:%d.%d %d",sWeek,chMonth,&nDay,&nHour,&nMin,&nSec,&nMilli,&nYear);

	size_t len = 0;
	size_t nMon = 0;
	for (int i = 0; i < 12; i++) {
		/* Abbreviated name. */
		len = strlen(abmon[i]);
		if (strnicmp(chMonth,(char*)(abmon[i]),len) == 0){
			nMon = ++i;
			break;
		}
	}

	char chbuf[80];
	sprintf(chbuf,"%d/%d/%d %d:%d:%d",nYear,nMon,nDay,nHour,nMin,nSec);
	return std::string(chbuf);
}
最后编辑:
作者:dnybz
这个作者貌似有点懒,什么都没有留下。