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. 触发器应用 trigger

    首先有一张表: create table T_SALARY ( name VARCHAR2(20), age NUMBER(2), salary NUMBER(5) ); insert into t_ ...

  2. mysql版sql助记

    新建用户 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; [host 中 使用 % 为通配符, 匹配任意远程主机] 赋权 GRANT p ...

  3. CRC校验

    小试一下CRC校验的verilog实现,采用最stupid的直接法. /* date : 2014/06/06 designer : pengxiaoen virsion : Altera-Model ...

  4. ASP.NET MVC 5 学习教程:生成的代码详解

    原文 ASP.NET MVC 5 学习教程:生成的代码详解 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 ...

  5. HDU 4883 TIANKENG’s restaurant (贪心)

    链接:pid=4883">带我学习.带我飞 第一次BC,稳挂,WA n多次.今天又一次做了一下 略挫 #include <iostream> #include <cs ...

  6. debian支持ll命令

    debian支持ll命令 $ ll -bash: ll: command not found 没有ll这个命令.尽管也知道ll事实上 是ls -l 这个命令的别名,可是总感觉不是非常习惯.由于之前一直 ...

  7. java高级project师须要掌握的技术

    1.你须要精通面向对象分析与设计(OOA/OOD).涉及模式(GOF,J2EEDP)以及综合模式.你应该十分了解UML,尤其是class,object,interaction以及statediagra ...

  8. Shell基础一

    Shell指令: shell识别三种基本命令:内建命令.Shell函数以及外部命令: 内建命令就是由shell本身所执行的命令.有些命令是由于其必要性才内建的:例如:cd.read: 另一种内建命令的 ...

  9. 2008r2 显示桌面图标

  10. 查询某库所有表的rows &查看当前sql的注册信息

    查询某库所有表的rows &查看当前sql的注册信息 1 2 3 4 5 6 7 select sobj.name,spar.rows FROM sys.objects sobj  INNER ...