Timeval
The timeval structure can be used to save time information.
In the <sys/time. h> file, the structure is as follows:
struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */};
TV _usec is in microseconds (10-6 seconds ).
Its main usage is as follows:
Struct timeval E; gettimeofday (& E, null); // obtain the current time
Settimeofday (& E, null); // set the current time
Time_t
Time_t stores the time information, which is actually a long integer. The time is represented by "the number of seconds elapsed from a standard time point to this time point.
Common Methods
1. Basic usage
#include <time.h>#include <stdio.h>#include <dos.h>int main(void){ time_t t; t = time(NULL); printf("The number of seconds since January 1, 1970 is %ld",t); return 0;}
2. The time function is also used to generate random numbers, and the calendar time is used as the seed.
#include <stdio.h> #include <time.h> #include<stdlib.h> int main(void) { int i; srand((unsigned) time(NULL)); printf("ten random numbers from 0 to 99\n\n"); for(i=0;i<10;i++) { printf("%d\n",rand()%100); } return 0; }
3. Use the time () function and other functions (such as localtime, gmtime, asctime, and ctime) to obtain the current system time or standard time.
# Include <stdio. h> # include <stddef. h> # include <time. h> int main (void) {time_t timer; // time_t is the long int type struct TM * tblock; timer = Time (null ); // This sentence can also be changed to time (& timer); tblock = localtime (& timer); printf ("local time is: % s \ n ", asctime (tblock); Return 0 ;}Struct TM
Struct TM {int tm_sec; // current number of seconds; normal range: 0-59, but allowed to 61 seconds int tm_min; // current score, range: 0-59 int tm_hour; // The number of hours counted from midnight, ranging from 0 to 23 int tm_mday; // the number of days of the current month, ranging from 01 to 31 int tm_mon; // the current month, counted from January 1, January, the value range is 0-11 int tm_year; // The number of years since January 1, 1900 int tm_wday; // the number of days of a week, counted from Monday, and the value range is 1-7 int tm_yday; // The number of days from January 1, January 1 this year to the present. The value range is 0-365 int tm_isdst; // The identifier of the time when dimensions are implemented, and the tm_isdst value is positive. // Wait time is not implemented, and tm_isdst is 0; // when the problem is unknown, tm_isdst is negative .};
Function
Gmtime
Truct TM * gmtime (const time_t * timep );
Function Description
Gmtime () converts the information in the time_t structure referred to by the timep parameter to the time and date representation method used in the real world, and then returns the result from the structure TM.
Example:
# Include <stdlib. h> # include <stdio. h> # include <time. h> int main (INT argc, char * argv []) {time_t cur_time; struct TM * tm_time; cur_time = Time (null ); printf ("the number of seconds since 1970.1.1 is % LD \ n", cur_time); // UTC time tm_time = gmtime (& cur_time ); printf ("% d-% d \ n", (1900 + tm_time-> tm_year), (1 + tm_time-> tm_mon), tm_time-> tm_mday ); printf ("% d: % d \ n", tm_time-> tm_wday, tm_time-> tm_hour, tm_time-> tm_min, tm_time-> tm_sec ); return 0 ;}
Localtime
struct tm *localtime(const time_t * timep);
Function Description: obtains the current local time and date.
Example:
#include <stdlib.h>#include <stdio.h>#include <time.h>int main(int argc, char* argv[]){ time_t cur_time; struct tm* tm_time; cur_time = time(NULL); printf("the number of seconds since 1970.1.1 is %ld\n", cur_time); tm_time = localtime(&cur_time); printf("%d-%d-%d\n",(1900+tm_time->tm_year), (1+tm_time->tm_mon),tm_time->tm_mday); printf("%d %d:%d:%d\n", tm_time->tm_wday, tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec); return 0;}