date comparison

G

Gernot Frisch

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
 
K

Karl Heinz Buchegger

Gernot said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top