How to use the Gettimeofday () function
1. Function prototypes
#include <sys/time.h>
int gettimeofday (struct timeval *tv, struct timezone *tz);
2. Description
Gettimeofday () returns the current time with the TV structure, and the local time zone information is placed in the structure that TZ refers to.
3. Structural body
struct timeval{
Long tv_sec;/* sec */
Long tv_usec;/* Subtle */
};
struct timezone{
int tz_minuteswest; /* and Greenwich TIME DIFFERENCE how many minutes */
int tz_dsttime; Correction of the/*DST * *
}
#include <stdio.h>#include<time.h>#include<sys/time.h>#include<string.h>#defineSize_of_datetime 20voidSysusectime (Char*ptime) { structTimeval TV; structtimezone TZ; intI=0; structTM *p; Charsys_time[size_of_datetime+1]=""; Gettimeofday (&TV, &TZ); P= LocalTime (&tv.tv_sec); sprintf (Sys_time,"%d%d%d%d%d%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, 2, 2, 2, 2, seconds 2 bit, 6 bit, 20 bits.*/ /*0 for the end of insufficient length*/ for(I=strlen (sys_time); i<size_of_datetime;i++) {Sys_time[i]='0'; } Sys_time[size_of_datetime]=' /'; strcpy (ptime,sys_time);}intMainvoid){ Charstrusectime[size_of_datetime+1]; Sysusectime (Strusectime); printf ("%s\n", Strusectime); return 0;}
C language acquisition of Linux system precise time