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

  • SEARCH
  • Home
  • 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 7775743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:42:26+00:00 2026-06-01T17:42:26+00:00

Depending on the amount of input data, I have a program that runs in

  • 0

Depending on the amount of input data, I have a program that runs in seconds or in days. At the end of my program, I want to print the elapsed “clock wall” time: in seconds if it is less then one minute, in min and sec if it is less than one hour, in hour-min-sec if it is less than one day, and in day-hour-min-sec otherwise. Here is the code I am using:

#include <cstdio>
#include <ctime>
#include <unistd.h> // for sleep

int main (int argc, char ** argv)
{
  time_t startRawTime, endRawTime;

  time (&startRawTime);
  printf ("%s", ctime (&startRawTime));

  sleep (3); // any preprocessing of input data

  time (&endRawTime);
  printf ("%s", ctime (&endRawTime));

  printf ("%.0fs\n", difftime (endRawTime, startRawTime));

  time_t elapsed = static_cast<time_t>(difftime (endRawTime, startRawTime));
  struct tm * ptm = gmtime (&elapsed);
  printf ("%id %ih %im %is\n", ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);

  return 0;
}

Here is what it prints:

Mon Apr  9 14:43:16 2012
Mon Apr  9 14:43:19 2012
3s
1d 0h 0m 3s

Of course the last line is wrong (it should be “0d”). It seems it can be solved easily by printing ptm->tm_mday - 1. However, ptm->tm_mday will also be “1” when there really was one day elapsed between the two dates. And so in that case, I don’t want to make it appear as “0d”.

So is there a way to handle this properly? Or should I get the result of difftime as a double (that is, as a number of seconds) and then calculate myself the number of sec/min/hours/days?

Remark: my code is used only on Linux, compiled with gcc -lstdc++.

  • 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-01T17:42:27+00:00Added an answer on June 1, 2026 at 5:42 pm

    A time_t value represents a particular moment in time. The result of difftime is the interval, in seconds, between two moments. That’s a very different thing.

    In your code, difftime() returns 3.0, since there are 3 seconds between the two specified times. Converting that to time_t gives you a moment 3 seconds after the epoch; on most systems, that’s going to be 3 seconds past midnight GMT on January 1, 1970. The tm_mday value is 1 because that was the first day of the month.

    You might be able to make this work by subtracting 1 from the tm_mday value, since tm_mday is 1-based rather than 0-based. But you’ll still get meaningless results for longer intervals. For example, an interval of 31.5 days will give you noon on February 1, because January has 31 days; that’s not relevant to the information you’re trying to get.

    Just treat the result of difftime() as a double (because that’s what it is) and compute the number of days, hours, minutes, and seconds by simple arithmetic.

    (With some loss of portability, you can just subract the time_t values directly rather than using difftime(). That will make some of the arithmetic a little easier, but it will break on systems where a time_t value is something other than an integer count of seconds since some epoch. difftime() exists for a reason.)

    Of course the last line is wrong (it should be “0d”). It seems it can
    be solved easily by printing “ptm->tm_mday – 1”. However, ptm->tm_mday
    will also be “1” when there really was one day elapsed between the two
    dates. And so in that case, I don’t want to make it appear as “0d”.

    That’s not correct; if the time interval is just over 1 day, ptm->tm_mday will be 2. You can verify this with a small modification to your code:

    time (&endRawTime);
    endRawTime += 86400; // add this line
    printf ("%s", ctime (&endRawTime));
    

    When I make this change, I get this output:

    Mon Apr  9 13:56:49 2012
    Tue Apr 10 13:56:52 2012
    86403s
    2d 0h 0m 3s
    

    which could be corrected by subtracting 1 from ptm->tm_mday. But again, that’s not the right approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to display an UIProgressbar depending on the amount of data downloaded from
I have an action method that depending on some conditions needs to return a
I have a form where depending on the website's brand one of two input
I have got an ASP page that does some back-end processing. It calls a
Depending on my task in Vim I have several tabs open. How can I
Depending on where I use my Class, I want to be able to show
I want to change the logging level depending if I'm debbugging or not, but
I've got a bit of an issue... We have an application that needs to
I have an assignment that asks me to retrieve delimited text from form values.
My program is as follows: #Question 6_2010.py PlayerOneScore=0 PlayerTwoScore=0 NoOfGamesInMatch=(int(input('How many games in the

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.