abt time functions

S

SSG

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
 
C

chellappa

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
 
R

Robert Gamble

chellappa said:
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

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
 
M

Martin Ambuhl

SSG said:
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;
}
 
E

Emmanuel Delahaye

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
 
E

Eric Sosman

Robert said:
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.
 
R

Robert Gamble

Eric said:
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
 
S

Stephen Mayes

Martin Ambuhl said:
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?
 
O

osmium

Stephen Mayes said:
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.
 
E

Emmanuel Delahaye

M

Martin Ambuhl

Stephen said:
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.
 
D

/dev/geek

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.
 
E

Eric Sosman

/dev/geek said:
Just thought I'd mention a couple things...



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 ...
 
D

/dev/geek

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.
 
S

Suman

Eric said:
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]
 
K

Keith Thompson

Suman said:
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.
 
S

Suman

Keith said:
Suman said:
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.
 
R

Richard Bos

Suman said:
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
 
S

Suman

Richard said:
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!
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top