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. centos下yum安装crontab+mysql自动备份

    参考博文: centos下yum安装crontab yum install vixie-cron crontabs      //安装 chkconfig crond on               ...

  2. 生成pdf文件

  3. 基于visual Studio2013解决C语言竞赛题之0505选数

     题目

  4. POJ 2777 线段树基础题

    题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...

  5. glog另启动线程写文本日志

    glog本身是很高效的,google的大牛肯定知道大规模的写日志用glog的话肯定会影响业务线程的处理,带负荷的磁盘IO谁都桑不起.比方levelDB就是默认异步写,更不用说google的三驾马车都是 ...

  6. 再造 “手机QQ” 侧滑菜单(一)——实现侧滑效果

    本系列文章中,我们将尝试再造手机QQ的侧滑菜单,力争最大限度接近手Q的实际效果,并使用 Auto Layout 仿造左侧菜单,实现和主视图的联动. 代码示例:https://github.com/jo ...

  7. 用Swift完成不同View Controller之间的切换

    之前用objective-c开发时,页面之间的切换很容易.其实用swift没有很大的变化,如果你是用storyboard完成的界面,基本上是同样的方式,只不过在代码部分写成swift风格的就行了. 今 ...

  8. Android SurfaceView实战 带你玩转flabby bird (下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43063331,本文出自:[张鸿洋的博客] 1.概述 在Android Surfa ...

  9. 11-UIKit(Storyboard、View的基本概念、绘制图形、UIBezierPath)

    目录: 1. Storyboard 2. Views 3. View的基本概念介绍 4. 绘制图形 5. UIBezierPath 回到顶部 1. Storyboard 1.1 静态表视图 1)Sec ...

  10. jQuery格式化时间插件formatDate

    一.不传时间 $.formatDate("yyyy-MM-dd HH:mm:ss");   二.传时间 $.formatDate("yyyy-MM-dd HH:mm:ss ...