netbsd源码(5)

来源:互联网 发布:centos 解压缩 tar.gz 编辑:程序博客网 时间:2024/06/02 05:29

本博客所有内容是原创,如果转载请注明来源

http://blog.csdn.net/myhaspl/


/*-
 * Background information:
 *
 * When converting between timestamps on parallel timescales of differing
 * resolutions it is historical and scientific practice to round down rather
 * than doing 4/5 rounding.
 *
 *   The date changes at midnight, not at noon.
 *
 *   Even at 15:59:59.999999999 it's not four'o'clock.
 *
 *   time_second ticks after N.999999999 not after N.4999999999
 */
在多个平行时间度量的时间戳中进行转换,这是一个有根据的和科学的一轮操作,这比四舍五入要好。
bintime转刚才的秒与纳秒格式。
static __inline void
bintime2timespec(const struct bintime *bt, struct timespec *ts)
{




ts->tv_sec = bt->sec;
ts->tv_nsec =
   (long)(((uint64_t)1000000000 * (uint32_t)(bt-bintime的加法
static __inline void
bintime_add(struct bintime *bt, const struct bintime *bt2)
{
uint64_t u;




u = bt->frac;
bt->frac += bt2-bt->sec--;
bt->sec -= bt2->sec;


/*-
 * Background information:
 *
 * When converting between timestamps on parallel timescales of differing
 * resolutions it is historical and scientific practice to round down rather
 * than doing 4/5 rounding.
 *
 *   The date changes at midnight, not at noon.
 *
 *   Even at 15:59:59.999999999 it's not four'o'clock.
 *
 *   time_second ticks after N.999999999 not after N.4999999999
 */
在多个平行时间度量的时间戳中进行转换,这是一个有根据的和科学的一轮操作,这比四舍五入要好。
bintime转刚才的秒与纳秒格式。
static __inline void
bintime2timespec(const struct bintime *bt, struct timespec *ts)
{




ts->tv_sec = bt->sec;
ts->tv_nsec =
   (long)(((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32);
}
秒与纳秒转bintime


static __inline void
timespec2bintime(const struct timespec *ts, struct bintime *bt)
{




bt->sec = ts->tv_sec;
/* 18446744073 = int(2^64 / 1000000000) */
bt->frac = ts->tv_nsec * (uint64_t)18446744073LL; 
}


bintime转秒和微秒


bintime
static __inline void
bintime2timeval(const struct bintime *bt, struct timeval *tv)
{




tv->tv_sec = bt->sec;
tv->tv_usec =
   (suseconds_t)(((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32);
}


秒与微秒转bintime


static __inline void
timeval2bintime(const struct timeval *tv, struct bintime *bt)
{




bt->sec = tv->tv_sec;
/* 18446744073709 = int(2^64 / 1000000) */
bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
}

#endif /* !defined(_STANDALONE) *}

接着是POSIX 标准的实时部分(POSIX.1b)中时间的操作。

 

/* Operations on timespecs. */

清除

#define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)

检测是否设置,即是否为空。

#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)

比较

#define timespeccmp(tsp, usp, cmp) \

(((tsp)->tv_sec == (usp)->tv_sec) ? \

    ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \

    ((tsp)->tv_sec cmp (usp)->tv_sec))

相加

#define timespecadd(tsp, usp, vsp) \

do { \

(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \

(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \

if ((vsp)->tv_nsec >= 1000000000L) { \

(vsp)->tv_sec++; \

(vsp)->tv_nsec -= 1000000000L; \

} \

} while (/* CONSTCOND */ 0)

减少

#define timespecsub(tsp, usp, vsp) \

do { \

(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \

(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \

if ((vsp)->tv_nsec < 0) { \

(vsp)->tv_sec--; \

(vsp)->tv_nsec += 1000000000L; \

} \

} while (/* CONSTCOND */ 0)

时间中的秒转 纳秒(十亿分之一秒)

#define timespec2ns(x) (((uint64_t)(x)->tv_sec) * 1000000000L + (x)->tv_nsec)

#endif /* _NETBSD_SOURCE */


原创粉丝点击