在线时间350 小时
UID219090
ST金币3105
蝴蝶豆45
注册时间2008-4-2
该用户从未签到
论坛元老
- 最后登录
- 2021-3-21
|
最近因工作需要把单片机的YYMMDDHHMMSS转为Unix的时间戳,网上大多都是上位机的程序,经过搜索引擎的帮助下,终于实现了把单片机的时间转为Unix的时间戳,特贴出代码给社区内需要的朋友,转载请注明出处!!!
由于使用了 struct tm 结构体好是mktime,所以需要包含 <time.h>头文件,把下面的年月日时分秒换成实际的年月日时分秒即可!
废话不多说,核心代码:
- int32_t GetTick(void)
- {
- struct tm stm;
- int iY, iM, iD, iH, iMin, iS;
-
- RTC_DateTypeDef sdatestructureget;
- RTC_TimeTypeDef stimestructureget;
- /* Get the RTC current Time */
- HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
- /* Get the RTC current Date */
- HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
-
- memset(&stm,0,sizeof(stm));
-
-
- iY = sdatestructureget.Year;
- iM = sdatestructureget.Month;
- iD = sdatestructureget.Date;
- iH = stimestructureget.Hours-8;//北京时间-8=UTC
- iMin = stimestructureget.Minutes;
- iS = stimestructureget.Seconds;
-
- stm.tm_year=iY+100;
- stm.tm_mon=iM-1;
- stm.tm_mday=iD;
- stm.tm_hour=iH;
- stm.tm_min=iMin;
- stm.tm_sec=iS;
- return mktime(&stm);
- }
复制代码 |
评分
-
查看全部评分
|