linux时间戳精确到毫秒,请问:如何得到精确到毫秒的时间?

ftime or gethrtime

Standard C Library Functions                            ftime(3C)

NAME

ftime - get date and time

SYNOPSIS

#include

int ftime(struct timeb *tp);

DESCRIPTION

The ftime() function sets the time and  millitm  members  of

the  timeb  structure  pointed  to  by  tp. The structure is

defined in and contains the following members:

time_t           time;

unsigned short   millitm;

short            timezone;

short            dstflag;

The time and millitm members contain the  seconds  and  mil-

liseconds  portions,  respectively,  of  the current time in

seconds since 00:00:00  UTC  (Coordinated  Universal  Time),

January 1, 1970.

The timezone  member  contains  the  local  time  zone.  The

dstflag  member contains a flag that, if non-zero, indicates

that  Daylight  Saving  time  applies  locally  during   the

appropriate part of the year.

The contents of the timezone and dstflag members of tp after

a call to ftime() are unspecified.

RETURN VALUES

Upon successful completion, the ftime() function returns  0.

Otherwise -1 is returned.

ERRORS

No errors are defined.

USAGE

For portability to  implementations  conforming  to  earlier

versions  of  this  document, time(2) is preferred over this

function.

The millisecond value usually has a granularity greater than

one due to the resolution of the system clock.  Depending on

any granularity (particularly a granularity of one)  renders

code non-portable.

SEE ALSO

date(1), time(2), ctime(3C), gettimeofday(3C), timezone(4)

Standard C Library Functions                        gethrtime(3C)

NAME

gethrtime, gethrvtime - get high resolution time

SYNOPSIS

#include

hrtime_t gethrtime(void);

hrtime_t gethrvtime(void);

DESCRIPTION

The gethrtime() function returns the current high-resolution

real time. Time is expressed as nanoseconds since some arbi-

trary time in the past; it is not correlated in any  way  to

the  time  of  day,  and thus is not subject to resetting or

drifting by way of adjtime(2) or settimeofday(3C).  The  hi-

res  timer  is  ideally  suited  to  performance measurement

tasks, where cheap, accurate interval timing is required.

The  gethrvtime()  function  returns   the   current   high-

resolution  LWP virtual time, expressed as total nanoseconds

of execution time. This function requires that  micro  state

accounting be enabled with the ptime utility (see proc(1)).

The gethrtime() and gethrvtime() functions  both  return  an

hrtime_t, which is a 64-bit (long long) signed integer.

EXAMPLES

The following code fragment measures  the  average  cost  of

getpid(2):

hrtime_t start, end;

int i, iters = 100;

start = gethrtime();

for (i = 0; i < iters; i++)

getpid();

end = gethrtime();

printf("Avg getpid() time = %lld nsec\n", (end - start) / iters);

ATTRIBUTES

See attributes(5) for descriptions of the  following  attri-

butes:

____________________________________________________________

|       ATTRIBUTE TYPE        |       ATTRIBUTE VALUE       |

|_____________________________|_____________________________|

| MT-Level                    | MT-Safe                     |

|_____________________________|_____________________________|

SEE ALSO

proc(1),  adjtime(2),  gettimeofday(3C),   settimeofday(3C),

attributes(5)

NOTES

Although the units  of  hi-res  time  are  always  the  same

(nanoseconds),  the actual resolution is hardware dependent.

Hi-res time is guaranteed to be monotonic (it won't go back-

ward, it won't periodically wrap) and linear (it won't occa-

sionally speed up or slow down for adjustment, like the time

of  day  can),  but not necessarily unique: two sufficiently

proximate calls may return the same value.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值