STM32中time.h文件详解

  • Post author:
  • Post category:其他


STM32的time.h文件有时间结构体的定义和时间转化的相关函数。

struct tm {
    int tm_sec;   /* seconds after the minute, 0 to 60
                     (0 - 60 allows for the occasional leap second) */
    int tm_min;   /* minutes after the hour, 0 to 59 */
    int tm_hour;  /* hours since midnight, 0 to 23 */
    int tm_mday;  /* day of the month, 1 to 31 */
    int tm_mon;   /* months since January, 0 to 11 */
    int tm_year;  /* years since 1900 */
    int tm_wday;  /* days since Sunday, 0 to 6 */
    int tm_yday;  /* days since January 1, 0 to 365 */
    int tm_isdst; /* Daylight Savings Time flag */
    union {       /* ABI-required extra fields, in a variety of types */
        struct {
            int __extra_1, __extra_2;
        };
        struct {
            long __extra_1_long, __extra_2_long;
        };
        struct {
            char *__extra_1_cptr, *__extra_2_cptr;
        };
        struct {
            void *__extra_1_vptr, *__extra_2_vptr;
        };
    };
};

上一篇RTC的代码实现文章中有一句Net_time = localtime(&local_timestamp)便是调用了time.h文件中的localtime函数,将秒计数值转换为当前的时间。

这里写图片描述

各函数的调用效果可参考链接:

time.h


若想打印出严格的时间格式输出,可采用%02d,如1小时3分5秒可以打印出01:03:05,前面自动补零。实现语句为

printf(“%d-%02d-%02d %02d:%02d:%02d\r\n”,\

Net_time->tm_year+1900, Net_time->tm_mon+1, Net_time->tm_mday,\

Net_time->tm_hour,Net_time->tm_min,Net_time->tm_sec);

打印效果如:2018-01-25 16:13:04

NTP实例的源码:

https://download.csdn.net/download/u014470361/10234803



版权声明:本文为u014470361原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。