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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

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

 
0
  #1
May 15th, 2005
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..
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

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

 
0
  #2
May 15th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

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

 
0
  #3
May 15th, 2005
Got anything handy?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

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

 
0
  #4
May 15th, 2005
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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

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

 
0
  #5
May 15th, 2005
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..
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

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

 
0
  #6
May 16th, 2005
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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

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

 
0
  #7
May 16th, 2005
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)
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

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

 
0
  #8
May 16th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 11
Reputation: Auron is an unknown quantity at this point 
Solved Threads: 1
Auron's Avatar
Auron Auron is offline Offline
Unverified User

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

 
0
  #9
May 17th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

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

 
0
  #10
May 17th, 2005
(Yes, I know - see my post #7)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC