内容简介
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz)%uA0
int settimeofday(const struct timeval *tv%uA0, const struct timezone *tz)
描述
The functions%uA0gettimeofday() and%uA0settimeofday() can get and set the time as well as a timezone. The%uA0tv%uA0argument is a%uA0struct timeval%uA0(as specified in%uA0<sys/time.h>):
struct timeval { time_t tv_sec /* seconds */ suseconds_t tv_usec /* microseconds */ } |
and gives the number of seconds and microseconds since the Epoch (see%uA0time(2)). Thetz%uA0argument is a%uA0struct timezone:
struct timezone { int tz_minuteswest /* minutes west of Greenwich */ int tz_dsttime /* type of DST correction */ } |
If either%uA0tv%uA0or%uA0tz%uA0is NULL, the corresponding structure is not set or returned.
The use of the%uA0timezone%uA0structure is obsolete the%uA0tz%uA0argument should normally be specified as NULL. The%uA0tz_dsttime%uA0field has never been used under Linux it has not been and will not be supported by libc or glibc. Each and every occurrence of this field in the kernel source (other than the declaration) is a bug. Thus, the following is purely of historic interest.
The field%uA0tz_dsttime%uA0contains a symbolic constant (values are given below) that indicates in which part of the year Daylight Saving Time is in force. (Note: its value is constant throughout the year: it does not indicate that DST is in force, it just selects an algorithm.) The daylight saving time algorithms defined are as follows :
DST_NONE%uA0/* not on dst */%uA0
DST_USA%uA0/* USA style dst */%uA0
DST_AUST%uA0/* Australian style dst */%uA0
DST_WET%uA0/* Western European dst */%uA0
DST_MET%uA0/* Middle European dst */%uA0
DST_EET%uA0/* Eastern European dst */%uA0
DST_CAN%uA0/* Canada */%uA0
DST_GB%uA0/* Great Britain and Eire */%uA0
DST_RUM%uA0/* Rumania */%uA0
DST_TUR%uA0/* Turkey */%uA0
DST_AUSTALT%uA0/* Australian style with shift in 1986 */
Of course it turned out that the period in which Daylight Saving Time is in force cannot be given by a simple algorithm, one per country indeed, this period is determined by unpredictable political decisions. So this method of representing time zones has been abandoned. Under Linux, in a call to%uA0settimeofday() the%uA0tz_dsttime%uA0field should be zero.
Under Linux there is some peculiar ‘warp clock’ semantics associated to thesettimeofday() system call if on the very first call (after booting) that has a non-NULL%uA0tzargument, the%uA0tv%uA0argument is NULL and the%uA0tz_minuteswest%uA0field is non-zero. In such a case it is assumed that the CMOS clock is on local time, and that it has to be incremented by this amount to get UTC system time. No doubt it is a bad idea to use this feature.
下面的宏定义在一个struct timeval操作:
#define timerisset(tvp)\ %uA0 %uA0((tvp)->tv_sec || (tvp)->tv_usec) #define timercmp(tvp, uvp, cmp)\ ((tvp)->tv_sec cmp (uvp)->tv_sec ||\ (tvp)->tv_sec == (uvp)->tv_sec &&\ (tvp)->tv_usec cmp (uvp)->tv_usec) #define timerclear(tvp)\ %uA0 %uA0((tvp)->tv_sec = (tvp)->tv_usec = 0) |
返回值
gettimeofday() and%uA0settimeofday() return 0 for success, or -1 for failure (in which caseerrno%uA0is set appropriately).错误
标签 | 描述 |
---|---|
EFAULT | One of%uA0tv%uA0or%uA0tz%uA0pointed outside the accessible address space. |
EINVAL | Timezone (or something else) is invalid. |
EPERM | The calling process has insufficient privilege to callsettimeofday() under Linux the%uA0CAP_SYS_TIMEꃊpability is required. |
注意
The prototype for%uA0settimeofday() and the defines for%uA0timercmp,%uA0timerisset,%uA0timerclear,timeradd,%uA0timersub%uA0are (since glibc2.2.2) only available if%uA0_BSD_SOURCE%uA0is defined.Traditionally, the fields of%uA0struct timeval%uA0were longs.
遵循于
SVr4, 4.3BSD. POSIX.1-2001 describes%uA0gettimeofday() but not%uA0settimeofday().另请参阅
-
%uA0