C language - get the system time is output as a date when minutes and seconds

ESP32 needs to be transmitted via the UART to stamp the next crew, in the form of minute and second is the date when the hexadecimal data packet.

#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 = 1900+timeinfo->tm_year;
  month = 1+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[2],lyy[2],MM[2],dd[2],hh[2],mm[2],ss[2];
  sprintf(hyy,"%02X",year/100);
  sprintf(lyy,"%02X",year%100);
  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 0;
} 

 

Guess you like

Origin www.cnblogs.com/tokure/p/11401393.html