1.简介:

在C语言中可以使用函数gettimeofday()函数来得到时间。它的精度可以达到微妙

2.函数原型:

#include<sys/time.h>

int gettimeofday(struct  timeval*tv,struct  timezone *tz )

3.说明:

gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中

4.结构体:

1>timeval

struct  timeval{

long  tv_sec;/*秒*/

long  tv_usec;/*微妙*/

};

2>timezone 结构定义为:

struct  timezone{

int tz_minuteswest;/*和greenwich 时间差了多少分钟*/

int tz_dsttime;/*type of DST correction*/

}

3>在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。

4>函数执行成功后返回0,失败后返回-1,错误代码存于errno中。

5.程序实例:

#include<stdio.h>
#include<sys/time.h>

#include<unistd.h>

int main()

{

struct  timeval    tv;

struct  timezone   tz;

gettimeofday(&tv,&tz);

printf(“tv_sec:%d\n”,tv.tv_sec);

printf(“tv_usec:%d\n”,tv.tv_usec);

printf(“tz_minuteswest:%d\n”,tz.tz_minuteswest);

printf(“tz_dsttime:%d\n”,tz.tz_dsttime);

}

说明:在使用gettimeofday()函数时,第二个参数一般都为空,因为我们一般都只是为了获得当前时间,而不用获得timezone的数值

gettimeofday()

Get the current time

Synopsis:

#include <sys/time.h>

int gettimeofday( struct timeval * when,
void * not_used );

Arguments:

when
A pointer to a timeval structure where the function can store the time. The struct timeval contains the following members:

  • time_t tv_sec — the number of seconds since the start of the Unix Epoch.
  • suseconds_t tv_usec — the number of microseconds.
not_used
This pointer must be NULL or the behavior of gettimeofday() is unspecified. This argument is provided only for backwards compatibility.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The gettimeofday() function returns the current time in when in seconds and microseconds, since the Unix Epoch, 00:00:00 January 1, 1970 Coordinated Universal Time (UTC) (formerly known as Greenwich Mean Time (GMT)).

Returns:

0 for success, or -1 if an error occurs (errno is set).

Errors:

EFAULT
An error occurred while accessing the when buffer.  

Linux-C语言中gettimeofday()函数的使用方法(转载)的更多相关文章

  1. C语言中malloc函数的使用方法

    C语言中malloc是动态内存分配函数.函数原型:void *malloc(unsigned int num_bytes);参数:num_bytes 是无符号整型,用于表示分配的字节数.返回值:如果分 ...

  2. C语言中system()函数的用法总结(转)

    system()函数功能强大,很多人用却对它的原理知之甚少先看linux版system函数的源码: #include <sys/types.h> #include <sys/wait ...

  3. C语言中qsort函数用法

    C语言中qsort函数用法-示例分析    本文实例汇总介绍了C语言中qsort函数用法,包括针对各种数据类型参数的排序,非常具有实用价值非常具有实用价值. 分享给大家供大家参考.C语言中的qsort ...

  4. 【转】Linux时间函数之gettimeofday()函数之使用方法

    原文网址:http://blog.csdn.net/tigerjibo/article/details/7039434 一.gettimeofday()函数的使用方法: 1.简介: 在C语言中可以使用 ...

  5. 使用C语言中qsort()函数对浮点型数组无法成功排序的问题

    一 写在开头 1.1 本节内容 本节主要内容是有关C语言中qsort()函数的探讨. 二 问题和相应解决方法 qsort()是C标准库中的一个通用的排序函数.它既能对整型数据进行排序也能对浮点型数据进 ...

  6. C语言中malloc函数返回值是否需要类型强制转换问题

    1. 在C语言中, 如果调用的函数没有函数原型, 则其返回值将默认为 int 型. 考虑调用malloc函数时忘记了 #include <stdlib.h>的情况 此时malloc函数返回 ...

  7. R语言中apply函数

    前言 刚开始接触R语言时,会听到各种的R语言使用技巧,其中最重要的一条就是不要用循环,效率特别低,要用向量计算代替循环计算. 那么,这是为什么呢?原因在于R的循环操作for和while,都是基于R语言 ...

  8. (转)C语言中Exit函数的使用

    C语言中Exit函数的使用 exit() 结束当前进程/当前程序/,在整个程序中,只要调用 exit ,就结束return() 是当前函数返回,当然如果是在主函数main, 自然也就结束当前进程了,如 ...

  9. C语言中malloc函数的理解

    在C语言中malloc函数主要是用在堆内存的申请上,使用malloc函数时,函数会返回一个void *类型的值,这个值就是你申请的堆内存的首地址:为什么返回的地址是一个void *类型的地址呢?首先我 ...

随机推荐

  1. BZOJ 1263: [SCOI2006]整数划分( 高精度 )

    yy一下发现好像越小越好...分解成3*3*3*3……这种形式是最好的...然后就是高精度了 ----------------------------------------------------- ...

  2. Android百度地图之显示地图

    添加地图显示 一.在百度官网下载相关的SDK (网址:http://developer.baidu.com/map/sdkandev-download.htm) 解压下载好的BaiduMap_Andr ...

  3. HDU 4267 线段树 离散点区间更新, 自叶子节点至根单点查询

    题意: n个数字 下面n个数字表示数列 2个操作 1 [u, v]  k  add [u,v ]区间 (u点要计算)每隔k个位置,该数字+add 2 pos 询问 pos下标的值(下标从1开始) 思路 ...

  4. Week2(9月19日):增加一个CodeFirst的例子来说明

    Part I:提问  =========================== 1.上堂课中我们使用了()数据库,它是()可部署的,只需要将相应的()文件添加到应用程序的()文件夹,就可以使用了,该数据 ...

  5. javascript语言精粹:继承

    继承提供了2个有用的任务: 1.代码重用 2.引入了一套类型系统的规范,因为程序员无需编写显示类型转换的代码,他们的工作量将大大减轻.这是一件很好的事情,应为类型转换会丧失类型系统在安全上的优势. 在 ...

  6. WPF-22:WPF绘制五角星改进版(增加半个五角星的绘制)-修改bug

    之前用坐标画多边形的方法,绘制五角星.今天调试时发现当时写的时候有bug,修改一下. 原文: http://blog.csdn.net/yysyangyangyangshan/article/deta ...

  7. Codeforces Round #260 (Div. 2)C. Boredom(dp)

    C. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  8. vpn技术原理详解

    转载于:http://blog.csdn.net/quqi99/article/details/7334617 假设有这样一个需求,需要从家中访问公司内网机器,可以用ssh遂道技术来作转发,遂道分正向 ...

  9. BZOJ 2510: 弱题( 矩阵快速幂 )

    每进行一次, 编号为x的数对x, 和(x+1)%N都有贡献 用矩阵快速幂, O(N3logK). 注意到是循环矩阵, 可以把矩阵乘法的复杂度降到O(N2). 所以总复杂度就是O(N2logK) --- ...

  10. java--异常处理总结

    [在程序中抛出异常] 在程序中抛出异常,一定要使用关键字throw. throw+异常实例对象. public class Demo2 { public static void main(String ...