毫秒时间戳与时间转换C源码

时间戳生成C源码

  • 时间转换毫秒时间戳
  • 毫秒时间戳转换时间

时间转换毫秒时间戳

uint32_t TIMESTRING_C(void)
{
	char str_time[25] = "2011-12-31 11:43:07";
	

	struct tm stm;
	int iY, iM, iD, iH, iMin, iS;
	uint8_t  nn,yy,rr,ss,ff,mm;           
	
	/*引入年月日时分秒数据
	nn = Tim_ny/100;
	yy = Tim_ny%100;
	
	rr = Tim_rs/100;
	ss = Tim_rs%100;
	
	ff = Tim_fs/100;
	mm = Tim_fs%100;
	*/
	
	sprintf(str_time,"20%02d-%02d-%02d %02d:%02d:%02d",nn, yy, rr, ss, ff, mm);

	memset(&stm,0,sizeof(stm));

	iY = atoi(str_time);
	iM = atoi(str_time+5);
	iD = atoi(str_time+8);
	iH = atoi(str_time+11);
	iMin = atoi(str_time+14);
	iS = atoi(str_time+17);
	
	stm.tm_year=iY-1900;
	stm.tm_mon=iM-1;
	stm.tm_mday=iD;
	stm.tm_hour=iH;
	stm.tm_min=iMin;
	stm.tm_sec=iS;

	/*printf("%d-%0d-%0d %0d:%0d:%0d\n", iY, iM, iD, iH, iMin, iS);*/

	return ((mktime(&stm))-28800);
}

毫秒时间戳转换时间

void TIMSTRING_S(uint32_t data)
{
	time_t t;
    struct tm *p;
	char s[100];
    t=data / 1000 + 28800;
    uint8_t  nn,yy,rr,ss,ff,mm; 
	
	
	p = localtime( &t );

    strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", p);

 /*对应年月日时分秒数据
	nn = p->tm_year)%100;
	yy = p->tm_mon+1;
	
	rr = p->tm_mday;
	ss = p->tm_hour;
	
	ff = p->tm_min;
	mm = p->tm_sec;
*/
}

相关文章:
[1]: https://blog.csdn.net/u013073067/article/details/102977039

你可能感兴趣的:(STM32,c语言,开发语言,单片机)