C language Settimeofday () function: Setting the current timestamp
header file:
#include <sys/time.h> #include <unistd.h>
To define a function:
int settimeofday (const struct Timeval *tv, const struct timezone);
Function Description: Settimeofday () will set the current time by the TV refers to the structure of information, local time zone information is set to TZ the structure referred to. For detailed instructions please refer to gettimeofday ().
Note that under Linux, only root permissions can be used to modify the time.
Return value: Success returns 0, failure returns-1, error code is stored in errno.
Error code:
Eperm is not called Settimeofday () by root permission, and permissions are not sufficient.
Einval time zone or some data is incorrect and cannot be set correctly.
C language Gettimeofday () function: getting the current time
header file:
#include <sys/time.h> #include <unistd.h>
Definition function: int gettimeofday (struct timeval * TV, struct timezone * tz);
Function Description: Gettimeofday () will have the current time is the structure of the TV refers to return, the local time zone information is placed in the TZ of the structure referred to.
The TIMEVAL structure is defined as:
struct timeval{
long tv_sec;//sec
long tv_usec;/microseconds
};
The timezone structure is defined as:
struct timezone
{
int tz_minuteswest;//and Greenwich TIME difference how many minutes
int tz_dsttime;//Daylight saver State
};
Both of these structures are defined in/usr/include/sys/time.h. The state represented by Tz_dsttime is as follows
Dst_none//Do not use DST_USA//
United States
Dst_aust//Australia Dst_wet//
Western Europe Dst_met
//Central
Dst_eet//Eastern Europe
Dst_ CAN//Canada
DST_GB//dst_rum//
Romanian
Dst_tur//Turkey
Dst_austalt//Australia (after 1986)
Return value: Success returns 0, failure returns-1, error code is stored in errno.
Additional instructions: the memory space referred to by efault Pointer TV and TZ exceeds access permissions.
Example
#include <sys/time.h>
#include <unistd.h>
main () {
struct timeval TV;
struct timezone tz;
Gettimeofday (&TV, &tz);
printf ("TV_SEC; %d\n ", tv.tv_sec);
printf ("TV_USEC; %d\n ", tv.tv_usec);
printf ("Tz_minuteswest; %d\n ", tz.tz_minuteswest);
printf ("Tz_dsttime,%d\n", tz.tz_dsttime);
}
Execution results:
tv_sec:974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0