Converting seconds to years, months etc...

• 08-06-2005
^xor
Converting seconds to years, months etc...
I have a double variable holding the number of seconds from the current time to when time_t wraps around (an exercise from the book Expert C Programming), and I need to convert it to years, months, weeks, days, hours and minutes.

How would you do such a thing when you need to take into account for leap years?
• 08-06-2005
Feite
1. Minutes: printf seconds divided by 60
2. Hours: printf minutes divided by 60
3. Days: printf hours divided by 24
4. Weeks: printf days divided by 7
5. Months: printf weeks divided by 30/31
6. Years: printf months divided by 12

:)
• 08-06-2005
^xor
I meant as a total. For example, 3701 seconds becomes 1 hour, 1 minute and 41 seconds.
• 08-06-2005
Feite
Ah i'm sorry.. My solution would be:
seconds - number of seconds in a year
till seconds is negative, then add the number of seconds in a year again.
Do the same for months weeks days hours and minutes.
• 08-06-2005
^xor
But the numbers of seconds in a year is not constant due to leap years. And the number of days in every month is different as well, so you need to keep check of what month it is.
• 08-06-2005
dwks
Because there are 365.24 days in a year, use that number instead of 365/366.

So, years = seconds/60/60/24/365.24.
• 08-06-2005
xddxogm3
Here is my idea on the problem.
I'm assuming you are trying to do something similar to the following.
33264120 seconds = 1 year 20 days 1 hrs 59 minutes ...
calculating the leap year portion is fairly easy.
here is some psuedo code for it.
Code:

```if (year mod 4 != 0) {use 28 for days in February} else if (year mod 400 == 0) {use 29 for days in February} else if (year mod 100 == 0) {use 28 for days in February} else {use 29 for days in February} //acquired from here```
in your case you are going from seconds to other time units, you would need to work backwards. gnu c library time_t starts at (seconds elapsed since 00:00:00 on January 1, 1970). so you need to find out if the next year is a leap year by using the pseudo code. if not subtract off non-leap year seconds from the total amount of seconds then tally up a year. do this for leap year seconds if the next year is leap year, then subtract leap year seconds. then keep going until you finish. this may take some processor time, but it would work. once you get finished with the years and have a left over seconds value the remainder is easy to calculate. i'm not versed enough with c to tell if there is a quicker and easier way. my way would almost be like brute force programming, but it would work. i'm sure a Cprogramming guru will step up and show you a better way, but i hope this helps in some way.
• 08-06-2005
^xor
Thanks, I'll give it a try. This is how it should be done (in real code):
Code:

`if (i % 400 == 0 || (i % 4 == 0 && i % 100 != 0))`
And dwks method is not accurate enough.

This is what I've got to work with by the way:
Code:

```/* How long from current time until time_t wraps around? */ #include <limits.h> #include <stdio.h> #include <time.h> int main(void) {         time_t end_of_time = LONG_MAX; /* Non-portable */         time_t now = time(NULL);         double diff = difftime(end_of_time, now);         return 0; }```