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);
}
Previous article:Conversion between MCU system time and Unix time
Next article:Summary of common errors in Keil5
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- CLUE development board information
- How to solve EMI compliance testing problems
- How to amplify a voltage signal of about 20mv from 0.3MHZ to 1MHZ
- GD32E231 DIY Competition (3) GD32E231C reads ADC sampling value
- Internal functions of MSP430
- When filling out college entrance examination applications, which majors must be chosen carefully?
- Differences between CPU and CLA and Error Handling Techniques
- EEWORLD University Hall ---- Pattern Recognition National University of Defense Technology Cai Xuanping
- ST32F103x Read the sensor value on X-NUCLEO-IKS01A3 via I2C
- Need help designing a CAN communication solution between two MCUs!