stuck on time_t

Discussion in 'C Programming' started by Jim Showalter, Aug 8, 2004.

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

    --
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    _/ Everything should be made as simple as possible, but not simpler.
    _/ -- Albert Einstein
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
     
    Jim Showalter, Aug 8, 2004
    #1
    1. Advertising

  2. 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).

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Aug 8, 2004
    #2
    1. Advertising

  3. Emmanuel Delahaye wrote:

    > 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.
    >
    > 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
    --
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    _/ Everything should be made as simple as possible, but not simpler.
    _/ -- Albert Einstein
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
     
    Jim Showalter, Aug 8, 2004
    #3
  4. Jim Showalter

    SM Ryan Guest

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

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    The little stoner's got a point.
     
    SM Ryan, Aug 8, 2004
    #4
  5. SM Ryan wrote:

    > # 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.
    >
    > --
    > SM Ryan http://www.rawbw.com/~wyrmwif/
    > The little stoner's got a point.


    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
    --
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
    _/ Everything should be made as simple as possible, but not simpler.
    _/ -- Albert Einstein
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
     
    Jim Showalter, Aug 8, 2004
    #5
  6. Jim Showalter

    SM Ryan Guest

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

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    GERBILS
    GERBILS
    GERBILS
     
    SM Ryan, Aug 8, 2004
    #6
  7. Jim Showalter

    CBFalconer Guest

    Jim Showalter wrote:
    >

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


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

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 8, 2004
    #7
  8. Jim Showalter

    Joe Wright Guest

    Emmanuel Delahaye wrote:

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


    time.h on my system prototypes ...

    char * ctime(const time_t *_cal);

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

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Aug 8, 2004
    #8
  9. Joe Wright wrote on 08/08/04 :
    >> printf("Today's date is: %s\n", ctime(eot));

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

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Aug 8, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWFyaw==?=

    DateTime to time_t bug

    =?Utf-8?B?TWFyaw==?=, May 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,680
    Steven Cheng[MSFT]
    May 18, 2004
  2. Anand CS

    converting to time_t

    Anand CS, Jul 3, 2003, in forum: C Programming
    Replies:
    1
    Views:
    7,739
    Luke Driscoll
    Jul 3, 2003
  3. Dave Sinkula

    Casting (time_t)(-1)

    Dave Sinkula, Mar 2, 2004, in forum: C Programming
    Replies:
    4
    Views:
    641
    Dan Pop
    Mar 2, 2004
  4. j0mbolar

    time_t in seconds

    j0mbolar, Apr 18, 2004, in forum: C Programming
    Replies:
    6
    Views:
    10,289
    Dan Pop
    Apr 19, 2004
  5. Jim Showalter

    Re: stuck on time_t

    Jim Showalter, Aug 11, 2004, in forum: C Programming
    Replies:
    21
    Views:
    1,100
    Dave Thompson
    Aug 16, 2004
Loading...

Share This Page