Questions tagged [timeval]
56 questions
2
votes
1 answer
choose between timeval and clock() to calculate elapsed time in C
I am using timeval as well as clock() function to see the time difference between 2 actions in my c program. Somehow timeval seems to give me the right amount of elapsed time in milliseconds where clock() gives very very less value.
g_time =…

Gaurav Kumar
- 153
- 10
1
vote
2 answers
Why isn't the timer running when I enter 1000 milliseconds as the interval?
I am working on a timer that gives timestamps for every interrupt at any amount of milliseconds the user wants it to shoot off at.
struct itimerspec takes seconds and nanoseconds when using:
it_interval (a member of the struct)
I want to let the…

Mr.Longbottom
- 23
- 6
1
vote
1 answer
Add or subtract an interval from a timeval
I've seen a number of questions / answers for subtracting two datetimes here but not one for adding or subtracting an interval to / from a timevalue.
Add milliseconds to timeval C++ This asnwer does not result in a valid timeval structure.
void…

James Newton
- 698
- 9
- 19
1
vote
1 answer
How is it possible that tv_sec of both timeval structures are the same but subtracting them gives me a non zero value
timeval end_time;
timeval Dbg_timer;
The above time structures where initialized with gettimeofday() at some point.
Below is how I calculated the time difference:
long int elaspsed_time_s = end_time.tv_sec -…

African_king
- 177
- 1
- 8
1
vote
1 answer
Obtain hardware timestamp
Is the time obtained by the pcap_next_ex function in libpcap the hard timestamp of the network card? If not, how to obtain the hard timestamp when the packet is received
pcap_next_ex(pcap_t* p,struct pcap_pkthdr** pkt_header,const u_char** pkt_data)

Junsheng Shen
- 51
- 4
1
vote
1 answer
IF function to calculate duration (Hours:Minutes:Seconds)
I use Importxml to extract the clock from a website that tells the date and time that the market will close ... Well, using the Now () function, I calculate the imported clock minus the current time. I converted the cell with the result of the…

Digital Farmer
- 1,705
- 5
- 17
- 67
1
vote
0 answers
C - Convert between struct timeval and uint32_t to calculate elapsed time
I read a lot of posts on stackoverflow about endianness for network.
For example, if I do something like this :
struct timeval start_t
gettimeofday(&start_t, NULL);
Now , how can I be sure this kind of cast will work ?
uint32_t receivedTime =…

jy95
- 773
- 13
- 36
1
vote
0 answers
Use of struct timeval to set the response timeout on LibModbus
I am using the library LibModbus to connect with ADAM devices. I had some previous problem sending some commands to the device but setting a bigger response timeout it was fixed, this was in a previous version of the library.
Now, I am using the…

Aday88
- 61
- 2
- 10
1
vote
2 answers
Timeval structs: negative delay with libpcap
I'm trying to calculate delay from a captured packet to a previous packet, both coming from the same connection.
I'm using a single linked list, each node corresponding to a connection; I discern packets coming from IP1 to IP2 from the ones coming…

elmazzun
- 1,066
- 2
- 18
- 44
1
vote
3 answers
timeval to string (converting between the two)
I'm trying to pull the two components out of a timeval struct and place them into strings.
I'm not having much luck with this. I've attempted casting and converting first to a long and then to a string. I need the most efficient way to do this.
Any…

BSchlinker
- 3,401
- 11
- 51
- 82
1
vote
1 answer
Better way to get absolute time?
Currently I'm trying to get absolute time to use with pthread_mutex_timedlock. I know I need to add the timeval from gettimeofday to the timespec and then add my arbitrary amount of time.
The below works but it might overflow when multiplying with…

Brandon
- 22,723
- 11
- 93
- 186
1
vote
0 answers
localtime() is showing 0 microseconds since 1970 as one hour
I have a timeval which is 0 seconds and 0 microseconds. I am therefore expecting zero hours:
//Create a timeval representing 0 microseconds since 1970
struct timeval test;
test.tv_sec = 0;
test.tv_usec = 0;
struct tm *nowtm2;
time_t…

user997112
- 29,025
- 43
- 182
- 361
1
vote
2 answers
warning: format '%06d' expects type 'int', but argument 5 has type '__suseconds_t'
I am using this code:
struct timeval tv;
time_t nowtime;
struct tm *nowtm;
char tmbuf[64], buf[64];
gettimeofday(&tv, NULL);
nowtime = tv.tv_sec;
nowtm = localtime(&nowtime);
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
snprintf(buf,…

user997112
- 29,025
- 43
- 182
- 361
1
vote
4 answers
timeval undefined when using windows.h and WIN32_LEAN_AND_MEAN
To avoid conflicts with winsock2.h, I want to wrap my include of windows.h with WIN32_LEAN_AND_MEAN (I undef it after windows.h so as not to interfere with applications that include my headers). Doing this causes timeval to be undefined when…

tjd
- 467
- 1
- 8
- 14
1
vote
2 answers
Mismatch in timing
struct timeval start, end, duration;
gettimeofday(&start, NULL);
res = curl_easy_perform(curl);
gettimeofday(&end, NULL);
timersub(&end, &start, &duration);
tm* startTime = localtime(&start.tv_sec);
tm* endTime = localtime(&end.tv_sec);
char…

Dave
- 1,645
- 2
- 23
- 39