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. mysql中if语句

    #1.IF表达式 IF(condition,expr1,expr2) //如果condition成立返回expr1,否则返回expr2 #2.IFNULL表达式 IFNULL(expr1,expr2) ...

  2. linux(Centos 6.3)学习笔记

    一.系统分区    1,磁盘分区        使用分区编辑器(partition editor)在磁盘上划分几个逻辑部分.碟片一旦划分成          数个分区,不同类的目录与文件可以存储进不同 ...

  3. QML在XP等显卡明显不好的情况下 可以参考

    http://doc.qt.io/qt-5/windows-requirements.html

  4. SDUTOJ 2128 树结构练习——排序二叉树的中序遍历

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvUl9NaXNheWE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  5. 【翻译自mos文章】当指定asm disk 为FRA时,11.2.0.3的dbua hang住

    当指定asm disk 为FRA时.11.2.0.3的dbua hang住 来源于: 11.2.0.3 DBUA Hangs While Specifying ASM Disk To FRA (文档 ...

  6. RelativeLayout的属性详解

    1. android:layout_below="@+id/first" //在某元素的的下方: android:layout_alignBottom="@+id/fir ...

  7. Java --CountDownLatch简介

    CountDownLatch 1.类介绍 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.用给定的计数 初始化 CountDownLatch.由于调用了 coun ...

  8. 02-UIKit控件、MVC

    目录: 一.控件使用 二.动态类型和静态类型 三.MVC 四.UIAlertView对话框 回到顶部 一.控件使用 1 事件源,事件处理方法有一个参数传进来,那个参数就是触发这个事件的时间源. UIS ...

  9. mysql 初识之日志文件篇

    日志文件 1. err日志     error log 记录mysql在运行的过程中所有较为严重的警告和错误信息,以及mysql server每次启动和关闭的详细信息.系统在默认情况下关闭error ...

  10. windows api 梳理

    PathMatchSpec Function Searches a string using a Microsoft MS-DOS wild card match type. Syntax BOOL  ...