gettimeofday() -- 获取当前时间
来源:互联网 发布:食品网络营销策划书 编辑:程序博客网 时间:2024/05/16 14:42
gettimeofday() -- 获取当前时间(保存在结构体timeval中)
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
int main(int argc, char * argv[]){
struct timeval tv; //(1)
while(1){
gettimeofday(&tv, NULL); //(2)
printf("time %u:%u\n", tv.tv_sec, tv.tv_usec);
sleep(2);
}
return 0;
}
(1) struct--timeval
--------------------------------------------------
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
millisecond 毫秒
microsecond 微秒
timeval表示一个时间点,比如:
timeval.tv_sec = 1 (s)
timevat.tv_usec = 500 000 (μs)
1:500 = 1s500000μs = 1.5s
(2) gettimeofday()
--------------------------------------------------
int gettimeofday(struct timeval *tv, struct timezone *tz);
The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone.
The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL.
(3) 运行结果:
--------------------------------------------------
time 1181788367:991487
time 1181788369:991602
表示睡眠2秒经过的精确时间为: 2s115μs
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
int main(int argc, char * argv[]){
struct timeval tv; //(1)
while(1){
gettimeofday(&tv, NULL); //(2)
printf("time %u:%u\n", tv.tv_sec, tv.tv_usec);
sleep(2);
}
return 0;
}
(1) struct--timeval
--------------------------------------------------
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
millisecond 毫秒
microsecond 微秒
timeval表示一个时间点,比如:
timeval.tv_sec = 1 (s)
timevat.tv_usec = 500 000 (μs)
1:500 = 1s500000μs = 1.5s
(2) gettimeofday()
--------------------------------------------------
int gettimeofday(struct timeval *tv, struct timezone *tz);
The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone.
The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL.
(3) 运行结果:
--------------------------------------------------
time 1181788367:991487
time 1181788369:991602
表示睡眠2秒经过的精确时间为: 2s115μs
- gettimeofday() -- 获取当前时间
- gettimeofday() -- 获取当前时间
- C语言gettimeofday()函数:获取当前时间
- gettimeofday() -- 获取当前时间(保存在结构体timeval中)
- gettimeofday() -- 获取当前时间(保存在结构体timeval中)
- gettimeofday() -- 获取当前时间(保存在结构体timeval中)
- linux下使用gettimeofday获取当前时间的注意事项
- gettimeofday() -- 获取当前时间(保存在结构体timeval中)【转】
- (笔记)linux c获取当前时间的函数gettimeofday(毫秒微秒)
- 时间编程--gettimeofday(获取高精度时间)
- C/C++获取时间方法:gettimeofday()
- gettimeofday时间
- c函数之gettimeofday()函数获取系统时间
- 获取系统当前时间
- 获取当前系统时间
- c++ 获取当前时间
- sql获取当前时间
- 获取当前系统时间
- gamebryo 2.5 编译找不到XINPUT_BATTERY_INFORMATION
- 转载一份 关于appwidget 很详细
- ACMSTEP 2.1.5 又见GCD //数论 GCD
- 我们【即将面对】和【已经面对】的那些事儿
- 总结学习到得知识
- gettimeofday() -- 获取当前时间
- 用perl操作word
- Unable to start activity ComponentInfo
- linux查看系统性能常用命令
- 说的好啊,基础是王道。
- Matlab与C++接口与混合编程讨论小结
- 开源代码库与VS的库冲突的解决方法
- 加密 解密【通俗易懂】
- WebBrowser 不打开新窗口