Thread: one Billion second Birthday!

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    41

    one Billion second Birthday!

    So I picked up a thred about an hour ago from the general dsicussions forum, about ideas for simple programs to keep the appetite going (which is exactly what i need right now. SO - i picked the one billionth birthday puzzle suggested by Matticus. here is what I have so far -

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
            int years = 1;
            int months = 12;
            int days = 365;
            int hours = 8760;
            int minutes = 525600;
            int seconds = 31536000;
            int billion = 1000000000;
            int doy = 1987;
            int dom = 5;
            int dod = 11;
            int cy = 2013;
            int cm = 9;
            int cd = 29;
            int agey = cy - doy;
            int agem = cm - dom;
            int aged = cd - dod;
            int age_in_secs = ((agey * seconds) + (agem * (seconds/12)) + (aged * (seconds/365)));
            printf("you are %i years, %i months and %i days old \n",agey,agem,aged);
            printf("you are %i seconds old \n",age_in_secs);
            int remains = billion - age_in_secs;
            printf("remains in secs = %i \n",remains);
    
            agey = (int)remains / seconds;
            int leftover = remains % seconds;
            printf("years left = %i \n",agey);
            printf("leftover = %i \n",leftover);
            return;
    }
    Not pretty,I know...efficient,I think not...However, just getting my head around the problem and so far I'm stuck on the part where Im using modulo to figure out the remaining seconds between the age of the person in seconds and a billion seconds - I get 5 years with a remainder of about 7 million seconds, need to get that 7million seconds into a month with remainder and so on... - Start nitpicking guys, I want this smarter and quicker by the end of the day!!

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    >and so on..
    This calls for a loop, doesn't it? However, I am not sure now if the logic is correct..
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I'm not sure how your age in years, months and days is correct when you don't account for days in months, leap years etc

    *puzzled*

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should check out some of the structures and functions in "time.h" - this will help take care of the minute details you'll encounter with accurate time-counting.

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Matticus View Post
    You should check out some of the structures and functions in "time.h" - this will help take care of the minute details you'll encounter with accurate time-counting.
    That's no challenge though :-)

  6. #6
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Quote Originally Posted by Matticus View Post
    You should check out some of the structures and functions in "time.h"
    Where would I find this accurately, I usually look at cplusplus.com for my c++ obviously, but what about for c?

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Here you are. Enjoy!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Quote Originally Posted by stach View Post
    I usually look at cplusplus.com for my c++ obviously, but what about for c?
    Did you miss this bit std?

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    When I first started and in the forum for IP, a master student provided a link to me from a C++ site, I had the same reaction as you.

    The functions of ctime are actually the ones of time.h, so I provided that link, because it is correct and it is good (in a matter of style, etc.).
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Thanks std. I usually look at most of those headers as compatable anyways, just try my best to understand the crazy world of professional programming!

  11. #11
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Thanks to Matticus I had a look at time.h and have reworked it a little to be this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    int main()
    {
    //      int years = 1;
    //      int months = 12;
    //      int weeks = 52;
    //      int days = 365;
    //      int hours = 8760;
            int seconds = 31536000; //this is how many seconds in a year
            int billion = 1000000000;
            int real_year, year, month ,day;
            time_t timer;
            struct tm y2k;
            double age_in_secs;
            double years_left;
            double months_left;
            double days_left;
            const char * c_month[] = { "January", "February", "March", "April",
                                       "May", "June", "July", "August", "September",
                                       "October","November", "December"};
            printf ("Enter year: "); scanf ("%d",&year);
            printf ("Enter month: "); scanf ("%d",&month);
            printf ("Enter day: "); scanf ("%d",&day);
                 if(year > 2000) real_year = year -2000;
            else if(year > 1900) real_year = year -1900;
                 if(month > 12) month = 12;
            else if(month < 1) month = 1;
                 if(day > 31) day = 31;
            else if(day < 1) day = 1;
            y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
            y2k.tm_year = real_year;  y2k.tm_mon = month; y2k.tm_mday = day;
            time(&timer);
    
            age_in_secs = difftime(timer,mktime(&y2k));
            printf ("%.f seconds since %s %i, %i \n", age_in_secs,c_month[month-1],day,year);
            int remains = billion - age_in_secs;
            years_left = remains / seconds;
            remains = remains % seconds;
            months_left = ((((remains/60)/60)/24)/30);
            printf ("%.f years and ", years_left);
            return 0;
    }
    Which i believe is getter closer to the point. What I believe I've done here at the end is to calculate how many seconds are left from now until a billion seconds given a persons birthdate. What I did then is devided that amount of seconds into seconds in a year to get how many years are left until a billion, with an amount of seconds left over - but this is where i'm dumbfounding myself, i cant for the life in me figure out what to do with these 'leftover' seconds. What I want to do is turn them into months and days, which i should be able to do since i did it to years - i think it comes down to my understading of modulo. can it be used in succession such as months_left = ((((remains%60)%60)%24)%30); or not?

  12. #12
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by stach View Post
    Where would I find this accurately
    At the Linux man-pages project.

    Section 3 includes the standard C library interfaces.

    I would not trust a C++ source, because the two are separate languages. Even if complete and reliable, it would still look at the functionality from a C++ viewpoint, and that will affect you and your code, whether you like it or not: we humans are social animals, and that's just how we are.

    While the Linux man-pages project is focused for Linux, it is deliberately written to help with portability to other systems. There are even translations to other languages.

    On each page, you can see the Conforming to section, and that tells you where the function was defined, and when and how you can expect to have it available.



    For the problem at hand -- in the direction Matticus gently tried to steer you towards, and what I'd personally warmly recommend -- consider this pseudo-code:
    1. Get user birthday date and time into a struct tm:
      1. Parse to numeric fields using the scanf() family of functions.
        You may have to parse month names or abbreviations (Jan, Feb, etc. in English) to numbers using e.g. strcmp().
      2. Fill in the year, month, day, hour, minute, and second of the birthday (expressed in local time) into a struct tm structure.
    2. Add a billion seconds to the datetime, by adding it into the tm_sec field of the structure.
      (You might wish to add (billion/3600) to tm_hour, ((billion % 3600)/60) to tm_min, and (billion % 60) to tm_sec, too, but it should not make a difference either way.)
    3. Call mktime() on the structure to normalize the fields.
    4. Print out the year, month, day, hour, minute, and second of the billionth-second birthday.


    The mktime() function is extremely useful for other date-time manipulations in general. When you call mktime(), it normalizes the fields in the struct tm structure.

    In other words, if you want to know what day is 9683 days after May 11, 2013, you can use
    Code:
    struct tm  t;
    t.tm_year = 2013 - 1900;   /* The value is years since 1900 */
    t.tm_mon = 5 - 1;   /* The value is months since January */
    t.tm_mday = 11;   /* The value is the day of month */
    
    /* For maximum resistance against daylight savings etc., I use noon as the time;
     * it's halfway through the specified date! */
    t.tm_hour = 12;
    t.tm_min = 0;
    t.tm_sec = 0;
    
    /* Add the specified duration, recklessly just adding to the fields!
    */
    t.tm_mday += 9683;
    
    /* Call mktime(). The result is the matching time_t, but you can ignore it if you don't need it for anything. */
    mktime(&t);
    
    printf("Year: %d\n", t.tm_year + 1900);
    printf("Month: %d\n", t.tm_mon + 1);
    printf("Day: %d\n", t.tm_mday);
    The result is, of course,
    Code:
    Year: 2039
    Month: 11
    Day: 14
    i.e. November 14, 2039.

    For such a large number of seconds, it is more common to take the time_t returned by mktime(), add the number of seconds to that, then convert the time_t back to a struct tm in local time using localtime(). You see, by definition, a time_t is the number of seconds since some Epoch.

    However, I recommend the struct tm -only approach.

    I'm sure you can see how powerful these two (time_t and struct tm) and localtime(), gmtime(), and mktime() are.
    Furthermore, they are standard C89 stuff!

    POSIX provides a further set of compatible and useful functions, like gettimeofday() and clock_gettime that use a structure that contains the current time as time_t, plus fractional seconds in a separate field; and strptime() to parse strings into a struct tm, and strftime() to format a datetime stored in a struct tm into a human-readable datetime, date, or time, in a locale-aware manner. There is even strcasecmp(), a locale-aware case-insensitive string comparison function.

    Unfortunately, I have no idea if Windows supports some/any of those additional POSIX features. Fortunately, you can do just about all date and time manipulations you can think of using the standard C89 stuff.
    Last edited by Nominal Animal; 08-29-2013 at 11:24 AM.

  13. #13
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Code:
    age_in_secs = difftime(timer,mktime(&y2k));
            int remains = billion - age_in_secs;
            struct tm  t;
            t.tm_year = 2013 - 1900;   /* The value is years since 1900 */
            t.tm_mon = month;   /* The value is months since January */
            t.tm_mday = day;   /* The value is the day of month */
            t.tm_hour = 12;
            t.tm_min = 0;
            t.tm_sec = 0;
            t.tm_sec += remains;
            mktime(&t);
            printf("\nDate of you 1 Billionth Brithday: %d %s %d ", t.tm_mday, c_month[t.tm_mon], t.tm_year + 1900);
    So effectively if I pass the remaining time in seconds of a persons life to that of the current seconds - this should now give me the date from today that is a persons billionth birthday? In my mind that works, but testing seems to produce queer results - such as incrementing by one day of someones birthdate, adds 2 days to the expected date of their billionth birthday - why is that?

  14. #14
    Registered User
    Join Date
    Jul 2013
    Posts
    41
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    int main()
    {
            int seconds = 31536000; //this is how many seconds in a year
            int billion = 1000000000;
            int real_year, year, month ,day;
            time_t timer;
            struct tm y2k;
            double age_in_secs;
            const char * c_month[] = { "January", "February", "March", "April",
                                       "May", "June", "July", "August", "September",
                                       "October","November", "December"};
    
            printf ("Enter year: "); scanf ("%d",&year);
            printf ("Enter month: "); scanf ("%d",&month);
            printf ("Enter day: "); scanf ("%d",&day);
    
                 if(year > 2000) real_year = year -2000;
            else if(year > 1900) real_year = year -1900;
                 if(month > 12) month = 12;
            else if(month < 1) month = 1;
                 if(day > 31) day = 31;
            else if(day < 1) day = 1;
    
            y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
            y2k.tm_year = real_year;  y2k.tm_mon = month; y2k.tm_mday = day;
    
            time(&timer);
    
            age_in_secs = difftime(timer,mktime(&y2k));
    
            int remains = billion - age_in_secs;
    
            struct tm  t;
            t.tm_year = 2013 - 1900;   /* The value is years since 1900 */
            t.tm_mon = month;   /* The value is months since January */
            t.tm_mday = day;   /* The value is the day of month */
    
            t.tm_hour = 12;
            t.tm_min = 0;
            t.tm_sec = 0;
            t.tm_sec += remains;
    
            mktime(&t);
    
            printf("\nDate of your 1 Billionth Brithday: %d %s %d ", t.tm_mday, c_month[t.tm_mon], t.tm_year + 1900);
    
            t.tm_year = 113;   
            t.tm_mon = 9;      /* Seemingly I could probably use timer to get these values right?*/
            t.tm_mday = 2;    
            t.tm_hour = 12;
            t.tm_sec = 0;
            t.tm_sec -= billion;
    
            mktime(&t);
    
            printf("\nIf you were born on: %d %s %d ", t.tm_mday, c_month[t.tm_mon], t.tm_year + 1900);
            printf("\nthen today is your Billionth Birthday!!!");
    
            return 0;
    }
    So if I did this to work 'backwards' if you like - this is the same premise - correct?

    Edit: Wait wait, I think i Figured it, it's because in the first use of t.tm_year/mon/mday etc. I've passed the persons birthday and not the current date - think thats it - just need to figure how to pass the curent date/time in mins/days/hours etc. now using some function from time.h

    Edit:Edit: Nope, don't got it, ifI enter my 'calculated billionth birthday' as the date to work back from, then the date that is produced from working backwards gives me 1 month later and one day less (maybe 30 days i think given the man pages on time.h) - so yeah, maybe 30 days later than my actual birthday
    Last edited by stach; 09-02-2013 at 08:44 AM.

  15. #15
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Consider the following C89 program:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    #include <locale.h>
    #include <errno.h>
    
    int diffdays(const struct tm *const after, const struct tm *const before)
    {
        struct tm  temp, target, source;
        time_t     ttarget, tsource;
        int        days;
    
        if (!after || !before) {
            errno = EINVAL;
            return 0;
        }
    
        /* Source date is before, at noon, DST unknown. */
        source.tm_year  = before->tm_year;
        source.tm_mon   = before->tm_mon;
        source.tm_mday  = before->tm_mday;
        source.tm_hour  = 12;
        source.tm_min   = 0;
        source.tm_sec   = 0;
        source.tm_isdst = -1;
    
        tsource = mktime(&source);
    
        /* Target date is after, at noon, DST unknown. */
        target.tm_year  = after->tm_year;
        target.tm_mon   = after->tm_mon;
        target.tm_mday  = after->tm_mday;
        target.tm_hour  = 12;
        target.tm_min   = 0;
        target.tm_sec   = 0;
        target.tm_isdst = -1;
    
        ttarget = mktime(&target);
    
        /* We estimate the day count initially by 24-hour, 60-minute, 60-second days. */
        days = difftime(ttarget, tsource) / 86400.0;
    
        while (1) {
            temp.tm_year  = source.tm_year;
            temp.tm_mon   = source.tm_mon;
            temp.tm_mday  = source.tm_mday + days;
            temp.tm_hour  = 12;
            temp.tm_min   = 0;
            temp.tm_sec   = 0;
            temp.tm_isdst = -1;
    
            mktime(&temp);
    
            /* The day count should not be off by more than a few days
             * (leap days and so on). If efficiency is desird, we could do
             * better calculations (see between() for example), but
             * single day increments an decrements is usually fine. */
            if (temp.tm_year > target.tm_year)
                days--;
            else
            if (temp.tm_year < target.tm_year)
                days++;
            else
            if (temp.tm_mon > target.tm_mon)
                days--;
            else
            if (temp.tm_mon < target.tm_mon)
                days++;
            else
            if (temp.tm_mday > target.tm_mday)
                days--;
            else
            if (temp.tm_mday < target.tm_mday)
                days++;
            else
                break;
        }
    
        errno = 0;
        return days;
    }
    
    int between(const struct tm *const after, const struct tm *const before,
                int *const yearsptr, int *const monthsptr, int *const daysptr)
    {
        struct tm  temp;
        int        years, months, days;
    
        if (!after || !before)
            return -1;
    
        years  = after->tm_year - before->tm_year;
        months = after->tm_mon  - before->tm_mon;
        days   = after->tm_mday - before->tm_mday;
    
        while (1) {
    
            /* Keep days to exact count if known, otherwise within 0 and 30, inclusive. */
            if (after->tm_mday >= before->tm_mday) {
                days = after->tm_mday - before->tm_mday;
            } else
            if (days < 1) {
                months--;
                days += 31;
            } else
            if (days > 31) {
                months++;
                days -= 31;
            }
    
            /* Keep months within 0 and 11, inclusive. */
            if (months < 0) {
                years--;
                months += 12;
            } else
            if (months > 11) {
                years++;
                months -= 12;
            }
    
            /* Calculate prior date based on elapsed years, months, days. */
            temp.tm_year  = after->tm_year - years;
            temp.tm_mon   = after->tm_mon  - months;
            temp.tm_mday  = after->tm_mday - days;
    
            /* At noon. DST unknown. */
            temp.tm_hour  = 12;
            temp.tm_min   = 0;
            temp.tm_sec   = 0;
            temp.tm_isdst = -1;
    
            mktime(&temp);
    
            /* Adjust days, if incorrect. */
            if (temp.tm_mday != before->tm_mday) {
                days += temp.tm_mday - before->tm_mday;
                continue;
            }
    
            /* Adjust months, if incorrect. */
            if (temp.tm_mon != before->tm_mon) {
                months += temp.tm_mon - before->tm_mon;
                continue;
            }
    
            /* Adjust years, if incorrect. */
            if (temp.tm_year != before->tm_year) {
                years += temp.tm_year - before->tm_year;
                continue;
            }
    
            /* Found! */
            if (yearsptr)  *yearsptr  = years;
            if (monthsptr) *monthsptr = months;
            if (daysptr)   *daysptr   = days;
    
            return 0;
        }
    }
    
    int main(int argc, char *argv[])
    {
        struct tm  birth, now, then;
        time_t     nowtime;
        int        y, m, d, value;
        char       dummy, suffix[10];
    
        setlocale(LC_ALL, "");
    
        if (argc != 3 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
            fprintf(stderr, "\n");
            fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
            fprintf(stderr, "       %s YYYY-MM-DD INTERVAL\n", argv[0]);
            fprintf(stderr, "       %s MM/DD/YYYY INTERVAL\n", argv[0]);
            fprintf(stderr, "Where INTERVAL is\n");
            fprintf(stderr, "       NUMBER s      seconds\n");
            fprintf(stderr, "       NUMBER mi     minutes\n");
            fprintf(stderr, "       NUMBER h      hours\n");
            fprintf(stderr, "       NUMBER d      days\n");
            fprintf(stderr, "       NUMBER w      weeks\n");
            fprintf(stderr, "       NUMBER mo     months\n");
            fprintf(stderr, "       NUMBER y      years\n");
            fprintf(stderr, "\n");
            return 1;
        }
    
        birth.tm_hour  = 12; /* Noon */
        birth.tm_min   =  0;
        birth.tm_sec   =  0;
        birth.tm_isdst = -1; /* Unknown whether DST or not. */
        birth.tm_wday  = -1; /* Irrelevant */
        birth.tm_yday  = -1; /* Irrelevant */
    
        if (sscanf(argv[1], "%d-%d-%d %c", &y, &m, &d, &dummy) == 3 ||
            sscanf(argv[1], "%d/%d/%d %c", &m, &d, &y, &dummy) == 3) {
    
            if (y < 1900) {
                fprintf(stderr, "%s: Invalid year.\n", argv[1]);
                return 1;
            }
    
            if (m < 1 || m > 12) {
                fprintf(stderr, "%s: Invalid month.\n", argv[1]);
                return 1;
            }
    
            if (d < 1 || d > 31) {
                fprintf(stderr, "%s: Invalid day.\n", argv[1]);
                return 1;
            }
    
            birth.tm_year = y - 1900;
            birth.tm_mon = m - 1;
            birth.tm_mday = d;
    
        } else {
            fprintf(stderr, "%s: Cannot parse birth date.\n", argv[1]);
            return 1;
        }
    
        mktime(&birth);
    
        printf("Birth date:  %04d-%02d-%02d\n", birth.tm_year + 1900, birth.tm_mon + 1, birth.tm_mday);
    
        if (sscanf(argv[2], "%d %9s", &value, suffix) != 2) {
            fprintf(stderr, "%s: Invalid interval.\n", argv[2]);
            return 1;
        }
    
        then = birth;
    
        if (suffix[0] == 'Y' || suffix[0] == 'y')
            then.tm_year += value;
        else
        if ((suffix[0] == 'M' || suffix[0] == 'm') && (suffix[1] == 'O' || suffix[1] == 'o'))
            then.tm_mon += value;
        else
        if (suffix[0] == 'W' || suffix[0] == 'w')
            then.tm_mday += value * 7;
        else
        if (suffix[0] == 'D' || suffix[0] == 'd')
            then.tm_mday += value;
        else
        if (suffix[0] == 'H' || suffix[0] == 'h')
            then.tm_hour += value;
        else
        if ((suffix[0] == 'M' || suffix[0] == 'm') && (suffix[1] == 'I' || suffix[1] == 'i'))
            then.tm_min += value;
        else
        if (suffix[0] == 'S' || suffix[0] == 's')
            then.tm_sec += value;
        else {
            fprintf(stderr, "%s: Invalid interval; unknown suffix '%s'.\n", argv[2], suffix);
            return 1;
        }
    
        mktime(&then);
    
        nowtime = time(NULL);
        now = *(localtime(&nowtime));
    
        printf("Todays date: %04d-%02d-%02d\n", now.tm_year + 1900, now.tm_mon + 1, now.tm_mday);
    
        value = diffdays(&now, &birth);
        if (value < 0)
            printf("You will be born in %d days.\n", value);
        else
        if (value > 0)
            printf("You are %d days old today.\n", value);
        else
            printf("Today is your birthday.\n");
    
        if ((then.tm_year > now.tm_year) ||
            (then.tm_year == now.tm_year && then.tm_mon > now.tm_mon) ||
            (then.tm_year == now.tm_year && then.tm_mon == now.tm_mon && then.tm_mday > now.tm_mday)) {
    
            if (between(&then, &now, &y, &m, &d)) {
                fprintf(stderr, "BUG in between().\n");
                return 1;
            }
    
            printf("Target date: %04d-%02d-%02d, in ", then.tm_year + 1900, then.tm_mon + 1, then.tm_mday);
            if (y > 0)
                printf("%d years, %d months, %d weeks, and %d days.\n", y, m, d / 7, d % 7);
            else
            if (m > 0)
                printf("%d months, %d weeks, and %d days.\n", m, d / 7, d % 7);
            else
            if (d > 0)
                printf("%d weeks and %d days.\n", d / 7, d % 7);
            else
                printf("%d days.\n", d);
            fflush(stdout);
    
        } else
        if (then.tm_year == now.tm_year &&
            then.tm_mon == now.tm_mon &&
            then.tm_mday == now.tm_mday) {
    
            printf("Target date: %04d-%02d-%02d, today!\n", then.tm_year + 1900, then.tm_mon + 1, then.tm_mday);
            fflush(stdout);
    
        } else {
    
            if (between(&now, &then, &y, &m, &d)) {
                fprintf(stderr, "BUG in between().\n");
                return 1;
            }
    
            printf("Target date: %04d-%02d-%02d, was ", then.tm_year + 1900, then.tm_mon + 1, then.tm_mday);
            if (y > 0)
                printf("%d years, %d months, %d weeks, and %d days ago.\n", y, m, d / 7, d % 7);
            else
            if (m > 0)
                printf("%d months, %d weeks, and %d days ago.\n", m, d / 7, d % 7);
            else
            if (d > 7)
                printf("%d weeks and %d days ago.\n", d / 7, d % 7);
            else
                printf("%d days ago.\n", d);
            fflush(stdout);
        }
    
        return 0;
    }
    If you compile and run it without parameters, it outputs usage to standard error.

    For example:
    Code:
    ./birthday 1976-05-11 15000d
    yields my fifteenth-thousand birthday:
    Code:
    Birth date:  1976-05-11
    Todays date: 2013-09-03
    You are 13629 days old today.
    Target date: 2017-06-05, in 3 years, 9 months, 0 weeks, and 2 days.
    The above agrees with e.g. LibreOffice Calc, as do a few other random dates and intervals I tried.

    Questions?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help: How to write a prime number greater than one billion
    By kenryuakuma in forum C++ Programming
    Replies: 6
    Last Post: 12-19-2008, 08:55 AM
  2. how to calculate billion number
    By mrmamon in forum C++ Programming
    Replies: 9
    Last Post: 10-16-2005, 06:34 AM
  3. My birthday!
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 07-10-2003, 05:16 AM
  4. one billion
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-30-2002, 08:01 PM
  5. Happy Billion UNIX!
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-09-2001, 10:11 AM