123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include "dbghlp.h"
- /////////////////////////////////////////////////////////////////////////////
- uint64_t Timeval2Us(const struct timeval *ptv)
- {
- if(ptv)
- return (uint64_t)ptv->tv_sec * 1000000 + (uint64_t)ptv->tv_usec;
- return (uint64_t)-1;
- }
- /////////////////////////////////////////////////////////////////////////////
- const struct timeval* Us2Timeval(uint64_t usTime, struct timeval *ptv)
- {
- if(ptv)
- {
- ptv->tv_sec = usTime / 1000000;
- ptv->tv_usec = usTime % 1000000;
- }
- return ptv;
- }
- /////////////////////////////////////////////////////////////////////////////
- uint64_t Timespec2Ns(const struct timespec *pts)
- {
- if(pts)
- return (uint64_t)pts->tv_sec * 1000000000 + (uint64_t)pts->tv_nsec;
- return (uint64_t)-1;
- }
- /////////////////////////////////////////////////////////////////////////////
- const struct timespec* Ns2Timespec(uint64_t nsTime, struct timespec *pts)
- {
- if(pts)
- {
- pts->tv_sec = nsTime / 1000000000;
- pts->tv_nsec = nsTime % 1000000000;
- }
- return pts;
- }
- /////////////////////////////////////////////////////////////////////////////
- int64_t TimespecDiff(const struct timespec *pts1, const struct timespec *pts2)
- {
- int64_t nRet = 0;
- if(pts1 && pts2)
- {
- uint64_t t1 = Timespec2Ns(pts1);
- uint64_t t2 = Timespec2Ns(pts2);
- nRet = (int64_t)(t1 - t2);
- }
- return nRet;
- }
- /////////////////////////////////////////////////////////////////////////////
- void GetTimespec(struct timespec *pts)
- {
- clock_gettime(CLOCK_MONOTONIC, pts);
- }
|