stuck on time_t

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Showalter

    stuck on time_t


    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
    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  • Emmanuel Delahaye

    #2
    Re: stuck on time_t

    Jim Showalter wrote on 08/08/04 :[color=blue]
    > I want to get the current date/time using time(), then use ctime() to
    > display it.
    >
    > What type is time_t?[/color]

    It's time_t. You don't need to know more except that it could be a
    floating point.
    [color=blue]
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    >
    > int
    > main (void)
    > {
    > time_t eot, *tod;
    >
    > eot = time(tod);[/color]

    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.
    [color=blue]
    > printf("*tod\t = %ul\n", *tod);
    > printf("Today's date is: %s\n", ctime(tod));[/color]

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

    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"

    Comment

    • Jim Showalter

      #3
      Re: stuck on time_t

      Emmanuel Delahaye wrote:
      [color=blue]
      > Jim Showalter wrote on 08/08/04 :[color=green]
      >> I want to get the current date/time using time(), then use ctime() to
      >> display it.
      >>
      >> What type is time_t?[/color]
      >
      > 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).
      >[/color]

      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
      _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

      Comment

      • SM Ryan

        #4
        Re: stuck on time_t

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

        Comment

        • Jim Showalter

          #5
          Re: stuck on time_t

          SM Ryan wrote:
          [color=blue]
          > # 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.[/color]

          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 "Programmin g 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
          _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

          Comment

          • SM Ryan

            #6
            Re: stuck on time_t

            # 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

            Comment

            • CBFalconer

              #7
              Re: stuck on time_t

              Jim Showalter wrote:[color=blue]
              >[/color]
              .... snip ...[color=blue]
              >
              > Surely there is a method using C to determine the greatest value
              > that any type can hold?[/color]

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

              --
              Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
              Available for consulting/temporary embedded and systems.
              <http://cbfalconer.home .att.net> USE worldnet address!


              Comment

              • Joe Wright

                #8
                Re: stuck on time_t

                Emmanuel Delahaye wrote:
                [color=blue]
                > Jim Showalter wrote on 08/08/04 :
                >[color=green]
                >> I want to get the current date/time using time(), then use ctime() to
                >> display it.
                >>
                >> What type is time_t?[/color]
                >
                >
                > It's time_t. You don't need to know more except that it could be a
                > floating point.
                >[color=green]
                >> #include <stdio.h>
                >> #include <stdlib.h>
                >> #include <time.h>
                >>
                >> int
                >> main (void)
                >> {
                >> time_t eot, *tod;
                >>
                >> eot = time(tod);[/color]
                >
                >
                > 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.
                >[color=green]
                >> printf("*tod\t = %ul\n", *tod);
                >> printf("Today's date is: %s\n", ctime(tod));[/color]
                >
                >
                > printf("*tod\t = %ul\n", (unsigned long) eot);
                > printf("Today's date is: %s\n", ctime(eot));
                >[color=green]
                >> return EXIT_SUCCESS;
                >> }[/color]
                >
                >
                > 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).[/color]

                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:joewwrig ht@comcast.net
                "Everything should be made as simple as possible, but not simpler."
                --- Albert Einstein ---

                Comment

                • Emmanuel Delahaye

                  #9
                  Re: stuck on time_t

                  Joe Wright wrote on 08/08/04 :[color=blue][color=green]
                  >> printf("Today's date is: %s\n", ctime(eot));[/color]
                  >
                  > char * ctime(const time_t *_cal);
                  >
                  > ... indicating that ctime() wants a pointer, ctime(&eot) in your example. No?[/color]

                  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"

                  Comment

                  Working...