Thread: Formatting Dates

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    93

    Formatting Dates

    Hi,

    I have to alter the format of a date in a string and have written this...

    Code:
    char process_date(FILE ** p_fp, char c)
    {
    char format[11];
    int index = 0;
    
          /* grab each character from the field */
          if(c != ';')
          {
             index = 0;
             *(format) = NULL;
             while((c != ' '))
             {
                format[index] = c;
                index++;
                c = getc(p_fp[OUT_FILE]);
             }
    
             /* nullify the string */
             *(format + index) = NULL;
    
             /* there are four types of date that can be received
              * in the input files
              * 1/1/2003 00:00:00
              * 1/11/2003 00:00:00
              * 11/1/2003 00:00:00
              * 11/11/2003 00:00:00
              * and the finished item should look like this
              * 11/11/2003
              * this is dealt with as follows
              */
             if(format[1] == '/')
             {
                putc('0', p_fp[FIN_FILE]);
                putc(format[0], p_fp[FIN_FILE]);
                putc(format[1], p_fp[FIN_FILE]);
    
                if(format[3] == '/')
                {
                   putc('0', p_fp[FIN_FILE]);
                   putc(format[2], p_fp[FIN_FILE]);
                   putc(format[3], p_fp[FIN_FILE]);
                   putc(format[4], p_fp[FIN_FILE]);
                   putc(format[5], p_fp[FIN_FILE]);
                   putc(format[6], p_fp[FIN_FILE]);
                   putc(format[7], p_fp[FIN_FILE]);
                }
                else
                {
                   putc(format[2], p_fp[FIN_FILE]);
                   putc(format[3], p_fp[FIN_FILE]);
                   putc(format[4], p_fp[FIN_FILE]);
                   putc(format[5], p_fp[FIN_FILE]);
                   putc(format[6], p_fp[FIN_FILE]);
                   putc(format[7], p_fp[FIN_FILE]);
                   putc(format[8], p_fp[FIN_FILE]);
                }
             }
             else if(format[2] == '/' && format[4] == '/')
             {
                putc(format[0], p_fp[FIN_FILE]);
                putc(format[1], p_fp[FIN_FILE]);
                putc(format[2], p_fp[FIN_FILE]);
                putc('0', p_fp[FIN_FILE]);
                putc(format[3], p_fp[FIN_FILE]);
                putc(format[4], p_fp[FIN_FILE]);
                putc(format[5], p_fp[FIN_FILE]);
                putc(format[6], p_fp[FIN_FILE]);
                putc(format[7], p_fp[FIN_FILE]);
                putc(format[8], p_fp[FIN_FILE]);
             }
             else
             {
                putc(format[0], p_fp[FIN_FILE]);
                putc(format[1], p_fp[FIN_FILE]);
                putc(format[2], p_fp[FIN_FILE]);
                putc(format[3], p_fp[FIN_FILE]);
                putc(format[4], p_fp[FIN_FILE]);
                putc(format[5], p_fp[FIN_FILE]);
                putc(format[6], p_fp[FIN_FILE]);
                putc(format[7], p_fp[FIN_FILE]);
                putc(format[8], p_fp[FIN_FILE]);
                putc(format[9], p_fp[FIN_FILE]);
             }
    
             /* remove the time component */
             c = getc(p_fp[OUT_FILE]);
             c = getc(p_fp[OUT_FILE]);
             c = getc(p_fp[OUT_FILE]);
             c = getc(p_fp[OUT_FILE]);
             c = getc(p_fp[OUT_FILE]);
             c = getc(p_fp[OUT_FILE]);
             c = getc(p_fp[OUT_FILE]);
             c = getc(p_fp[OUT_FILE]);
    
             /* remove the semi-colon */
             c = getc(p_fp[OUT_FILE]);
    
             /* put the last character back */
             putc(c, p_fp[FIN_FILE]);
             c = getc(p_fp[OUT_FILE]);
          }
          else /* date is null */
          {
             putc(c, p_fp[FIN_FILE]);
             c = getc(p_fp[OUT_FILE]);
          }
          return(c);
    }
    it is working without error and reasonably quickly, but I was wondering if you would do it differently,

    comments welcome,


    tia,

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Can you explain what it's supposed to do, showing some examples of input/output too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    sure



    before
    <more here>;P;LL;LN;0.00;2.40;;30/9/1998 00:00:00;;;;213.16;<more here>
    <more here>;P;LL;LN;0.00;9.60;;17/6/1997 00:00:00;;;;303.29;<more here>
    <more here>;P;LL;LN;0.00;9.60;;3/7/1997 00:00:00;;;;213.16;<more here>
    <more here>;P;LL;LN;0.00;2.40;;19/10/2000 00:00:00;;;;213.16;<more here>


    after
    <more here>;P;LL;LN;0.00;2.40;;30/09/1998;;;;213.16;<more here>
    <more here>;P;LL;LN;0.00;9.60;;17/06/1997;;;;303.29;<more here>
    <more here>;P;LL;LN;0.00;9.60;;03/07/1997;;;;213.16;<more here>
    <more here>;P;LL;LN;0.00;2.40;;19/10/2000;;;;213.16;<more here>


    the specific delimiter before a date is found and that point is then sent to the function where all the characters (before the next delimiter is found) are grabbed and then sorted

    I only found four types of possible date
    1/1/2003 00:00:00
    1/11/2003 00:00:00
    11/1/2003 00:00:00
    11/11/2003 00:00:00


  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >but I was wondering if you would do it differently

    I might build a string out of the date/time field, then use sscanf to parse the month, day, year, hour, minute and second from the string. And then [f]printf in the desired format. That is, after the string has been built, doing something similar to this.
    Code:
    char buffer[20];
    int month, day, year, hour, minute, second;
    /* [Build string] */
    if ( sscanf(buffer, "%d/%d/%d %d:%d:%d", &day, &month, &year,
                &hour, &minute, &second) == 6 )
    {
       fprintf(stdout, "%02d/%02d/%04d", day, month, year);
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    thanks Dave,

    that knocks mine into a cocked hat

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating Dates! Compiles but does not work...
    By ottomated in forum C Programming
    Replies: 11
    Last Post: 04-22-2008, 03:58 AM
  2. Getting the difference between dates
    By Leftos in forum C Programming
    Replies: 7
    Last Post: 01-20-2008, 12:49 AM
  3. number of days between 2 dates.
    By explosive in forum C++ Programming
    Replies: 10
    Last Post: 02-17-2005, 07:30 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  5. Formatting problem with dates.
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2002, 09:35 AM