date comparison

Discussion in 'C++' started by Gernot Frisch, Feb 28, 2005.

  1. bool isOlderThanToday(const char* pDate)
    {
    // How to do this?
    }


    void main()
    {
    const char* pDate = "29-Feb-2005";
    if (isOlderThanToday(pDate))
    printf ("older!");
    }


    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
     
    Gernot Frisch, Feb 28, 2005
    #1
    1. Advertising

  2. Gernot Frisch wrote:
    >
    > bool isOlderThanToday(const char* pDate)
    > {
    > // How to do this?
    > }
    >
    > void main()
    > {
    > const char* pDate = "29-Feb-2005";
    > if (isOlderThanToday(pDate))
    > printf ("older!");
    > }
    >


    eg.
    (C solution, error handling ommitted)


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

    int TextToNum( const char* Month )
    {
    char* AllMonth[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

    for( int i = 0; i < 12; ++i )
    if( strcmp( Month, AllMonth ) == 0 )
    return i;

    return -1;
    }

    time_t ParseDate( const char* pDate )
    {
    char MonthAsString[4];
    int Day;
    int Year;
    time_t Date;
    tm NewDate;

    sscanf( pDate, "%d-%3c-%d", &Day, MonthAsString, &Year );
    MonthAsString[3] = '\0';

    printf( "%d : %s : %d\n", Day, MonthAsString, Year );

    memset( &NewDate, 0, sizeof( NewDate ) );
    NewDate.tm_mday = Day;
    NewDate.tm_mon = TextToNum( MonthAsString );
    NewDate.tm_year = Year - 1900;

    Date = mktime( &NewDate );
    return Date;
    }


    bool isOlderThanToday( const char* pDate )
    {
    time_t today;
    time( &today );

    time_t Date = ParseDate( pDate );
    return difftime( Date, today ) < 0.0;
    }


    int main()
    {

    const char* pDate = "27-Feb-2005";
    if( isOlderThanToday( pDate ) )
    printf( "older!\n" );
    else
    printf( "In the future\n" );

    return 0;
    }

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Feb 28, 2005
    #2
    1. Advertising

  3. sscanf! Thank you very much.
    -Gernot
     
    Gernot Frisch, Feb 28, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    646
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,366
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    566
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    755
    Richard Heathfield
    Nov 8, 2003
  5. Deepu
    Replies:
    1
    Views:
    266
    ccc31807
    Feb 7, 2011
Loading...

Share This Page