C language acquisition of high-precision clock gettimeofday function in Linux

Source: Internet
Author: User
Tags diff

Objective:
In development, there are many times when you need to know the function or the operation of some device on the command, so we need to use Gettimeofday to get the current clock.

One, function description

#include

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

Note:

1. Precise level, subtle level
2. Affected by system time modification
3. The number of seconds returned starts from January 1, 1970 0:0 0 seconds

The parameter TV is the structure that holds the result of the acquisition time, and the parameter tz is used to save the time zone result:

The structure timeval is defined as:

Click (here) to collapse or open

    struct Timeval    {        longint tv_sec;     // number of seconds        Long int tv_usec;     // number of microseconds    }
It gets the time accurate to microseconds (1e-6 s) magnitude

The structure timezone is defined as:

Click (here) to collapse or open

    struct TimeZone    {        int tz_minuteswest; /* Time difference between GMT and West */        int tz_dsttime;    /* How to fix DST time */     }
The timezone parameter is passed in NULL if it is not used.
the value of which tz_dsttime:

Click (here) to collapse or open

Dst_none/*Do not use*/Dst_usa/*United States*/Dst_aust/*Australia*/Dst_wet/*Western Europe*/Dst_met/*Central Europe*/Dst_eet/*Eastern Europe*/Dst_can/*Canada*/DST_GB/*Britannia*/Dst_rum/*Romania*/Dst_tur/*Turkey*/Dst_austalt/*Australia (after 1986)*/
return value
The Success returns 0, the failure returns 1, and the error code is stored in errno.

ERRORS
Efault one of TV or TZ pointed outside the accessible address space.
EINVAL Timezone (or something else) is invalid.
 
Two, example

Click (here) to collapse or open 

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <sys/time.h>5 intTime_substract (structTimeval *result,structTimeval *begin,structTimeval *end)6 {7     if(Begin->tv_sec > End->tv_sec)return-1;8     if((begin->tv_sec = = end->tv_sec) && (Begin->tv_usec > End->tv_usec))return-2;9Result->tv_sec = (end->tv_sec-begin->tv_sec);TenResult->tv_usec = (end->tv_usec-begin->tv_usec); One      A     if(Result->tv_usec <0) -     { -result->tv_sec--; theResult->tv_usec + =1000000; -     } -     return 0; - } + intMainintargcChar**argv) - { +     structtimeval Start,stop,diff; Amemset (&start,0,sizeof(structtimeval)); atmemset (&stop,0,sizeof(structtimeval)); -memset (&diff,0,sizeof(structtimeval)); -Gettimeofday (&start,0); -     //do what you have to do ... -printf"Hello world\n"); -Gettimeofday (&stop,0); inTime_substract (&diff,&start,&stop); -printf"Total time:%d s,%d us\n",(int) Diff.tv_sec, (int) diff.tv_usec); to}
Operation Result:
reprinted from: Http://blog.chinaunix.net/uid-28458801-id-4214306.html

C language acquisition of high-precision clock gettimeofday function in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.