944,022 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 80403
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 15th, 2005
1

convert seconds to minutes, hours, days, months, years, etc.

Expand Post »
Is there a standard algorithm that can take the output of difftime (which is in seconds) and use it to print out the number of minutes, hours, days, weeks, etc.?

I was going to try and calculate this myself by using the modulo, etc. but thought that something might pre-exist..
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 15th, 2005
0

Re: convert seconds to minutes, hours, days, months, years, etc.

There's probably a function or set of functions that do what you want, but nothing in the standard library. It would be easier to just do it manually since the algorithm isn't that hard.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 15th, 2005
0

Re: convert seconds to minutes, hours, days, months, years, etc.

Got anything handy?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 15th, 2005
0

Re: convert seconds to minutes, hours, days, months, years, etc.

It's as simple as this, more or less:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. double seconds = 1234567;
  6. double minutes = seconds / 60;
  7. double hours = minutes / 60;
  8. double days = hours / 24;
  9. double weeks = days / 7;
  10. double months = weeks / 52;
  11. double years = months / 12;
  12.  
  13. std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
  14. std::cout.precision(3);
  15.  
  16. std::cout << "Seconds: " << seconds << '\n'
  17. << "Minutes: " << minutes << '\n'
  18. << "Hours: " << hours << '\n'
  19. << "Days: " << days << '\n'
  20. << "Weeks: " << weeks << '\n'
  21. << "Months: " << months << '\n'
  22. << "Years: " << years << '\n';
  23. }
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 15th, 2005
0

Re: convert seconds to minutes, hours, days, months, years, etc.

Dogtree,

Thanks - I was planning however, to only show minutes if it was over 60 seconds, only show hours if it was over 60 minutes, etc.. That's where it got a little hairy..
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 16th, 2005
0

Re: convert seconds to minutes, hours, days, months, years, etc.

Not hairy at all. If integral division results in 0, don't show it:
  1. #include <functional>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. template <typename Pred>
  6. void print_if(int part, Pred p, std::string msg = "")
  7. {
  8. if (p(part))
  9. std::cout << msg << part << '\n';
  10. }
  11.  
  12. int main()
  13. {
  14. int seconds = 1234567;
  15. int minutes = seconds / 60;
  16. int hours = minutes / 60;
  17. int days = hours / 24;
  18. int weeks = days / 7;
  19. int months = weeks / 52;
  20. int years = months / 12;
  21.  
  22. std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
  23. std::cout.precision(3);
  24.  
  25. print_if(seconds, std::bind2nd(std::greater<int>(), 0), "Seconds: ");
  26. print_if(minutes, std::bind2nd(std::greater<int>(), 0), "Minutes: ");
  27. print_if(hours, std::bind2nd(std::greater<int>(), 0), "Hours: ");
  28. print_if(days, std::bind2nd(std::greater<int>(), 0), "Days: ");
  29. print_if(months, std::bind2nd(std::greater<int>(), 0), "Months: ");
  30. print_if(years, std::bind2nd(std::greater<int>(), 0), "Years: ");
  31. }
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 16th, 2005
0

Re: convert seconds to minutes, hours, days, months, years, etc.

This isn't the output I'm seeking - what you've provided is the conversion between seconds and each parameter (hours, minutes, etc.)

basically, if the minutes is above 60 I don't want to 61, etc.

