stuck on time_t

J

Jim Showalter

I want to get the current date/time using time(), then use ctime() to
display it.

What type is time_t? I've tried looking it up in time.h and elsewhere.
"grep" shows: typedef __time_t time_t;

which, in turn, greps as: __time_t tv_sec;

Here's my latest attempt:


#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int
main (void)
{
time_t eot, *tod;

eot = time(tod);
printf("*tod\t = %ul\n", *tod);
printf("Today's date is: %s\n", ctime(tod));

return EXIT_SUCCESS;
}


What am I doing wrong?

Also, how can I determine the max value time_t can hold?


jim
 
E

Emmanuel Delahaye

Jim Showalter wrote on 08/08/04 :
I want to get the current date/time using time(), then use ctime() to
display it.

What type is time_t?

It's time_t. You don't need to know more except that it could be a
floating point.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main (void)
{
time_t eot, *tod;

eot = time(tod);

Wrong. You are passing an undefined value to a function. My compiler
chokes on that. Yours should if it has been correctly configured.

eot = time (NULL);

or

time (&eot);

get rid of this tod. It's useless.
printf("*tod\t = %ul\n", *tod);
printf("Today's date is: %s\n", ctime(tod));

printf("*tod\t = %ul\n", (unsigned long) eot);
printf("Today's date is: %s\n", ctime(eot));
return EXIT_SUCCESS;
}

Note that ctime() returns the adress of a static string. It may behave
strangely if you don't make a copy of the pointed string. Better to use
strftime() (and it's more fun, actually).
 
J

Jim Showalter

Emmanuel said:
Jim Showalter wrote on 08/08/04 :

It's time_t. You don't need to know more except that it could be a
floating point.

Note that ctime() returns the adress of a static string. It may behave
strangely if you don't make a copy of the pointed string. Better to use
strftime() (and it's more fun, actually).

Ok, I got it working - thanks Emmanuel! But you missed my other
question at the end, which was: How can I determine the max value
time_t can hold?


jim
 
S

SM Ryan

# Ok, I got it working - thanks Emmanuel! But you missed my other
# question at the end, which was: How can I determine the max value
# time_t can hold?

System dependent. On current unices it's good to about 2039. Mac Classic
is until 2104 I think, perhaps. Windows clocks are good until 2003. Or
8:03 PM if you prefer that notation. If you have a choice, don't store
time_t in a file, but use something like ISO calendar values. Hopefully
the time_t value will change to 64 bits before 2039.
 
J

Jim Showalter

SM said:
# Ok, I got it working - thanks Emmanuel! But you missed my other
# question at the end, which was: How can I determine the max value
# time_t can hold?

System dependent. On current unices it's good to about 2039. Mac Classic
is until 2104 I think, perhaps. Windows clocks are good until 2003. Or
8:03 PM if you prefer that notation. If you have a choice, don't store
time_t in a file, but use something like ISO calendar values. Hopefully
the time_t value will change to 64 bits before 2039.

2003? Glad I'm not programming on Windows! :)

Seriously, that's about what I figured - but not my concern. I'm just
trying to complete the first "Programming Challenge" in Peter van der
Linden's book, "Expert C Programming", and I'm already stumped!

Surely there is a method using C to determine the greatest value that
any type can hold?


jim
 
S

SM Ryan

# Surely there is a method using C to determine the greatest value that
# any type can hold?

sizeof(T)*CHAR_BIT will usually be the number of bits, but doesn't say
anything about how the value is encoded in the bits.
 
C

CBFalconer

Jim said:
.... snip ...

Surely there is a method using C to determine the greatest value
that any type can hold?

#include <limits.h>
#include <float.h>
 
J

Joe Wright

Emmanuel said:
Jim Showalter wrote on 08/08/04 :



It's time_t. You don't need to know more except that it could be a
floating point.



Wrong. You are passing an undefined value to a function. My compiler
chokes on that. Yours should if it has been correctly configured.

eot = time (NULL);

or

time (&eot);

get rid of this tod. It's useless.



printf("*tod\t = %ul\n", (unsigned long) eot);
printf("Today's date is: %s\n", ctime(eot));



Note that ctime() returns the adress of a static string. It may behave
strangely if you don't make a copy of the pointed string. Better to use
strftime() (and it's more fun, actually).

time.h on my system prototypes ...

char * ctime(const time_t *_cal);

.... indicating that ctime() wants a pointer, ctime(&eot) in your
example. No?
 
E

Emmanuel Delahaye

Joe Wright wrote on 08/08/04 :
char * ctime(const time_t *_cal);

... indicating that ctime() wants a pointer, ctime(&eot) in your example. No?

Yes, I meant:

printf("Today's date is: %s\n", ctime(&eot));

thanks for the correction.
 

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,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top