Accurately obtain the system time in Linux/Unix

Source: Internet
Author: User

In the analysis of live555CodeOccasional acquisition:

If (fisfirstpacket) {// record the fact that we're starting to play now: gettimeofday (& fnextsendtime, null );}

UNIX and Linux time systems start from epoch, in seconds. Epoch is the Greenwich Mean Time (Greenwich Mean Time) specified at 00:00:00, January 1, January 1, 1970. At present, most Unix systems use 32-bit to record time. Positive values are expressed after 1970, and negative values are expressed before 1970.

Next let's take a look at the time function in time. h, and the gettimeofday function in sys/time. h.

# Include <time. h>
# Include <sys/time. h>
# Include <stdio. h>

Int main (){
Time_t tt;
Struct timeval TV;
TT = Time (null );
Gettimeofday (& TV, null );
Printf ("time_t sec is % d \ n", TT );
Printf ("gettimeofday TV _sec is % d, USEC is % d \ n", TV. TV _sec, TV. TV _usec );
}

PS: the time and gettimeofday functions get the number of seconds from epoch to the current one (TT = TV. TV _sec), and the latter can get more precise results in microseconds, that is, TV _sec * (10 ^ 6) + TV _usec is the number of microseconds from epoch to the current

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.