This is how I ended up doing it:
  1. string Date::operator - ( const Date& b )
  2. {
  3. double diff = difftime( rawtime, b.rawtime );
  4. if ( diff < 0 )
  5. diff*=-1;
  6. ostringstream o;
  7.  
  8.  
  9. if ( diff < 60 )
  10. {
  11. o<<diff<<" SECOND(S)";
  12. return o.str();
  13. }
  14.  
  15. if ( diff < 3600 )
  16. {
  17. int min = (int)diff/60;
  18. int sec= (int)diff%60;
  19. o<<min<<" MINUTE(S), "<<sec<< " SECOND(S)";
  20. }
  21. else if ( diff < 86400 ) /* DAY */
  22. {
  23. int hours = (int) diff/3600;
  24. int hourRemainder = (int)diff%3600;
  25. int min = (int)hourRemainder/60;
  26. int sec= (int)diff%60;
  27. o<<hours<< " HOUR(S), "<< min<< " MINUTE(S), "<<sec<< " SECOND(S)";
  28. }
  29. else if ( diff < 31536000 ) /* YEAR */
  30. {
  31. int days = (int) diff/86400;
  32. int daysRemainder = (int)diff%86400;
  33. int hours = (int) daysRemainder/3600;
  34. int hourRemainder = (int)(diff - 86400)%3600;
  35. int min = (int)hourRemainder/60;
  36. int sec= (int)diff%60;
  37. o<<days<<" DAY(S), "<<hours<< " HOUR(S), "<< min<< " MINUTE(S), "<<sec<< " SECOND(S)";
  38. }
  39. else
  40. {
  41. int years = (int) diff/31536000;
  42. int yearsRemainder = (int) diff%31536000;
  43. int days = (int) yearsRemainder/86400;
  44. int daysRemainder = (int)diff%86400;
  45. int hours = (int) daysRemainder/3600;
  46. int hourRemainder = (int)(diff - 86400)%3600;
  47. int min = (int)hourRemainder/60;
  48. int sec= (int)diff%60;
  49. o<<years<<" YEAR(S), "<<days<<" DAY(S), "<<hours<< " HOUR(S), "<< min<< " MINUTE(S), "<<sec<< " SECOND(S)";
  50. }
  51.  
  52.  
  53.  
  54. return o.str();
  55. }

Here's an example in action:
NOW:05/16/05 20:39:18
LATER:05/17/07 21:58:21
Difference:2 YEAR(S), 1 DAY(S), 1 HOUR(S), 19 MINUTE(S), 3 SECOND(S)
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 16th, 2005
0

Re: convert seconds to minutes, hours, days, months, years, etc.

I wasn't trying to solve your problem for you, sorry if I gave you that impression. I'm glad you figured it out without my help anyway.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 17th, 2005
0

Re: convert seconds to minutes, hours, days, months, years, etc.

Well have you guys ever heard of the MOD (%) function.

Well let's say the count was 153 seconds.
  1. char *print_time( int time )
  2. {
  3. static char buf[20];
  4. char tmp[200];
  5.  
  6. buf[0] = '\0';
  7.  
  8. sprintf( tmp, "%d ", time );
  9. strcat( buf, tmp );
  10.  
  11. return buf;
  12. }
  13.  
  14. int main( void )
  15. {
  16. int time[1];
  17.  
  18. int seconds = 153;
  19.  
  20. time[0] = seconds % 60; // time[0] will equal 153 - 60 - 60, this will go on
  21. // until there isn't 60 left to subtract, then return
  22. // what is left
  23. seconds = seconds - time[0]; // we have the seconds so remove it
  24. time[1] = ( seconds / 60 ) % 60; // we divide by 60 cause there is 60 secs in a min.
  25. // the same will happen here as above but seeing
  26. // the seconds was 120 (153 - 33) it will return 2
  27.  
  28. printf( "%s%s%s%s%s\n",
  29. time[1] > 0 ? print_time( time[1] ) : "", time[1] > 0 ? "minutes" : "",
  30. time[1] > 0 ? " and " : "",
  31. time[0], " seconds" );
  32.  
  33. return 0;
  34. }
Basically this will return x minutes and x seconds if both are bigger than 0 or just the seconds, it is very adaptable, now using 153 seconds it would return 2 minutes and 33 seconds.
Reputation Points: 10
Solved Threads: 1
Unverified User
Auron is offline Offline
11 posts
since May 2005
May 17th, 2005
0

Re: convert seconds to minutes, hours, days, months, years, etc.

(Yes, I know - see my post #7)
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: FIrst time with C- Issue
Next Thread in C Forum Timeline: Age calculation program in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC