1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Convert three integers to MM/DD/YYYY format

Discussion in 'C' started by LaMouche, Sep 7, 2010.

  1. LaMouche

    LaMouche New Member

    Joined:
    Sep 7, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello, I'm working on a project in C to practice before I take a class this fall that requires C. I decided on making a task list. First, I'm starting with a cli interface, and then I hope to move it to GTK+ since that would look better and I have PyGTK experience.

    Anyways, I just finished a function that formats the due date for a task. I want it in MM/DD/YYYY format so the date strings are all the same width (for aesthetics).

    So I have a date struct like this:
    Code:
    typedef struct date
    {
      int month;
      int day;
      int year;
    } date;
    
    I have written a date_to_string() function that takes a pointer to a date struct and returns a char pointer to a string of the date in MM/DD/YYYY format. I'm a bit rusty with char arrays/pointers, so I'm not sure if this is a good way to do it.
    Code:
     /* Convert a date to a string in the format MM/DD/YYYY */
    char *date_to_string_std(date *d)
    {
        char *date_string = (char *) malloc(11*sizeof(char)); /* 11 characters for MM/DD/YYYY + \0 */
        char month_string[3];
        char day_string[3];
    
        /* Put month int and day int into char arrays of length 3*/
        sprintf(month_string, "%d", d->month);
        sprintf(day_string, "%d", d->day);
        
        /* If the month < 10, add a 0 in front of the month number
         * to fit MM format */
        if (strlen(month_string) == 1)
        {
            month_string[2] = '\0';
            month_string[1] = month_string[0];
            month_string[0] = '0';
        }
        
        /* If the day < 10, add a 0 in front of the day number
         * to fit DD format */
        if (strlen(day_string) == 1)
        {
            day_string[2] = '\0';
            day_string[1] = day_string[0];
            day_string[0] = '0';
        }
    
        /* Create a string of the date in MM/DD/YYYY format */
        sprintf(date_string, "%s/%s/%d", month_string, day_string, d->year);
    
        return date_string;    
    }
    I would really appreciate any feedback about how I implemented this function. One problem is that I don't free the malloc'd memory given to date_string. But I can't add a free(date_string); before the end of the function because the function returns that value. It seems like I should have a local variable for date_string but I'm not sure how that works with returning a local variable.

    Also, is there a better way to add the '0' character in front of a month or day number that is only one digit?

    Thanks for any suggestions. I'm enjoying figuring things out in C. Python makes it way too easy. :P
     

Share This Page