ESP32需要给下位机通过UART发送时间戳,形式是年月日时分秒的十六进制数据包。
#include <stdio.h> #include <time.h> int main()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
int year,month,day,hour,min,sec;
year = +timeinfo->tm_year;
month = +timeinfo->tm_mon;
day = timeinfo->tm_mday;
hour = timeinfo->tm_hour;
min = timeinfo->tm_min;
sec = timeinfo->tm_sec;
printf ( "当前时间:%4d-%02d-%02d %02d:%02d:%02d\n\n",year, month,day,hour,min,sec);
printf ( "你需要的格式:%4d%02d%02d%02d%02d%02d\n\n",year, month,day,hour,min,sec); char hyy[],lyy[],MM[],dd[],hh[],mm[],ss[];
sprintf(hyy,"%02X",year/);
sprintf(lyy,"%02X",year%);
sprintf(MM,"%02X",month);
sprintf(dd,"%02X",day);
sprintf(hh,"%02X",hour);
sprintf(mm,"%02X",min);
sprintf(ss,"%02X",sec); printf("转化为16进制:%02s%02s%02s%02s%02s%02s%02s\n",hyy,lyy,MM,dd,hh,mm,ss);
//exit(0);
return ;
}