Parsing and generating timestamps for STM32

Publisher:太和清音Latest update time:2018-08-16 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

What is a timestamp?

Timestamp refers to the total number of seconds from 00:00:00 Greenwich time on January 1, 1970 (08:00:00 Beijing time on January 1, 1970) to the present. In layman's terms, a timestamp is a complete and verifiable piece of data that can indicate that a piece of data existed at a specific point in time. It is mainly proposed to provide users with an electronic evidence to prove the time when some of the user's data was generated. In practical applications, it can be used in various aspects including e-commerce and financial activities, especially to support the "non-repudiation" service of public key infrastructure.


How to use timestamp in STM32

STM32 uses timestamps mainly in two aspects:

1 Get the timestamp and parse it into local time

2 Generate timestamp based on local time

Note: When testing, you can use the online tool https://tool.lu/timestamp/ to convert timestamps and local time.


The timestamp parsing and generation here mainly use the mktime function and localtime function provided by time.h in C. The former is used to parse the timestamp and generate data in tm format, and the latter generates the timestamp according to the data in tm format (refer to the comments in the time.h file for details).

Let's look at the definition of the tm data structure:

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;

        };

    };

};

Here we should pay attention to the following:

1 The year defined in tm starts from 1900, and the year value set in STM32 RTC ranges from 0 to 99, so when assigning the value, add 100 to the RTC year. That is: stm.tm_year = RTC_DateStruct.RTC_Year + 100;

2 Similarly, the months value range defined in tm is 0-11, and the month value range set in STM32 RTC is 1-12, so when assigning values, subtract 1 from the RTC month. That is: stm.tm_mon = RTC_DateStruct.RTC_Month-1;

3 When we generate the timestamp, we convert the local time to UTC time. The relationship is: UTC + time zone difference = local time, so the UTC time we set is: UTC = local time (Beijing time)) - 8

After paying attention to the above details, the following implements the function of parsing the timestamp into the local time and the function of generating the timestamp according to the local time.


Generate timestamp function from local time

char* time_to_timestamp(void)

{

unsigned int timestamp;

struct tm stm;

char *timestamp_buf;

char *buf;

timestamp_buf = (char *)mem_malloc(10);

buf = (char *)mem_malloc(100);

RTC_DateTypeDef RTC_DateStruct;

RTC_TimeTypeDef RTC_TimeStruct;

RTC_GetDate(RTC_Format_BIN,&RTC_DateStruct);

RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);

stm.tm_year = RTC_DateStruct.RTC_Year + 100; //RTC_Year rang 0-99,but tm_year since 1900

stm.tm_mon = RTC_DateStruct.RTC_Month-1; //RTC_Month rang 1-12,but tm_mon rang 0-11

stm.tm_mday = RTC_DateStruct.RTC_Date; //RTC_Date rang 1-31 and tm_mday rang 1-31

stm.tm_hour = RTC_TimeStruct.RTC_Hours-8; //RTC_Hours rang 0-23 and tm_hour rang 0-23

stm.tm_min = RTC_TimeStruct.RTC_Minutes;   //RTC_Minutes rang 0-59 and tm_min rang 0-59

stm.tm_sec = RTC_TimeStruct.RTC_Seconds;

sprintf(buf,"\r\nrtc %d-%d-%d  %d.%d.%d\r\n",\

RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_Month,RTC_DateStruct.RTC_Date,\

RTC_TimeStruct.RTC_Hours,RTC_TimeStruct.RTC_Minutes,RTC_TimeStruct.RTC_Seconds);

USART_COM3_Send_data(buf,strlen(buf));

mem_free(buf);

sprintf(timestamp_buf,"%u",mktime(&stm));

USART_COM3_Send_data(timestamp_buf,strlen(timestamp_buf));

return timestamp_buf;

}

Timestamp parsing into local time function

void timestamp_to_time(unsigned int timestamp)

{

struct tm *stm= NULL;

char *buf;

buf = (char *)mem_malloc(100);

RTC_DateTypeDef RTC_DateStruct;

RTC_TimeTypeDef RTC_TimeStruct;

stm = localtime(×tamp);

RTC_DateStruct.RTC_Year = stm->tm_year - 100;

RTC_DateStruct.RTC_Month = stm->tm_mon + 1;

RTC_DateStruct.RTC_Date = stm->tm_mday;

RTC_TimeStruct.RTC_Hours = stm->tm_hour + 8;

RTC_TimeStruct.RTC_Minutes = stm->tm_min;

RTC_TimeStruct.RTC_Seconds = stm->tm_sec;

sprintf(buf,"\r\nstm %d-%d-%d  %d.%d.%d\r\n",\

stm->tm_year,stm->tm_mon,stm->tm_mday,\

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

USART_COM3_Send_data(buf,strlen(buf));

mem_free(buf);

RTC_SetDate(RTC_Format_BIN,&RTC_DateStruct);

RTC_SetTime(RTC_Format_BIN,&RTC_TimeStruct);

}


Keywords:STM32 Reference address:Parsing and generating timestamps for STM32

Previous article:Conversion between MCU system time and Unix time
Next article:Summary of common errors in Keil5

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号