struct Timeval and Gettimeofday ()

Source: Internet
Author: User
Tags epoch time

Http://www.cppblog.com/lynch/archive/2011/08/05/152520.html

The struct timeval struct is defined in time.h as: struct Timeval {time_t tv_sec;/* Seconds. */suseconds_t tv_usec;/* microseconds. */}; Where Tv_sec is epoch to the number of seconds when the struct timeval is created, Tv_usec is microseconds

The struct timeval struct is defined in time.h as:
struct Timeval
{
time_t tv_sec; /* Seconds. */
suseconds_t tv_usec; /* microseconds. */
};
Where Tv_sec is epoch to the number of seconds to create a struct timeval, tv_usec is the number of microseconds, or a fraction of the seconds later. For example, when I write a blog, the tv_sec is 1244770435,tv_usec 442388, which is the current time from the epoch time 1,244,770,435 seconds, 442388 microseconds. It is important to note that because of the process of looping, new structure variables, and so on, we take some time, we will get the following result:
int i;
for (i = 0; i < 4; ++i)
{
Gettimeofday (&TV, NULL);
printf ("%d\t%d\n", Tv.tv_usec, tv.tv_sec);
Sleep (1);
}
442388 1244770435
443119 1244770436
443543 1244770437
444153 1244770438
Before the number of microseconds, followed by the number of seconds, it can be seen that in this simple operation can only be accurate to the decimal point after one to two bits, or you can see that each cycle, it takes 0.005 seconds to use this program to make a timer is obviously not possible, unless the exact calculation of the resulting code consumption time.

Gettimeofday ()--Get the current time (saved in structure Timeval)

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

int main (int argc, char * argv[]) {

struct Timeval TV; (1)
while (1) {
gettimeofday (&TV, NULL); (2)
printf ("Time%u:%u\n", tv.tv_sec, tv.tv_usec);
Sleep (2);
}
return 0;

}

(1) struct--timeval
--------------------------------------------------
struct Timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
Millisecond MS
Microsecond μs

Timeval represents a point in time, such as:
Timeval.tv_sec = 1 (s)
Timevat.tv_usec = (μs)
1:500 = 1s500000μs= 1.5s

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

The functions Gettimeofday () and Settimeofday () can get and set the time as well as a timezone.
The use of the timezone structure is obsolete; The TZ argument should normally be specified as NULL.

(3) Operation result:
--------------------------------------------------
Time 1,181,788,367:991,487
Time 1,181,788,369:991,602

Indicates the exact time of 2 seconds of sleep: 2s115 μs
http://www.6san.com/575/Unix Time: Epoch time

Unix time, also known as POSIX time/epoch time, is the total number of seconds from 0 seconds from January 1, 1970 0:0 UTC, excluding leap seconds. A positive value represents 1970, and a negative value indicates before 1970.

Unix 2038 bug (Jason hatchet bug)

Speaking of Unix time had to mention Unix 2038 bug (Jason hatchet bug): January 19, 2038 3:14 07 seconds, the Unix time of the 32-bit system will be reset.

A 32-bit UNIX system will represent time in 32-bit binary digits, which can only be represented to coordinated world time, January 19, 2038, 3:14 07 seconds (binary: 01111111 11111111 11111111 11111111), In the next second the binary number will be 10000000 00000000 00000000 00000000, which is negative, so the system will misinterpret the time as December 13, 1901 20:45 52 seconds (also say return to 1970). This may cause problems with the software, causing the system to become paralyzed.

The current solution is to convert the system from 32-bit to 64-bit. Under 64-bit systems, this time can be represented up to 292,277,026,596 December 4 15:30 08 seconds.

Wireshark display timestamp/epoch time

In the menu, select View–>time Display fromat–>seconds Since Epoch (1970-01-01): 1234567890.123456 " You can set the time format for the Wireshark display to the timestamp, epoch time, and the number of seconds since January 1, 1970 (00:00:00 GMT).

Reprint Please specify Source:6san.com
Original address: http://www.6san.com/575/

struct Timeval and Gettimeofday ()

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.