reading date and time

L

leorulez

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.

Thanks
 
E

Eric Sosman

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");
 
W

Walter Roberson

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.

time_t currenttime = time();
if (currenttime != (time_t)-1) {
struct tm *tf = localtime(currenttime);
/* now examine tf->tm_year tm_mon tm_hour tm_min tm_sec,
taking into account that tm_year is relative to 1900
and tm_mon is 0 for january */
}


Be careful, though, to take into account the timezone and the
"daylight savings time". You need to precisely define what it means
for a particular time to fall between two other times considering
these factors. Suppose for example that a reference time falls
within the hour that daylight time is changing -- there might not
*be* a 01:15 on a particular day, so what do you do if you
are given that as a reference time? Or there might be -two- 01:15's
on a day about 6 months later.

Oh yes, and also watch out for Feb 29th ;-)
 
S

Shastri

Hi,

you can get the time using time_t structure included in time.h. Here is
code to read the current time from your system.

#include <stdio.h>
#include <time.h>
#include<conio.h>
int main ()
{
time_t rawtime;
time ( &rawtime );
printf ( "Current date and time are: %s", ctime(&rawtime));
getch();
return 0;
}

Cheers
Shastri
 
K

Keith Thompson

Shastri said:
you can get the time using time_t structure included in time.h. Here is
code to read the current time from your system.

#include <stdio.h>
#include <time.h>
#include<conio.h>
int main ()
{
time_t rawtime;
time ( &rawtime );
printf ( "Current date and time are: %s", ctime(&rawtime));
getch();
return 0;
}

Please don't top-post. Your response goes below, or interspersed
with, any quoted text.

<conio.h> is not a standard C function, and getch() is not a standard
C function.

Your output should be terminated with a '\n'; otherwise it's not
guaranteed to appear.
 
R

Robert Gamble

Keith said:
Shastri said:
you can get the time using time_t structure included in time.h. Here is
code to read the current time from your system.

#include <stdio.h>
#include <time.h>
#include<conio.h>
int main ()
{
time_t rawtime;
time ( &rawtime );
printf ( "Current date and time are: %s", ctime(&rawtime));
getch();
return 0;
}
[snip]

Your output should be terminated with a '\n'; otherwise it's not
guaranteed to appear.

The string formed by ctime contains a newline.

Robert Gamble
 

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

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,298
Latest member
ZenLeafCBDSupplement

Latest Threads

Top