Summary of basic usage of time in C language

  • 2020-05-12 02:58:52
  • OfStack

preface

In these programming languages that I have learned, I can't always remember their time processing methods, so I have to re-read them every time I use them, so I want to record them here, which is convenient for me to find them when I use them, and also convenient for my friends who need to refer to them.

time_t and struct tm

In C time_t A type represents a time, usually a sum long An integer of 1 length, that is, in 32-bit, it is 4 bytes, and in 64-bit, it is 8 bytes. It holds an integer value representing the number of seconds from 1970-01-01 08:00:00 to the time it represents, which is a non-negative integer, so time_t cannot represent the time earlier than 1970-01-01 08:00:00.

One of the most common functions is time_t time(time_t *_v) Its parameter is 1 time_t Returns 1 pointer of type equal to the passed parameter time_t Type value. if time() The function takes 0 as an argument and returns the current time.

Now we can use C language to represent a time, but this time is only the number of seconds from a certain time, how to represent a more detailed time detail? And that's where it comes in struct tm Type, which can represent more specific time information.

It has 1 structure, so let's look at 1 for its member information, 1 struct tm Types include the following members:

int tm_year It's a year of time, and it starts in 1900, so when it's 1, it's 1901. because time_t The type represents a time range no earlier than 1970, so this value is usually no less than 70. long0 It's a number of months, and it goes from 0 to 11, and 0 is January, and 11 is December. int tm_mday The time is the date of the current month, and the range of its value is different according to the month. int tm_wday The time is the day of the week, and its values range from 0 to 6. 0 is Sunday, 1 is Monday, and 6 is Saturday. int tm_yday The date is the day of the current year. It is important to note that January 1 is day 0. int tm_hour What time is it? int tm_min What time is it? int tm_sec Time is a few seconds. int tm_isdst Is summer time.

localtime()

How do you take 1 time_t Type construction into struct tm Type? with struct tm *localtime(const time_t *_v) Function, notice that both the parameter and the return value are pointer types.


#include <stdio.h>
#include <time.h>

int main()
{
 time_t tt = time(0); // Get the current time 
 struct tm *pst = localtime(&tt); // the time_t Type conversion to struct tm type 
 printf("The year is %d.\n", pst->tm_year + 1900); // Don't forget to add 1900

 return 0;
}

Output of the above program:


The year is 2016.

mktime()

So how do you put struct tm Type conversion to time_t Type? Just use time_t mktime(struct tm *_v) Function, note that the parameter is of pointer type.

So how do you output time? It's easy to use char *ctime(time_t *_v) Functions and char *asctime(struct tm *_v) Function, note that the string returned ends with the newline \n.


#include <stdio.h>
#include <time.h>

int main()
{
 struct tm st;
 st.tm_year = 2016 - 1900;
 st.tm_mon = 8;
 st.tm_mday = 13;
 st.tm_hour = 16;
 st.tm_min = 30;
 st.tm_sec = 0;
 st.tm_isdst = 0;
 time_t tt = mktime(&st);
 printf("%s", asctime(&st));
 printf("%s", ctime(&tt));

 return 0;
}

Output of the above program:


Tue Sep 13 16:30:00 2016
Tue Sep 13 16:30:00 2016

We use it ourselves struct tm A time is constructed and executed mktime() After the function, tm_wday Properties are also automatically computed.

clock()

In time.h, there are some other functions that are commonly used, such as clock_t clock() The function, time_t0 It's also an integer time_t1 . This function returns the time the program spent running to this statement. Units of 1 are usually milliseconds, which you can pass through time_t2 So, if I output 1000, it turns out to be milliseconds.

We can easily use it to calculate the time consumed by a certain segment of the program:


#include <stdio.h>
#include <time.h>

int main()
{
 int i = 0;

 printf("CLOCKS_PER_SEC: %ld\n", CLOCKS_PER_SEC);

 long c = clock();
 while(i < 1<<30) i++;
 printf("The while loop cost %ld.\n", clock() - c);

 c = clock();
 for(i = 0; i < 1<<30; i++);
 printf("The for loop cost %ld.\n", clock() - c);

 return 0;
}

Output of the above program:


CLOCKS_PER_SEC: 1000
The while loop cost 2234.
The for loop cost 2206.

conclusion

The above is the whole content of this article, I hope the content of this article can help you to learn or use C language, if you have any questions, you can leave a message to communicate.


Related articles: