Coding a Time Conversion Calculator

Could somebody give an example of how to optimally code a time conversion calculator for seconds to x Years, x Days, x Hours, x Minutes, x Seconds format? For example it should give output like:

1
2
3
  int Userinput = 988977;
  [couts the following once calculations are done]
  988977 seconds = 0 Years, 11 Days, 10 Hours, 42 Minutes, 57 Seconds


This will be using long long variables instead of int because int can not store a large enough number in seconds to show up as years when converted. Pseudocode for this would be greatly appreciated!
Last edited on
This is a math implementation problem. How many seconds are in a minute, hour, day, week, month, year?

divide, mod, divide, mod, divide,...,mod
That is a pretty vague concept you are saying. Please provide detailed pseudocode that gives a template for how to code this program. This program is not as simple as dividing the total number of seconds down for each unit of time, as I have tried that.
you think so huh?

1
2
3
4
minutes = seconds * 60;
hours = minutes * 60;
days = (hours/24);
years = (days/365);

do you want code to do it or you want to figure it out?
FINALLY! I figured it out! Now I need help optimizing this code, as I feel there is a simpler way to code this program with less lines than what I have. This is the source:

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

long long totalSeconds;
//constants
const int DAYS = 365; //does not account for leap years
const int HOURS = 24;
const int MINUTES = 60;
const int SECONDS = 60;

//class declaration
class UserSeconds
{
private:
	long long TotalSeconds;
	long long Years;
	long long Days;
	long long Hours;
	long long Minutes;
	long long Seconds;
public:
	UserSeconds();
	//Notes for how I did the conversions are written down on paper.
	//It was quite long. ph stands for place holder basically.
	void Convert(long long totalSeconds)
	{
		TotalSeconds = totalSeconds;
		long long phDays, phHours, phHours2, phMinutes, phMinutes2, phMinutes3, phSeconds, phSeconds2, phSeconds3, phSeconds4;
		//QUICK NOTES ON CALCULATIONS
		//Each ph without numbers (ph1) converts Years to the unit of time being calculated
		//Each ph2 finds the userInput converted to that unit of time, then subtracts ph1 from it
		//Each ph3 subtracts Days converted to that unit of time from ph2
		//Each ph4 subtracts Hours converted to that unit of time from ph3


		//This finds the number of years
		Years = TotalSeconds / (DAYS*HOURS*MINUTES*SECONDS);

		//This finds the number of days
		phDays = Years*DAYS;
		Days = (TotalSeconds / (HOURS*MINUTES*SECONDS)) - phDays;

		//this finds the number of hours
		phHours = Years*DAYS*HOURS;
		phHours2 = (TotalSeconds / (MINUTES*SECONDS)) - phHours;
		Hours = phHours2 - Days*HOURS;

		//this finds the number of minutes
		phMinutes = Years*DAYS*HOURS*MINUTES;
		phMinutes2 = (TotalSeconds / SECONDS) - phMinutes;
		phMinutes3 = phMinutes2 - Days*HOURS*MINUTES;
		Minutes = phMinutes3 - Hours*MINUTES;

		//this finds the number of seconds
		phSeconds = Years*DAYS*HOURS*MINUTES*SECONDS;
		phSeconds2 = TotalSeconds - phSeconds;
		phSeconds3 = phSeconds2 - Days*HOURS*MINUTES*SECONDS;
		phSeconds4 = phSeconds3 - Hours*MINUTES*SECONDS;
		Seconds = phSeconds4 - Minutes*SECONDS;
	};
};
*bump*
Topic archived. No new replies allowed.