Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use mktime and difftime in C language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use mktime and difftime in C language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use mktime and difftime in C language.

The two function prototypes are as follows:

_ _ CRT_INLINE time_t _ _ cdecl mktime (struct tm * _ Tm); _ _ CRT_INLINE double _ _ cdecl difftime (time_t _ Time1,time_t _ Time2); mktime function

The mktime function converts the argument to the structure pointed to by timeptr to the number of seconds of duration since January 1, 1970, and returns-1 if an error occurs.

The prototype of the parameter structure is as follows:

Struct tm {int tm_sec; / * seconds, ranging from 0 to 59 * / int tm_min; / * minutes, from 0 to 59 * / int tm_hour; / * hours, from 0 to 23 * / int tm_mday / * days of the month, from 1 to 31 * / int tm_mon; / * months, from 0 to 11 * / int tm_year; / * years from 1900 * / int tm_wday; / * days of the week, from 0 to 6 * / int tm_yday / * the day of the year, ranging from 0 to 365 * / int tm_isdst; / * daylight saving time * /}

The following is demonstrated directly through a piece of code.

# include # include # include int main (int argc, char** argv) {int ret,time_cnt; struct tm info; info.tm_year = 2022-1900; info.tm_mon = 1-1; info.tm_mday = 25; info.tm_hour = 11; info.tm_min = 28; info.tm_sec = 50; info.tm_isdst =-1; ret = mktime (& info); time_cnt = time (NULL) If (ret = =-1) {printf ("Error: unable to make time using mktime\ n");} else {printf ("% d% d", ret,time_cnt);} return 0;}

First, define the time structure, then assign values to the variables in the structure, assign the current time value to the variables, then obtain the number of seconds of the current time through the time function, and finally print out the seconds converted by the mktime function and the seconds returned by the time function.

Through the results, we can see that the difference in seconds between the two functions is 1, which is due to the one-second delay in the compilation and execution of the program, indicating that the number of seconds after the conversion of the mktime function is the same as that returned by the time function.

Difftime function

The difftime function has two time parameters, and the main function of this function returns the difference between the two time parameters, that is, the number of seconds between the two time values.

You can usually use this function to calculate the running time of a piece of code.

# include # include # include int main (int argc, char** argv) {time_t start_t, end_t; double diff_t; printf ("Program starts.\ n"); time (& start_t); sleep (2); printf ("end of run! \ n "); time (& end_t); diff_t = difftime (end_t, start_t); printf ("\ nstart time:% d end time:% d code run time:% fs\ n ", start_t,end_t,diff_t); return 0;}

Two variables are defined to record the time value before and after the program is run, then the delay function is used to simulate the running process of the program, and finally the difftime function is used to calculate the running time of the function.

Through the printed results, it can be seen that the execution time of the delay function is 2s, and the delay in the program is also 2s, indicating that the result of the function calculation is correct. Note here that the return value of the difftime function is data of type double.

At this point, I believe you have a deeper understanding of "how to use mktime and difftime in C language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report