C language: get the exact time of the Linux system; C language: Get the linux System
How to Use the gettimeofday () function
1. function prototype
# Include <sys/time. h>
Int gettimeofday (struct timeval * TV, struct timezone * tz );
2. Description
Gettimeofday () will return the current time using the TV struct, and put the information of the local time zone in the structure referred to by tz
3. struct
Struct timeval {
Long TV _sec;/* seconds */
Long TV _usec;/* subtle */
};
Struct timezone {
Int tz_minuteswest;/* the time difference between greenwich and */
Int tz_dsttime;/* DST correction */
}
# Include <stdio. h> # include <time. h> # include <sys/time. h> # include <string. h> # define SIZE_OF_DATETIME 20 void sysUsecTime (char * pTime) {struct timeval TV; struct timezone tz; int I = 0; struct tm * p; char sys_time [SIZE_OF_DATETIME + 1] = ""; gettimeofday (& TV, & tz); p = localtime (& TV. TV _sec); sprintf (sys_time, "% d % ld", 1900 + p-> tm_year, 1 + p-> tm_mon, p-> tm_mday, p-> tm_hour, p-> tm_min, p-> tm_sec, TV. TV _usec); printf ("strlen (sys_time) = [% d] \ n", strlen (sys_time); printf ("sys_time = [% s] \ n ", sys_time);/* the maximum length of time is: 4-digit, 2-digit, 2-digit, 2-digit, 2-digit, 2-digit, 2-digit, 2-digit, 2-digit, 2-digit, 6-digit, second, 6-digit = 20-digit * // * Add 0 to the end of a given length. */for (I = strlen (sys_time ); I <SIZE_OF_DATETIME; I ++) {sys_time [I] = '0';} sys_time [SIZE_OF_DATETIME] = '\ 0'; strcpy (pTime, sys_time );} int main (void) {char strusecTime [SIZE_OF_DATETIME + 1]; sysUsecTime (strusecTime); printf ("% s \ n", strusecTime); return 0 ;}