Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:16:12+00:00 2026-06-04T02:16:12+00:00

struct timeval start, end, duration; gettimeofday(&start, NULL); res = curl_easy_perform(curl); gettimeofday(&end, NULL); timersub(&end, &start,

  • 0
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 buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", startTime);
char buf2[64];
strftime(buf2, sizeof(buf2), "%Y-%m-%d %H:%M:%S", endTime);

ofstream timeFile;
timeFile.open ("timingSheet.txt");
timeFile << fixed << showpoint;
timeFile << setprecision(6);
timeFile << "Duration: " << duration.tv_sec << "." << duration.tv_usec << " seconds \n";
timeFile << "Start time: " <<  buf <<"." << start.tv_usec << "\n";
timeFile << "End time: " <<  buf2 <<"." << end.tv_usec << "\n";
timeFile.close();

When I run this code I get this output:

Duration: 3.462243 seconds 
Start time: 2012-05-15 17:14:07.432613
End time: 2012-05-15 17:14:07.894856

What puzzles me is that the duration value doesn’t match up with the Start and End times. The two dates only differ by the micro seconds. Is there a reason for this?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:16:15+00:00Added an answer on June 4, 2026 at 2:16 am

    localtime return a statically allocated buffer and you call it twice ,so StartTime and EndTime are the same. You need to copy it in another buffer directcly after each call.

    tm* startTime = localtime(&start.tv_sec); 
    char buf[64]; 
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", startTime); 
    
    tm* endTime = localtime(&end.tv_sec); 
    char buf2[64]; 
    strftime(buf2, sizeof(buf2), "%Y-%m-%d %H:%M:%S", endTime); 
    

    EDIT: you can also write this:

    tm* pTimeBuf = localtime(&start.tv_sec); 
    char buf[64]; 
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", pTimeBuf); 
    
    localtime(&end.tv_sec); // NB. I don't store th return value (since I have it already)
    char buf2[64]; 
    strftime(buf2, sizeof(buf2), "%Y-%m-%d %H:%M:%S", pTimeBuf); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

struct timeval start, end; start.tv_usec = 0; end.tv_usec = 0; gettimeofday(&start, NULL); functionA(); gettimeofday(&end,
struct timeval start, end; . . . elapsedTime = (((end.tv_sec * 1000000) - (start.tv_sec
struct timeval represents and instant in time with two members, tv_sec (seconds) and tv_usec
POSIX uses struct timeval to represent time intervals. struct timeval { time_t tv_sec; unsigned
Could you please help me how to format a struct timeval instance to human
struct Div { int i; int j; }; class A { public: A(); Div&
i need to exchange a struct timeval between kernl-space and user-space (through a netlink
Given a time_t or struct timeval, how do I get the timeval or time_t
I am basically trying to get the time with struct timeval tv = {
I have a C program containing a structure struct S{ int x; struct timeval

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.