| | |
convert seconds to minutes, hours, days, months, years, etc.
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
It's as simple as this, more or less:
C Syntax (Toggle Plain Text)
#include <iostream> int main() { double seconds = 1234567; double minutes = seconds / 60; double hours = minutes / 60; double days = hours / 24; double weeks = days / 7; double months = weeks / 52; double years = months / 12; std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield); std::cout.precision(3); std::cout << "Seconds: " << seconds << '\n' << "Minutes: " << minutes << '\n' << "Hours: " << hours << '\n' << "Days: " << days << '\n' << "Weeks: " << weeks << '\n' << "Months: " << months << '\n' << "Years: " << years << '\n'; }
Not hairy at all. If integral division results in 0, don't show it:
C Syntax (Toggle Plain Text)
#include <functional> #include <iostream> #include <string> template <typename Pred> void print_if(int part, Pred p, std::string msg = "") { if (p(part)) std::cout << msg << part << '\n'; } int main() { int seconds = 1234567; int minutes = seconds / 60; int hours = minutes / 60; int days = hours / 24; int weeks = days / 7; int months = weeks / 52; int years = months / 12; std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield); std::cout.precision(3); print_if(seconds, std::bind2nd(std::greater<int>(), 0), "Seconds: "); print_if(minutes, std::bind2nd(std::greater<int>(), 0), "Minutes: "); print_if(hours, std::bind2nd(std::greater<int>(), 0), "Hours: "); print_if(days, std::bind2nd(std::greater<int>(), 0), "Days: "); print_if(months, std::bind2nd(std::greater<int>(), 0), "Months: "); print_if(years, std::bind2nd(std::greater<int>(), 0), "Years: "); }
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:
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)
basically, if the minutes is above 60 I don't want to 61, etc.
This is how I ended up doing it:
C Syntax (Toggle Plain Text)
string Date::operator - ( const Date& b ) { double diff = difftime( rawtime, b.rawtime ); if ( diff < 0 ) diff*=-1; ostringstream o; if ( diff < 60 ) { o<<diff<<" SECOND(S)"; return o.str(); } if ( diff < 3600 ) { int min = (int)diff/60; int sec= (int)diff%60; o<<min<<" MINUTE(S), "<<sec<< " SECOND(S)"; } else if ( diff < 86400 ) /* DAY */ { int hours = (int) diff/3600; int hourRemainder = (int)diff%3600; int min = (int)hourRemainder/60; int sec= (int)diff%60; o<<hours<< " HOUR(S), "<< min<< " MINUTE(S), "<<sec<< " SECOND(S)"; } else if ( diff < 31536000 ) /* YEAR */ { int days = (int) diff/86400; int daysRemainder = (int)diff%86400; int hours = (int) daysRemainder/3600; int hourRemainder = (int)(diff - 86400)%3600; int min = (int)hourRemainder/60; int sec= (int)diff%60; o<<days<<" DAY(S), "<<hours<< " HOUR(S), "<< min<< " MINUTE(S), "<<sec<< " SECOND(S)"; } else { int years = (int) diff/31536000; int yearsRemainder = (int) diff%31536000; int days = (int) yearsRemainder/86400; int daysRemainder = (int)diff%86400; int hours = (int) daysRemainder/3600; int hourRemainder = (int)(diff - 86400)%3600; int min = (int)hourRemainder/60; int sec= (int)diff%60; o<<years<<" YEAR(S), "<<days<<" DAY(S), "<<hours<< " HOUR(S), "<< min<< " MINUTE(S), "<<sec<< " SECOND(S)"; } return o.str(); }
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)
Well have you guys ever heard of the MOD (%) function.
Well let's say the count was 153 seconds.
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.
Well let's say the count was 153 seconds.
C Syntax (Toggle Plain Text)
char *print_time( int time ) { static char buf[20]; char tmp[200]; buf[0] = '\0'; sprintf( tmp, "%d ", time ); strcat( buf, tmp ); return buf; } int main( void ) { int time[1]; int seconds = 153; time[0] = seconds % 60; // time[0] will equal 153 - 60 - 60, this will go on // until there isn't 60 left to subtract, then return // what is left seconds = seconds - time[0]; // we have the seconds so remove it time[1] = ( seconds / 60 ) % 60; // we divide by 60 cause there is 60 secs in a min. // the same will happen here as above but seeing // the seconds was 120 (153 - 33) it will return 2 printf( "%s%s%s%s%s\n", time[1] > 0 ? print_time( time[1] ) : "", time[1] > 0 ? "minutes" : "", time[1] > 0 ? " and " : "", time[0], " seconds" ); return 0; }
![]() |
Other Threads in the C Forum
- Previous Thread: interfacing systems of different makes
- Next Thread: Mapping Sonar Data with C
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming stack standard strchr string strings suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h





