Next Tuesday – Solution

One way to discover next Tuesday’s date is just to wait. You could code that solution in C, which would be silly, but I hope that you instead chose to use some time functions to derive your answer.

The first step to discovering next Tuesday’s date is to obtain the current date: Use the time() function to fetch a time_t value.

Next, pass the current time, the time_t value, to the localtime() function. It returns a tm structure with details about the current date. Specifically you want the day of the week and day of the month, the tm structure’s tm_wday and tm_mday members, respectively.

The tm structure holds Tuesday as value 2 in the tm_wday member. The algorithm I use in my solution subtracts 2 (Tuesday) from the current tm_wday value. The result is the number of days until next Tuesday. If the result is negative, add 7 to the result to obtain the proper value.

Finally, you must compare the value returned (the number of days until next Tuesday) with the number of days in the current month. If next Tuesday’s date is greater than the number of days in the current month, you must subtract the number of days in the current month to obtain an accurate next-Tuesday value.

After all this calculating and scheming, the result is the day of the month for next Tuesday. The following code implements such insanity:

#include <stdio.h>
#include <time.h>

void ordinal(int d);

int main()
{
    time_t now;
    struct tm *today;
    int tuesday;
    char *days[] = {
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday"
    };
    /* this array assumes February to have 28 days */
    int months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    /* obtain the current time */
    time(&now);
    /* translate into date and time fields */
    today = localtime(&now);

    /* output today's day and date */
    printf("Today is %s the %d",
            days[today->tm_wday],
            today->tm_mday
          );
    ordinal(today->tm_mday);

    /* calculate next Tuesday */
        /* Tuesday is tm_mday value 2 */
    tuesday = (2 - today->tm_wday);
    if( tuesday < 1 )
        tuesday+=7+today->tm_mday;
        /* check for the end of the month */
    if( tuesday > months[today->tm_mon] )
        tuesday -= months[today->tm_mon];
        /* output the result */
    printf("Next Tuesday is the %d",tuesday);
    ordinal(tuesday);

    return(0);
}

/* handle ordinal output */
void ordinal(int d)
{
    if( d>10 && d<14 )
        puts("th");
    else
    {
        switch(d % 10 )
        {
            case 1:
                puts("st");
                break;
            case 2:
                puts("nd");
                break;
            case 3:
                puts("rd");
                break;
            default:
                puts("th");
        }
    }
}

The code works through the steps outlined earlier in this post. Here’s a sample run, which I ran on Saturday, August 31:

Today is Saturday the 31st
Next Tuesday is the 3rd

I hope your solution also yields the correct, “next Tuesday” value.

  • For more details on the ordinal() function, refer to this Exercise from 2013.
  • If you desire to implement the proper day count for February, refer to this Exercise Solution from 2018.

2 thoughts on “Next Tuesday – Solution

  1. “Finally, you must compare the value returned (the number of days until next Tuesday) with the number of days in the current month. If next Tuesday’s date is greater than the number of days in the current month, you must subtract the number of days in the current month to obtain an accurate next-Tuesday value.”

    I think if you add a number to tm_mday and then call mktime to get a time_t it will adjust the date by wrapping it round if it overruns the end of the month.

Leave a Reply