目录
测试代码
结构体 timeval
结构体 timespec
函数gettimeofday()
如下:每秒打印一次输出。
#include
#include
#include
typedef signed char int8_t;
typedef short int int16_t;
typedef int int32_t;
typedef long int int64_t;
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long int uint64_t;
/* General types */
typedef uint8_t boolean;
#define TRUE 1
#define FALSE 0
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef int64_t int64;
typedef uint64_t uint64;
typedef float float32;
typedef double float64;
#define USECS_PER_SEC 1000000
typedef struct
{
uint32 sec; /*< Seconds elapsed since the Epoch (Jan 1, 1970) */
int32 usec; /*< Microseconds elapsed since last second boundary */
} ec_timet;
typedef struct timer
{
ec_timet stop_time;
} timert;
void timer_start (timert * self, uint32 timeout_usec)
{
struct timeval start_time;
struct timeval timeout;
struct timeval stop_time;
gettimeofday (&start_time, 0);
timeout.tv_sec = timeout_usec / USECS_PER_SEC;
timeout.tv_usec = timeout_usec % USECS_PER_SEC;
timeradd (&start_time, &timeout, &stop_time);
self->stop_time.sec = stop_time.tv_sec;
self->stop_time.usec = stop_time.tv_usec;
}
boolean timer_is_expired (timert * self)
{
struct timeval current_time;
struct timeval stop_time;
int is_not_yet_expired;
gettimeofday (¤t_time, 0);
stop_time.tv_sec = self->stop_time.sec;
stop_time.tv_usec = self->stop_time.usec;
is_not_yet_expired = timercmp (¤t_time, &stop_time, <);
return is_not_yet_expired == FALSE;
}
//1 秒 = 1000 毫秒=1000,000 微秒 1 秒=1000,000,000 纳秒
void main()
{
uint32 i = 0;
uint32 s_count = 10;
uint32 us_count=s_count*USECS_PER_SEC;
timert timer1;
struct timeval act_timer1 ;
struct timeval last_timer1 ;
timer_start (&timer1, us_count);//us
do
{
i = i + 1;
gettimeofday (&act_timer1, 0);
if (act_timer1.tv_sec != last_timer1.tv_sec)
{
printf ("Actual Secs = %ld\t, number = %d\n", act_timer1.tv_sec,i);//i = 每秒计算多少次。
//printf ("number = %d\n",i);
i=0;
}
last_timer1.tv_sec = act_timer1.tv_sec;
/* wait for answer with WKC>=0 or otherwise retry until timeout */
} while (!timer_is_expired (&timer1));
printf ("timer expried \n");
}
运行结果如下:
zzh@ubuntu:~/linux/learn/test_timer$ gcc timer.c
zzh@ubuntu:~/linux/learn/test_timer$ ./a.out
Actual Secs = 1690299755 , number = 1
Actual Secs = 1690299756 , number = 16901286
Actual Secs = 1690299757 , number = 20853513
Actual Secs = 1690299758 , number = 20693789
Actual Secs = 1690299759 , number = 20792398
Actual Secs = 1690299760 , number = 20695619
Actual Secs = 1690299761 , number = 20902803
Actual Secs = 1690299762 , number = 20854518
Actual Secs = 1690299763 , number = 20923948
Actual Secs = 1690299764 , number = 20872630
Actual Secs = 1690299765 , number = 20915321
timer expried
zzh@ubuntu:~/linux/learn/test_timer$
知识点总结:
/* A time value that is accurate to the nearest
microsecond but also has a range of years. */
struct timeval
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* Microseconds. */
};
该结构体只是包括: tv_sec 单位是秒,tv_usec单位是微秒。
struct timespec
{
__time_t tv_sec; /* Seconds. */
__syscall_slong_t tv_nsec; /* Nanoseconds. */
};
该结构体只是包括: tv_sec 单位是秒,tv_nsec单位是纳秒。
/* Get the current time of day and timezone information,
putting it into *TV and *TZ. If TZ is NULL, *TZ is not filled.
Returns 0 on success, -1 on errors.
NOTE: This form of timezone information is obsolete.
Use the functions and variables declared in instead. */
extern int gettimeofday (struct timeval *__restrict __tv,
__timezone_ptr_t __tz) __THROW __nonnull ((1));
获取当前时间和时区,为UTC时间,1970年以来的秒+微秒、时区信息。