473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

reading date and time

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

Feb 1 '06 #1
6 4909


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

Feb 1 '06 #2
In article <11**********************@z14g2000cwz.googlegroups .com>,
<le******@gmail.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
Feb 1 '06 #3
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


le******@gmail.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.

Thanks


Feb 1 '06 #4
"Shastri" <sa*****@gmail.com> 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) ks***@mib.org <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.
Feb 1 '06 #5
Keith Thompson wrote:
"Shastri" <sa*****@gmail.com> 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

Feb 2 '06 #6
"Robert Gamble" <rg*******@gmail.com> writes:
Keith Thompson wrote:
"Shastri" <sa*****@gmail.com> 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) ks***@mib.org <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.
Feb 2 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ekong, Samuel Akpan | last post by:
Hi, I am parsing a binary file and intend to read a date(not time) field encoded thus: 0xA1290B(little-endian). Now I know the date to be 12/04/2003 but just cannot get any of the known datetime...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
9
by: Mike Reed | last post by:
I must be having a "senile" day! I cannot recall, nor get to work, code to read a cookie's expiration date/time in an ASP page/VBScript. What am I missing? *** Sent via Developersdex...
4
by: J.D. | last post by:
Hello, I am working on a client project and they have a connection to a catalog system that passes data back and forth as xml over http The format of the data is as follow(s) > <?xml...
1
by: Justin Fancy | last post by:
Hi everyone, I have a textfile which I need to read and compare dates. The text file summarizes every time I do an update to an internet site. Sample output is as follows: Copying...
9
by: pob | last post by:
I currently have a procedure that loops thru a recordset to determine what files need to be loaded to my database. The naming convention of the files has always been accounts.txt, namelist.txt,...
3
by: Bharathi | last post by:
Hi, I got strucked with reading date value from excel file using C#.NET. For Jan-2000 the value I am getting is 36526.0. For all other dates also I am getting some double value like this. ...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
1
by: ndedhia1 | last post by:
I was reading in a log file like this that had no milliseconds: QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 601650761 block size QuoteBlockTiming...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.