Hi All!
I want to know how to display the system time without
using--system("date").
can anyone give me the idea to handle this operation.
By
S.S.G
|
|
0
|
|
|
|
Reply
|
ssg14j (14)
|
7/6/2005 1:37:31 PM |
|
hi ssg,
this is program using display date and time ..
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(void)
{
// system("date");
time_t rt;
struct tm * ti; // this structure have all int variable of tm
members... like tm_hour
char *p;
time (&rt); // this one return 0:0:0 gst 1979
ti = localtime ( &rt ); // get local time store to tm structure
printf ( "Current date and time are: %s", asctime (ti) );
//converting date structure to string
printf ("%d::%d::%d",ti->tm_hour,ti->tm_min,ti->tm_sec); //simply
using that structure
}
By
Sree Krishna
TooMuch Semiconductors Solutions
INDIA
|
|
0
|
|
|
|
Reply
|
N.Chellappa (47)
|
7/6/2005 1:45:47 PM
|
|
chellappa wrote:
> hi ssg,
> this is program using display date and time ..
> #include<stdio.h>
> #include<stdlib.h>
> #include<time.h>
> int main(void)
> {
> // system("date");
> time_t rt;
> struct tm * ti; // this structure have all int variable of tm
> members... like tm_hour
> char *p;
What is this doing here?
> time (&rt); // this one return 0:0:0 gst 1979
Huh?
No check for error here.
> ti = localtime ( &rt ); // get local time store to tm structure
No check for error.
> printf ( "Current date and time are: %s", asctime (ti) );
You need a \n at the end of your format string.
> //converting date structure to string
> printf ("%d::%d::%d",ti->tm_hour,ti->tm_min,ti->tm_sec); //simply
Again, you need a \n at the end of the format string.
Why the double colons?
> using that structure
> }
Robert Gamble
|
|
0
|
|
|
|
Reply
|
rgamble99 (794)
|
7/6/2005 3:00:26 PM
|
|
SSG wrote:
> Hi All!
> I want to know how to display the system time without
> using--system("date").
> can anyone give me the idea to handle this operation.
Please get an elementary C textbook. Use the one in the library if you
can't afford one.
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now = time(0);
printf("The time is %s", ctime(&now));
return 0;
}
|
|
0
|
|
|
|
Reply
|
mambuhl (2201)
|
7/6/2005 3:28:12 PM
|
|
chellappa wrote on 06/07/05 :
> #include<stdio.h>
> #include<stdlib.h>
> #include<time.h>
> int main(void)
> {
> // system("date");
> time_t rt;
> struct tm * ti; // this structure have all int variable of tm
> members... like tm_hour
> char *p;
> time (&rt); // this one return 0:0:0 gst 1979
> ti = localtime ( &rt ); // get local time store to tm structure
> printf ( "Current date and time are: %s", asctime (ti) );
> //converting date structure to string
> printf ("%d::%d::%d",ti->tm_hour,ti->tm_min,ti->tm_sec); //simply
> using that structure
> }
A little enhancement...
#include<stdio.h>
#include<time.h>
int main (void)
{
/* this structure have all int variable of tm members... like
tm_hour */
struct tm ti;
time_t rt;
time (&rt);
/* get local time store to tm structure */
ti = *localtime (&rt);
printf ("Current date and time are: %s", asctime (&ti));
/* converting date structure to string
simply using that structure
*/
printf ("%02d:%02d:%02d\n", ti.tm_hour, ti.tm_min, ti.tm_sec);
return 0;
}
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
..sig under repair
|
|
0
|
|
|
|
Reply
|
emdel (952)
|
7/6/2005 6:03:59 PM
|
|
Robert Gamble wrote:
> chellappa wrote:
>> printf ( "Current date and time are: %s", asctime (ti) );
>
> You need a \n at the end of your format string.
Only if the output is to be double-spaced. The
string returned by asctime() has a '\n' at the end.
--
Eric Sosman
esosman@acm-dot-org.invalid
|
|
0
|
|
|
|
Reply
|
esosman (1335)
|
7/6/2005 6:38:36 PM
|
|
Eric Sosman wrote:
> Robert Gamble wrote:
>
> > chellappa wrote:
> >> printf ( "Current date and time are: %s", asctime (ti) );
> >
> > You need a \n at the end of your format string.
>
> Only if the output is to be double-spaced. The
> string returned by asctime() has a '\n' at the end.
That's me not engaging my brain ;)
Robert Gamble
|
|
0
|
|
|
|
Reply
|
rgamble99 (794)
|
7/6/2005 7:13:33 PM
|
|
"Martin Ambuhl" <mambuhl@earthlink.net> wrote in message
news:gMSye.17030$pa3.14288@newsread2.news.atl.earthlink.net...
> SSG wrote:
>> Hi All!
>> I want to know how to display the system time without
>> using--system("date").
>> can anyone give me the idea to handle this operation.
>
> Please get an elementary C textbook. Use the one in the library if you
> can't afford one.
Why must one even travel to a library? Why are not older editions of K&R
available online?
>
> #include <stdio.h>
> #include <time.h>
>
> int main(void)
> {
> time_t now = time(0);
> printf("The time is %s", ctime(&now));
> return 0;
> }
>
>
>
|
|
0
|
|
|
|
Reply
|
oompah62 (8)
|
7/6/2005 8:44:29 PM
|
|
"Stephen Mayes" writes:
>> Please get an elementary C textbook. Use the one in the library if you
>> can't afford one.
>
> Why must one even travel to a library? Why are not older editions of K&R
> available online?
Copyrights can last a long time in the US. Mickey Mouse is still covered by
copyright and he goes back to 1928 or so. Complain to your congressman if
you think copyrights last too long. Otherwise come back in a hundred years.
The current edition of K&R is copyrighted 1988. The version prior to that
is hardly useful to write programs in the current environment. Not that any
of that makes any difference WRT your question.
|
|
0
|
|
|
|
Reply
|
r124c4u1022 (2252)
|
7/6/2005 8:58:33 PM
|
|
Stephen Mayes wrote on 06/07/05 :
>> Please get an elementary C textbook. Use the one in the library if you
>> can't afford one.
>
> Why must one even travel to a library? Why are not older editions of K&R
> available online?
This a legal on-line C-book:
http://publications.gbdirect.co.uk/c_book/
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
|
|
0
|
|
|
|
Reply
|
emdel (952)
|
7/6/2005 9:15:41 PM
|
|
Stephen Mayes wrote:
> Why must one even travel to a library? Why are not older editions of K&R
> available online?
In this newsgroup, very few of us would suggest that you engage in
illegal behavior. Don't tell us if you do find and use such an illegal
copy; some people here have been known to report these violations of law.
|
|
0
|
|
|
|
Reply
|
mambuhl (2201)
|
7/6/2005 9:56:18 PM
|
|
On Wed, 6 Jul 2005 13:58:33 -0700, in comp.lang.c , "osmium"
<r124c4u102@comcast.net> wrote:
>"Stephen Mayes" writes:
>
>>> Please get an elementary C textbook. Use the one in the library if you
>>> can't afford one.
>>
>> Why must one even travel to a library? Why are not older editions of K&R
>> available online?
>
>Copyrights can last a long time in the US.
Not just in the US - the Berne Convention requires fifty years after
the owner's death.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
|
|
0
|
|
|
|
Reply
|
markmcintyre (4547)
|
7/6/2005 10:36:42 PM
|
|
Just thought I'd mention a couple things...
> time (&rt);
Since time() returns -1 on failure, we need to do some error checking:
time_t rt;
if ((rt = time(NULL)) == -1) {
perror("time()");
exit(EXIT_FAILURE);
}
> ti = *localtime (&rt);
Again, some error checking is needed here. Since localtime() returns NULL on
failure, it's possible that your code might dereference a NULL pointer.
A better function should be available, localtime_r(), which allows easier error
checking and is also threadsafe.
Cheers.
--
# /dev/geek
|
|
0
|
|
|
|
Reply
|
dev
|
7/7/2005 3:36:33 AM
|
|
/dev/geek wrote:
> Just thought I'd mention a couple things...
>
>> time (&rt);
>
>
> Since time() returns -1 on failure, we need to do some error checking:
>
> time_t rt;
>
> if ((rt = time(NULL)) == -1) {
It will probably never make any difference, but the
pedant-approved way to write this test is
if ((rt = time(NULL)) == (time_t)-1) {
Here's why: We do not actually know what hides behind
the `time_t' mask; all we know is that it is an arithmetic
type capable of representing times. "Arithmetic type" could
be any signed or unsigned integer type or even a floating-point
type; in C99 I suppose it could be a complex type. (Hawking
has argued on cosmological grounds that time is complex.)
Now, suppose `time_t' is an unsigned integer type that
is narrower than `int' -- `unsigned char' or `unsigned short'
or one of C99's extended types, say. Then if time() fails,
rt will have a positive narrower-than-`int' value (it will
be sometype_MAX), and this value will remain positive after
being promoted for comparison with the `int' value -1. The
comparison will report "unequal" and the failure will go
undetected.
Admittedly, this is all pretty far-fetched. The original
reason time() took an argument was that the "returned" value
was too large for the 16-bit `int' of the era and `long' hadn't
been invented yet, so it's unlikely that you'll ever find a
`time_t' that's narrower than `int'. Indeed, some systems are
beginning to adopt a `time_t' that's wider than `long'! Still,
there's always the remote possibility that your code might need
to run on the DeathStation 9000, where `time_t' occupies three
eleven-bit bytes and an `int' occupies five ...
--
Eric Sosman
esosman@acm-dot-org.invalid
|
|
0
|
|
|
|
Reply
|
esosman (1335)
|
7/7/2005 1:16:49 PM
|
|
> It will probably never make any difference, but the
> pedant-approved way to write this test is
Aye, I purposely left that off. :-) My intent was to show a basic error
checking method, not to be pedantic.
Cheers.
--
# /dev/geek
|
|
0
|
|
|
|
Reply
|
dev
|
7/7/2005 1:34:17 PM
|
|
Eric Sosman wrote:
> /dev/geek wrote:
> > Just thought I'd mention a couple things...
> >
> >> time (&rt);
> >
> >
> > Since time() returns -1 on failure, we need to do some error checking:
> >
> > time_t rt;
> >
> > if ((rt = time(NULL)) == -1) {
>
> It will probably never make any difference, but the
> pedant-approved way to write this test is
>
> if ((rt = time(NULL)) == (time_t)-1) {
>
> Here's why: We do not actually know what hides behind
> the `time_t' mask; all we know is that it is an arithmetic
> type capable of representing times. "Arithmetic type" could
> be any signed or unsigned integer type or even a floating-point
> type;
In which case, the above pedant-approved line is no longer much
pedant-approved.Ain't it so?
> in C99 I suppose it could be a complex type. (Hawking
> has argued on cosmological grounds that time is complex.)
[snip]
|
|
0
|
|
|
|
Reply
|
skarpio (196)
|
7/8/2005 4:18:07 AM
|
|
"Suman" <skarpio@gmail.com> writes:
> Eric Sosman wrote:
[...]
>> It will probably never make any difference, but the
>> pedant-approved way to write this test is
>>
>> if ((rt = time(NULL)) == (time_t)-1) {
>>
>> Here's why: We do not actually know what hides behind
>> the `time_t' mask; all we know is that it is an arithmetic
>> type capable of representing times. "Arithmetic type" could
>> be any signed or unsigned integer type or even a floating-point
>> type;
>
> In which case, the above pedant-approved line is no longer much
> pedant-approved.Ain't it so?
Why not? time() returns (time_t)-1 on error; if it's floating-point,
that's -1.0. Unless the floating-point subsystem is so badly screwed
up that -1.0 != -1.0, the above line should be safe.
--
Keith Thompson (The_Other_Keith) kst-u@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.
|
|
0
|
|
|
|
Reply
|
kst-u (21474)
|
7/8/2005 7:50:10 AM
|
|
Keith Thompson wrote:
> "Suman" <skarpio@gmail.com> writes:
> > Eric Sosman wrote:
> [...]
> >> It will probably never make any difference, but the
> >> pedant-approved way to write this test is
> >>
> >> if ((rt = time(NULL)) == (time_t)-1) {
> >>
> >> Here's why: We do not actually know what hides behind
> >> the `time_t' mask; all we know is that it is an arithmetic
> >> type capable of representing times. "Arithmetic type" could
> >> be any signed or unsigned integer type or even a floating-point
> >> type;
> >
> > In which case, the above pedant-approved line is no longer much
> > pedant-approved.Ain't it so?
>
> Why not? time() returns (time_t)-1 on error; if it's floating-point,
> that's -1.0. Unless the floating-point subsystem is so badly screwed
> up that -1.0 != -1.0, the above line should be safe.
Because direct comparison on floating point numbers are almost always
best avoided. Or, that is what I thought was the right thing to do in
general.
|
|
0
|
|
|
|
Reply
|
skarpio (196)
|
7/8/2005 8:00:22 AM
|
|
"Suman" <skarpio@gmail.com> wrote:
> Keith Thompson wrote:
> > "Suman" <skarpio@gmail.com> writes:
> > > Eric Sosman wrote:
> > >> Here's why: We do not actually know what hides behind
> > >> the `time_t' mask; all we know is that it is an arithmetic
> > >> type capable of representing times. "Arithmetic type" could
> > >> be any signed or unsigned integer type or even a floating-point
> > >> type;
> > >
> > > In which case, the above pedant-approved line is no longer much
> > > pedant-approved.Ain't it so?
> >
> > Why not? time() returns (time_t)-1 on error; if it's floating-point,
> > that's -1.0. Unless the floating-point subsystem is so badly screwed
> > up that -1.0 != -1.0, the above line should be safe.
> Because direct comparison on floating point numbers are almost always
> best avoided. Or, that is what I thought was the right thing to do in
> general.
It _is_ true in general. In this specific case, though, the Standard
speaks literally of (time_t)(-1), and -1.0 must be exactly representable
in any kind of float, so this is safe.
Richard
|
|
0
|
|
|
|
Reply
|
rlb (4118)
|
7/8/2005 10:24:13 AM
|
|
Richard Bos wrote:
> "Suman" <skarpio@gmail.com> wrote:
>
> > Keith Thompson wrote:
> > > "Suman" <skarpio@gmail.com> writes:
> > > > Eric Sosman wrote:
> > > >> Here's why: We do not actually know what hides behind
> > > >> the `time_t' mask; all we know is that it is an arithmetic
> > > >> type capable of representing times. "Arithmetic type" could
> > > >> be any signed or unsigned integer type or even a floating-point
> > > >> type;
> > > >
> > > > In which case, the above pedant-approved line is no longer much
> > > > pedant-approved.Ain't it so?
> > >
> > > Why not? time() returns (time_t)-1 on error; if it's floating-point,
> > > that's -1.0. Unless the floating-point subsystem is so badly screwed
> > > up that -1.0 != -1.0, the above line should be safe.
> > Because direct comparison on floating point numbers are almost always
> > best avoided. Or, that is what I thought was the right thing to do in
> > general.
>
> It _is_ true in general. In this specific case, though, the Standard
> speaks literally of (time_t)(-1),
I am not too sure I follow you, can you be more specific?
> and -1.0 must be exactly representable
> in any kind of float, so this is safe.
Aye, aye! Sir!
|
|
0
|
|
|
|
Reply
|
skarpio (196)
|
7/8/2005 10:42:02 AM
|
|
"Suman" <skarpio@gmail.com> wrote:
> Richard Bos wrote:
> > It _is_ true in general. In this specific case, though, the Standard
> > speaks literally of (time_t)(-1),
> I am not too sure I follow you, can you be more specific?
The only way I could is by quoting C&V: 7.23.2.4#3, in n869.txt.
Richard
|
|
0
|
|
|
|
Reply
|
rlb (4118)
|
7/8/2005 10:54:53 AM
|
|
Richard Bos wrote:
> "Suman" <skarpio@gmail.com> wrote:
>
> > Richard Bos wrote:
> > > It _is_ true in general. In this specific case, though, the Standard
> > > speaks literally of (time_t)(-1),
> > I am not too sure I follow you, can you be more specific?
>
> The only way I could is by quoting C&V: 7.23.2.4#3, in n869.txt.
That's a lot of help. Will check. Thanks.
|
|
0
|
|
|
|
Reply
|
skarpio (196)
|
7/8/2005 11:14:27 AM
|
|
|
21 Replies
22 Views
(page loaded in 0.251 seconds)
|