zoukankan      html  css  js  c++  java
  • Linux-C语言中gettimeofday()函数的使用方法(转载)

     

    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 ”,tv.tv_sec);

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

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

            printf(“tz_dsttime:%d ”,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.  
    一分耕耘,一分收获!
  • 相关阅读:
    2019天梯赛训练1
    Python课程设计 搭建博客
    最容易理解的贪吃蛇小游戏
    数据结构-队列
    数据结构-堆栈(2)
    数据结构-堆栈(1)
    数据结构-线性表(3)
    数据结构-线性表(1)
    linux知识积累
    Maven学习笔记
  • 原文地址:https://www.cnblogs.com/splovecyk/p/4385947.html
Copyright © 2011-2022 走看看