le******@gmail.com wrote On 02/01/06 16:31,:
I was wondering if there is any way in C to read the date and time
(either system time or from the keyboard) and see if it falls between
certain date and time? I am not sure how to compare the 2 entries of
date/time. Any help on this would be great.
You can get the current time as a time_t value by
calling the time() function.
You can construct the time_t value for a given
date and time by inserting values in a struct tm and
calling mktime().
You can compare two time_t values by passing them
to difftime() and checking the sign of the result.
Error-checking omitted:
time_t early, later, now;
struct tm when;
/* Get the early time: 2006-Mar-01 12:34:56 (local) */
when.tm_year = 2006 - 1900; /* years since 1900 */
when.tm_mon = 3 - 1; /* months since January */
when.tm_mday = 1;
when.tm_hour = 12;
when.tm_min = 34;
when.tm_sec = 56;
when.tm_isdst = -1; /* DST status unknown */
early = mktime(&when);
/* Get the later time: 2006-May-07 08:09:10 (local) */
when.tm_year = 2006 - 1900;
when.tm_mon = 5 - 1;
when.tm_mday = 7;
when.tm_hour = 8;
when.tm_min = 9;
when.tm_sec = 10;
when.tm_isdst = -1;
later = mktime(&when);
now = time(NULL);
if (difftime(now, early) < 0)
printf ("Earlier than early\n");
else if (difftime(now, later) > 0)
printf ("Later than late\n");
else
printf ("Now is the time for all good parties\n"
"to come to the aid of Man.\n");
--
Er*********@sun.com