Thread: Converting seconds to years, months etc...

  1. #1
    FOX
    Join Date
    May 2005
    Posts
    188

    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?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    HOLLAND
    Posts
    10
    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


  3. #3
    FOX
    Join Date
    May 2005
    Posts
    188
    I meant as a total. For example, 3701 seconds becomes 1 hour, 1 minute and 41 seconds.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    HOLLAND
    Posts
    10
    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.

  5. #5
    FOX
    Join Date
    May 2005
    Posts
    188
    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.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Because there are 365.24 days in a year, use that number instead of 365/366.

    So, years = seconds/60/60/24/365.24.
    Last edited by dwks; 08-06-2005 at 09:14 PM. Reason: Changing * to /
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    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.
    Last edited by xviddivxoggmp3; 08-06-2005 at 11:31 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  8. #8
    FOX
    Join Date
    May 2005
    Posts
    188
    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;
    }
    Last edited by ^xor; 08-06-2005 at 11:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting localtime seconds into float
    By superpickle in forum C Programming
    Replies: 2
    Last Post: 05-12-2007, 05:09 PM
  2. Converting seconds to hours in decimal form?
    By ajmcello in forum C Programming
    Replies: 3
    Last Post: 04-24-2006, 02:35 AM
  3. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 08:08 PM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. Counting days
    By TheShaggmeister in forum C++ Programming
    Replies: 5
    Last Post: 07-17-2003, 10:29 AM