生成时间戳

https://blog.csdn.net/oshan2012/article/details/79892659

RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
void time_to_timestamp(void)
{
unsigned int timestamp;
struct tm stm;
char timestamp_buf[10];
char buf[100];

RTC_DateTypeDef RTC_DateStruct;
RTC_TimeTypeDef	RTC_TimeStruct;
HAL_RTC_GetTime(&RTC_Handler,&RTC_TimeStruct,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&RTC_Handler,&RTC_DateStruct,RTC_FORMAT_BIN);

stm.tm_year     = RTC_DateStruct.Year + 100;	//RTC_Year rang 0-99,but tm_year since 1900
stm.tm_mon	= RTC_DateStruct.Month-1;	//RTC_Month rang 1-12,but tm_mon rang 0-11
stm.tm_mday	= RTC_DateStruct.Date;	        //RTC_Date rang 1-31 and tm_mday rang 1-31
stm.tm_hour	= RTC_TimeStruct.Hours-8;       //RTC_Hours rang 0-23 and tm_hour rang 0-23
stm.tm_min	= RTC_TimeStruct.Minutes;       //RTC_Minutes rang 0-59 and tm_min rang 0-59
stm.tm_sec	= RTC_TimeStruct.Seconds;		

sprintf(buf,"\r\nrtc %d-%d-%d  %d.%d.%d\r\n",\
RTC_DateStruct.Year,RTC_DateStruct.Month,RTC_DateStruct.Date,\
RTC_TimeStruct.Hours,RTC_TimeStruct.Minutes,RTC_TimeStruct.Seconds);

sprintf(timestamp_buf,"%u",mktime(&stm));
printf("time_to_timestamp: %s", timestamp_buf);

}

posted @ 2022-02-25 18:36  源世  阅读(88)  评论(0编辑  收藏  举报