分享

STM32 之 时间戳的解析与生成

 筱肆 2018-09-03

STM32 之 时间戳的解析与生成

2018年06月27日 17:53:01 阅读数:553 标签: STM32 时间戳
什么是时间戳
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。通俗的讲, 时间戳是一份能够表示一份数据在一个特定时间点已经存在的完整的可验证的数据。 它的提出主要是为用户提供一份电子证据, 以证明用户的某些数据的产生时间。 在实际应用上, 它可以使用在包括电子商务、 金融活动的各个方面, 尤其可以用来支撑公开密钥基础设施的 “不可否认” 服务。

STM32 怎么使用时间戳
STM32 使用时间戳主要涉及两方面:
1 获取时间戳后解析成本地时间
2 根据本地时间生成时间戳
注:测试时,可以用在线工具 https:///timestamp/ 转换时间戳和本地时间

这里的时间戳解析和生成主要用到 C 中 time.h 提供的 mktime 函数和 localtime 函数,前者用于解析时间戳,生成 tm 格式的数据,后者根据 tm 格式的数据生成时间戳(详细内容参考 time.h 文件中的注释)。
下面看下 tm 数据结构的定义:
  1. struct tm {
  2. int tm_sec; /* seconds after the minute, 0 to 60
  3. (0 - 60 allows for the occasional leap second) */
  4. int tm_min; /* minutes after the hour, 0 to 59 */
  5. int tm_hour; /* hours since midnight, 0 to 23 */
  6. int tm_mday; /* day of the month, 1 to 31 */
  7. int tm_mon; /* months since January, 0 to 11 */
  8. int tm_year; /* years since 1900 */
  9. int tm_wday; /* days since Sunday, 0 to 6 */
  10. int tm_yday; /* days since January 1, 0 to 365 */
  11. int tm_isdst; /* Daylight Savings Time flag */
  12. union { /* ABI-required extra fields, in a variety of types */
  13. struct {
  14. int __extra_1, __extra_2;
  15. };
  16. struct {
  17. long __extra_1_long, __extra_2_long;
  18. };
  19. struct {
  20. char *__extra_1_cptr, *__extra_2_cptr;
  21. };
  22. struct {
  23. void *__extra_1_vptr, *__extra_2_vptr;
  24. };
  25. };
  26. };
在这里我们要注意如下内容:
1 tm 中定义的 year 是从 1900 开始的,而 STM32 RTC中设置的 year 取值范围在 0-99,那么赋值时要在 RTC year上加 100。即:stm.tm_year = RTC_DateStruct.RTC_Year + 100;
2 同理,tm 中定义的 months 取值范围是 0-11,而 STM32 RTC中设置 month 取值范围在 1-12,那么在赋值时要在 RTC month 上 减 1。即:stm.tm_mon= RTC_DateStruct.RTC_Month-1;
3 我们在生成时间戳时是将 本地时间 转为 UTC 时间,其关系为:UTC + 时区差 = 本地时间,那么我们设置的UTC时间为:UTC = 本地时间(北京时间))- 8
注意了如上细节后,下面分别实现时间戳解析成本地时间函数和根据本地时间生成时间戳函数。

本地时间生成时间戳函数
  1. char* time_to_timestamp(void)
  2. {
  3. unsigned int timestamp;
  4. struct tm stm;
  5. char *timestamp_buf;
  6. char *buf;
  7. timestamp_buf = (char *)mem_malloc(10);
  8. buf = (char *)mem_malloc(100);
  9. RTC_DateTypeDef RTC_DateStruct;
  10. RTC_TimeTypeDef RTC_TimeStruct;
  11. RTC_GetDate(RTC_Format_BIN,&RTC_DateStruct);
  12. RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
  13. stm.tm_year = RTC_DateStruct.RTC_Year + 100; //RTC_Year rang 0-99,but tm_year since 1900
  14. stm.tm_mon = RTC_DateStruct.RTC_Month-1; //RTC_Month rang 1-12,but tm_mon rang 0-11
  15. stm.tm_mday = RTC_DateStruct.RTC_Date; //RTC_Date rang 1-31 and tm_mday rang 1-31
  16. stm.tm_hour = RTC_TimeStruct.RTC_Hours-8; //RTC_Hours rang 0-23 and tm_hour rang 0-23
  17. stm.tm_min = RTC_TimeStruct.RTC_Minutes; //RTC_Minutes rang 0-59 and tm_min rang 0-59
  18. stm.tm_sec = RTC_TimeStruct.RTC_Seconds;
  19. sprintf(buf,"\r\nrtc %d-%d-%d %d.%d.%d\r\n",\
  20. RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_Month,RTC_DateStruct.RTC_Date,\
  21. RTC_TimeStruct.RTC_Hours,RTC_TimeStruct.RTC_Minutes,RTC_TimeStruct.RTC_Seconds);
  22. USART_COM3_Send_data(buf,strlen(buf));
  23. mem_free(buf);
  24. sprintf(timestamp_buf,"%u",mktime(&stm));
  25. USART_COM3_Send_data(timestamp_buf,strlen(timestamp_buf));
  26. return timestamp_buf;
  27. }
时间戳解析成本地时间函数
  1. void timestamp_to_time(unsigned int timestamp)
  2. {
  3. struct tm *stm= NULL;
  4. char *buf;
  5. buf = (char *)mem_malloc(100);
  6. RTC_DateTypeDef RTC_DateStruct;
  7. RTC_TimeTypeDef RTC_TimeStruct;
  8. stm = localtime(×tamp);
  9. RTC_DateStruct.RTC_Year = stm->tm_year - 100;
  10. RTC_DateStruct.RTC_Month = stm->tm_mon + 1;
  11. RTC_DateStruct.RTC_Date = stm->tm_mday;
  12. RTC_TimeStruct.RTC_Hours = stm->tm_hour + 8;
  13. RTC_TimeStruct.RTC_Minutes = stm->tm_min;
  14. RTC_TimeStruct.RTC_Seconds = stm->tm_sec;
  15. sprintf(buf,"\r\nstm %d-%d-%d %d.%d.%d\r\n",\
  16. stm->tm_year,stm->tm_mon,stm->tm_mday,\
  17. stm->tm_hour,stm->tm_min,stm->tm_sec);
  18. USART_COM3_Send_data(buf,strlen(buf));
  19. mem_free(buf);
  20. RTC_SetDate(RTC_Format_BIN,&RTC_DateStruct);
  21. RTC_SetTime(RTC_Format_BIN,&RTC_TimeStruct);
  22. }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多