精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。需要全文内容也请联系孙老师。
The system supplies the current time via the system calls time or gettimeofday—only one of these is a system call, but the system determines which one it is. 系统通过系统调用time 或gettimeofday提供当前时间获取功能,只有一个是系统调用,但系统决定了哪一个是。
#include <sys/types.h>
#include <time.h>
time_t time (time_t *tloc);
time returns the current time in time_t form, both as a return value and at tloc if this is not NULL. time is implemented as a system call in System V and as a library function (which calls gettimeofday) in BSD. Since it returns a scalar value, a call to time can be used as a parameter to functions like localtime or ctime.
time函数以time_t形式返回当前时间,如果这不是NULL,既是一个返回值也放在了tloc参数里,。在System V里,time函数以系统调用实现,在BSD里,以库函数实现(它调用了gettimeofday)。因为它返回一个标量值,time函数的调用可被用作一个参数,比如在使用localtime或ctime。
ftime is a variant of time that returns time information with a resolution of one millisecond. It originally came from 4.2BSD, but is now considered obsolete. ftime是时间变量,它以一毫秒的分辨率返回时间信息。它最初来自4.2BSD,但现在已经过时了。
#include <sys/types.h>
#include <sys/timeb.h>
typedef long time_t; /* (typically) */
struct timeb
{
time_t time; /* the same time returned by time */
unsigned short millitm; /* Milliseconds */
short timezone; /* System default time zone */
short dstflag; /* set during daylight savings time */
};
struct timeb *ftime (struct timeb *tp);
The timezone returned is the system default, possibly not what you want. System V.4 deprecates* the use of this variable as a result. Depending on which parameters are actually used, there are a number of alternatives to ftime. In many cases, time supplies all you need. However, time is accurate only to one second.
返回的时区是系统默认的,可能不是你想要的。System V.4不赞成*使用这个变量。根据实际使用的参数,有很多替代ftime的功能。在许多情况下,time函数提供给你所需要的。然而,time函数只准确到一秒。
On some systems, you may be able to define ftime in terms of gettimeofday, which returns the time of the day with a 1 microsecond resolution—see the next section. On other systems, unfortunately, the system clock does not have a finer resolution than one second, and you are stuck with time.
在一些系统中,您可以根据gettimeofday定义访问时间,返回当时的时间为1微秒分辨率,请参阅下一节。在其他系统,不幸的是,系统时钟没有比一秒更好的分辨率,你会被time函数作弄
#include <sys/time.h>
struct timeval
{
long tv_sec; /* seconds since Jan. 1, 1970 */
long tv_usec; /* and microseconds */
};
int gettimeofday (struct timeval *tp,
struct timezone *tzp); /* (BSD) */
int gettimeofday (struct timeval *tp); /* (System V.4) */
gettimeofday returns the current system time, with a resolution of 1 microsecond, to tp. The name is misleading, since the struct timeval representation does not relate to the time of day. Many implementations ignore tzp, but others, such as SunOS 4, return time zone information there.
gettimeofday返回当前系统时间,1微秒的分辨率,返回保存到tp参数。该名称具有误导性,因为timeval结构表示不涉及到一天的时间。很多安装启用忽略tzp,但其他的,比如SunOS 4,返回时区信息。
In BSD, gettimeofday is a system call. In some versions of System V.4 it is emulated as a library function defined in terms of time, which limits its resolution to 1 second. Other versions of System V appear to have implemented it as a system call, though this is not documented.
在BSD中,函数gettimeofday是一个系统调用。在SystemV.4的某些版本中它被模拟为在时间上定义的库函数,这就限制了它的分辨率为1秒。其他版本System V似乎已经实现了系统调用,虽然这没有被记录。