Unix下时间相关函数
2013-10-23 18:45
162 查看
本文系转载http://blog.chinaunix.net/uid-20548989-id-2533161.html,以防源链unaccessible.
另一有价值的博文是:/article/7608580.html
一、struct timeval结构体
struct timeval结构体在time.h中的定义为:
struct timeval
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* Microseconds. */
};
其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。比如当前我写博文时的tv_sec为1244770435,tv_usec为442388,即当前时间距Epoch时间1244770435秒,442388微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:
#include <sys/time.h>
#include <stdio.h>
int
main(void)
{
int i;
struct timeval tv;
for(i = 0; i < 4; i++){
gettimeofday(&tv, NULL);
printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);
sleep(1);
}
return 0;
}
329612 1314851429
329782 1314851430
329911 1314851431
330036 1314851432
前面为微秒数,后面为秒数,可以看出,在这个简单运算中,只能精确到小数点后面一到两位,或者可以看出,每进行一次循环,均需花费0.005秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。
二、gettimeofday()函数
原型:
/* 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 <time.h> instead. */
extern int gettimeofday (struct timeval *__restrict __tv,
__timezone_ptr_t __tz) __THROW __nonnull ((1));
gettimeofday()功能是得到当前时间和时区,分别写到tv和tz中,如果tz为NULL则不向tz写入。
另一有价值的博文是:/article/7608580.html
一、struct timeval结构体
struct timeval结构体在time.h中的定义为:
struct timeval
{
__time_t tv_sec; /* Seconds. */
__suseconds_t tv_usec; /* Microseconds. */
};
其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。比如当前我写博文时的tv_sec为1244770435,tv_usec为442388,即当前时间距Epoch时间1244770435秒,442388微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:
#include <sys/time.h>
#include <stdio.h>
int
main(void)
{
int i;
struct timeval tv;
for(i = 0; i < 4; i++){
gettimeofday(&tv, NULL);
printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);
sleep(1);
}
return 0;
}
329612 1314851429
329782 1314851430
329911 1314851431
330036 1314851432
前面为微秒数,后面为秒数,可以看出,在这个简单运算中,只能精确到小数点后面一到两位,或者可以看出,每进行一次循环,均需花费0.005秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。
二、gettimeofday()函数
原型:
/* 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 <time.h> instead. */
extern int gettimeofday (struct timeval *__restrict __tv,
__timezone_ptr_t __tz) __THROW __nonnull ((1));
gettimeofday()功能是得到当前时间和时区,分别写到tv和tz中,如果tz为NULL则不向tz写入。
相关文章推荐
- Unix时间相关的函数
- unix 时间的相关函数
- mysql日期相关函数大全,日期,字符串,Unix时间戳...
- UNIX时间相关的结构体和函数
- unix 时间的相关函数
- FROM_UNIXTIME 格式化MYSQL时间戳函数
- Vim技能修炼教程(15) - 时间和日期相关函数
- JS中的与时间相关的函数(long与date,timestamp之间的转换)
- FROM_UNIXTIME 格式化MYSQL时间戳函数
- java_时间戳与Date_相互转化相关函数
- Python中与时间相关的函数
- js时间格式化函数,支持Unix时间戳
- SQL Server中经常会用到的与时间相关的函数
- Java中时间相关函数Date、Calendar、SimpleDateFormat
- linux中与时间处理相关的函数
- uC/OS-II 函数之时间相关函数
- T-SQL: 15 个与日期时间相关的精典语句函数
- MTK 和时间相关的两个函数
- 分享几个php时间相关的函数
- MySQL插入时间戳用函数:UNIX_TIMESTAMP()