I found an interesting assignment in a C/C++ programming book I have. It was about converting seconds to minutes, and another about converting minutes/seconds to seconds. I decided to take that on, pushing it all the way to years. What this has actually become is an exercise in using modulo and division well.
The algorithm begins with calculating years, with each successive calculation containing all of the previous ones, and then changing the last operation from division to modulo. You have to do that in order to access the remainder left from the previous calculation. It’s more simple than it sounds. You only need to know ahead of time how many seconds are in a year, month, week, day, hour, and minute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | // ================================================== // Programmer: Jonathan Landrum // Date: 20 February 2012 // ================================================== // Program: timeConverter.cpp // Purpose: Converts seconds input (integer) to // weeks, days, hours, minutes (etc.) // Assumptions: 1.) Input is integer form. Floating // point input will be rounded down // to the nearest integer. // ================================================== #include <iostream> using namespace std; // -------------------------------------------------- // main(): // -------------------------------------------------- int main () { // Variables int years = 0; int months = 0; // 30 days int weeks = 0; int days = 0; int hours = 0; int minutes = 0; int seconds = 0; unsigned long int input; // Introduce the program cout << endl; cout << "----------------------------" << endl; cout << "- Time Converter -" << endl; cout << "----------------------------" << endl; cout << endl; cout << "How many seconds to convert? "; cin >> input; // Main processing loop years = input / 31536000; months = (input % 31536000) / 2592000; weeks = ((input % 31536000) % 2592000) / 604800; days = (((input % 31536000) % 2592000) % 604800) / 86400; hours = ((((input % 31536000) % 2592000) % 604800) % 86400) / 3600; minutes = (((((input % 31536000) % 2592000) % 604800) % 86400) % 3600) / 60; seconds = ((((((input % 31536000) % 2592000) % 604800) % 86400) % 3600) % 60); // Return the results cout << "There are "; if (years != 0) cout << years << " years "; if (months != 0) cout << months << " months "; if (weeks != 0) cout << weeks << " weeks "; if (days != 0) cout << days << " days "; if (hours != 0) cout << hours << " hours "; if (minutes != 0) cout << minutes << " minutes "; if (seconds != 0) { if (input > 60) cout << "and " << seconds << " seconds "; else cout << seconds << " seconds "; } // End if. Will be rather uninpressive for input < 60. cout << "in " << input << " seconds." << endl; cout << endl; cout << "\\\\//_ Live long and prosper." << endl; cout << endl; return (0); } // End main |
Here’s a sample output from the program:

I’ve checked the output numerous times and it’s correct. The only thing it doesn’t do is Leap Year (as in calculating the date based on the Unix Epoch, for example), so it will return a higher number for days, weeks, etc. on those years.
Making correct Leap Year output could be done, but it would require inputting a starting year and calculating from there, and that’s not what this program does. It’s not a static time, for instance. It’s a general time, as in “how many days are in x seconds”. But the algorithm can be adapted for use in a time-since program. Perhaps I will write that one next.
~Jonathan



