Gcc 中的 gettimeofday 函数使用

本文详细介绍了 gettimeofday 函数的使用方法,包括如何获取当前时间并以秒和微妙的形式返回,以及如何获取本地时间信息。此外,还提供了一个示例程序帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


参考链接:http://rabbit.eng.miami.edu/info/functions/time.html

原文链接:http://blog.youkuaiyun.com/hongwenjun/article/details/7795459

gettimeofday
表头文件:#include <sys/time.h>
函数原型: int gettimeofday(struct timeval *tv,struct timezone *tz)
函数说明:把目前的时间按tv所指的结构返回,当时地区的信息则放到tz所指的结构中
struct timeval
{
   long tv_sec; //秒
   long tv_usec;//微妙
}
struct timezone
{
   int tz_minuteswest;//和格林威治时间差了多少分钟
   int tz_dsttime;    //日光节约时间的状态
}

学习代码
[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <sys/time.h>  
  3.   
  4.   
  5. int main(int argc, char** argv)  
  6. {  
  7.     timeval tv;  
  8.     gettimeofday(&tv, NULL);  
  9.     double cl = tv.tv_sec + (double)tv.tv_usec / 1000000;  
  10.   
  11.     getchar(); //暂停等待  
  12.   
  13.     gettimeofday(&tv, NULL);  
  14.     cl = (tv.tv_sec + (double)tv.tv_usec / 1000000) - cl;  
  15.   
  16.     printf("暂停时间:%f\n", cl);  
  17.   
  18.     struct timezone tz;  
  19.     gettimeofday(&tv, &tz);  
  20.     printf("tv.tv_sec:%d\n", tv.tv_sec);  
  21.     printf("tv.tv_usec:%d\n", tv.tv_usec);  
  22.     printf("tz.tz_minuteswest:%d\n", tz.tz_minuteswest);  
  23.     printf("tv.tz_dsttime:%d\n", tz.tz_dsttime);  
  24.   
  25.     return 0;  
  26. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值