Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > reading date and time

Reply
Thread Tools

reading date and time

 
 
leorulez@gmail.com
Guest
Posts: n/a
 
      02-01-2006
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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-01-2006


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

--




 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-01-2006
In article < .com>,
<> wrote:
>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
--
All is vanity. -- Ecclesiastes
 
Reply With Quote
 
Shastri
Guest
Posts: n/a
 
      02-01-2006
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




wrote:
> 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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-01-2006
"Shastri" <> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      02-02-2006
Keith Thompson wrote:
> "Shastri" <> writes:
> > 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-02-2006
"Robert Gamble" <> writes:
> Keith Thompson wrote:
>> "Shastri" <> writes:

[...]
>> > printf ( "Current date and time are: %s", ctime(&rawtime));

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

>
> The string formed by ctime contains a newline.


So it does. Thanks.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
w3.org suggestion .. page, date, time and topic, date, time code (wish list). Keith Cochrane HTML 2 08-06-2006 06:57 AM
covert time from date Hour min sec format to epoch time i.e time since 1 jan 1970 in C Summu82 C Programming 5 06-07-2006 02:51 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Date & Time chooser for java 1.1 - using only the mouse to select time & date Chris Berg Java 0 10-27-2003 10:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57