What does ((time_t)-1) mean?

Discussion in 'C Programming' started by loudking, Oct 24, 2007.

  1. loudking

    loudking Guest

    Hello, all

    I don't quite understand what does ((time_t)-1) mean when I execute
    "man 2 time"

    RETURN VALUE
    On success, the value of time in seconds since the Epoch is
    retu
    rned.
    On error, ((time_t)-1) is returned, and errno is set
    appropriately
    ..
    Could anybody tell me its meaning please?

    And, is my way of checking return value correct?

    time_t now;

    time(&now);
    if (!now) {
    fprintf(stderr, "Unable to fetch time information: %s
    \n",
    strerror(errno));
    return 1;
    }

    Thanks for answering!
     
    loudking, Oct 24, 2007
    #1
    1. Advertising

  2. loudking wrote:
    > Hello, all
    >
    > I don't quite understand what does ((time_t)-1) mean when I execute
    > "man 2 time"
    >
    > RETURN VALUE
    > On success, the value of time in seconds since the Epoch is
    > retu
    > rned.
    > On error, ((time_t)-1) is returned, and errno is set
    > appropriately
    > .
    > Could anybody tell me its meaning please?


    time's return type is time_t, so the return value in case of error is a
    time_t typed value of -1

    >
    > And, is my way of checking return value correct?
    >
    > time_t now;
    >
    > time(&now);
    > if (!now) {
    > fprintf(stderr, "Unable to fetch time information: %s
    > \n",
    > strerror(errno));
    > return 1;
    > }
    >
    > Thanks for answering!
    >


    time_t now;
    if(time(&now) == -1) {
    fprintf(stderr, "Unable to fetch time information: %sn",
    strerror(errno));
    return 1;
    }



    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
     
    Pietro Cerutti, Oct 24, 2007
    #2
    1. Advertising

  3. loudking

    Martin Wells Guest

    loudking:

    > On error, ((time_t)-1) is returned, and errno is set
    > appropriately
    > .
    > Could anybody tell me its meaning please?



    When a signed integer type expression whose value is negative is
    converted to an unsigned integer type, e.g.:

    unsigned a = -5;

    short unsigned b = -5;

    char unsigned c = -5;

    then it's the same as:

    unsigned a = UINT_MAX - 4;

    short unsigned b = USHRT_MAX - 4;

    char unsigned c = UCHAR_MAX - 4;

    Therefore, when you assign -1 to an unsigned integer type, you're
    giving it its maximum value (i.e. MAX - 0)

    A more general formula would be:

    (uint_type)-x

    is the same as:

    UINT_TYPE_MAX - (x-1)


    The function you're asking about will return the max value for a
    time_t.

    Martin
     
    Martin Wells, Oct 24, 2007
    #3
  4. loudking

    loudking Guest

    > time_t now;
    > if(time(&now) == -1) {
    > fprintf(stderr, "Unable to fetch time information: %sn",
    > strerror(errno));
    > return 1;
    >
    > }


    Then do i have to write it in this way?

    if(time(&now) == (time_t)-1) {
    .......
     
    loudking, Oct 24, 2007
    #4
  5. loudking

    Martin Wells Guest

    loudking:

    > Then do i have to write it in this way?
    >
    > if(time_t(&now) == (time_t)-1)



    I don't know what function you're working with, but if it returns a
    time_t, then you'd want:

    if ((time_t)-1 == Func())

    (The time_t cast is redundant if time_t is bigger than int, but still
    I'd leave it in)

    Martin
     
    Martin Wells, Oct 24, 2007
    #5
  6. loudking wrote:
    >> time_t now;
    >> if(time(&now) == -1) {
    >> fprintf(stderr, "Unable to fetch time information: %sn",
    >> strerror(errno));
    >> return 1;
    >>
    >> }

    >
    > Then do i have to write it in this way?
    >
    > if(time(&now) == (time_t)-1) {
    > ......
    >


    You don't have to. It's done automatically (integer promotion).


    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
     
    Pietro Cerutti, Oct 24, 2007
    #6
  7. Martin Wells wrote:
    > loudking:
    >
    >> On error, ((time_t)-1) is returned, and errno is set
    >> appropriately
    >> .
    >> Could anybody tell me its meaning please?

    ....
    > The function you're asking about will return the max value for a
    > time_t.


    It's an expression, not a function. It has a value which is the result
    of converting -1 to whatever type time_t is. Your answer is correct only
    if time_t is an unsigned type. If it is a signed integer or floating
    point type, then that expression will have a value of -1, which is very
    definitely not the maximum value for those types.
     
    James Kuyper Jr., Oct 24, 2007
    #7
  8. loudking

    James Kuyper Guest

    Pietro Cerutti wrote:
    > loudking wrote:
    >>> time_t now;
    >>> if(time(&now) == -1) {
    >>> fprintf(stderr, "Unable to fetch time information: %sn",
    >>> strerror(errno));
    >>> return 1;
    >>>
    >>> }

    >> Then do i have to write it in this way?
    >>
    >> if(time(&now) == (time_t)-1) {
    >> ......
    >>

    >
    > You don't have to. It's done automatically (integer promotion).


    You're making assumptions about time_t which might be reasonable, but
    which are not required by the standard, which only requires it to be an
    arithmetic type. If, for instance, time_t is a 32-bit unsigned short,
    and int is 64 bits, then leaving out the cast will result in value of
    USHRT_MAX being promoted to an 'int' and then failing when it is
    compared with -1.

    In real life, systems where time_t is both unsigned and smaller than int
    are probably very uncommon, maybe even non-existent; but they would be
    legal.
     
    James Kuyper, Oct 24, 2007
    #8
  9. loudking

    Eric Sosman Guest

    Pietro Cerutti wrote:
    > loudking wrote:
    >>> time_t now;
    >>> if(time(&now) == -1) {
    >>> fprintf(stderr, "Unable to fetch time information: %sn",
    >>> strerror(errno));
    >>> return 1;
    >>>
    >>> }

    >> Then do i have to write it in this way?
    >>
    >> if(time(&now) == (time_t)-1) {
    >> ......
    >>

    >
    > You don't have to. It's done automatically (integer promotion).


    If (time_t)-1 < INT_MAX, the cast is required. (I've never
    seen and may never see a system where this condition holds, but
    why take even a small risk if it's unnecessary?)

    --
    Eric Sosman
    lid
     
    Eric Sosman, Oct 24, 2007
    #9
  10. In article <>,
    loudking <> wrote:
    >Hello, all
    >
    >I don't quite understand what does ((time_t)-1) mean when I execute
    >"man 2 time"
    >


    This (perfectly legal) program says that the answer to your question is 5:

    #include <stdio.h>
    int main(void)
    {
    int time_t = 6;
    printf("The result is %d\n",((time_t)-1));
    return 0;
    }
     
    Kenny McCormack, Oct 24, 2007
    #10
  11. loudking

    Mark Bluemel Guest

    loudking wrote:
    > Hello, all
    >
    > I don't quite understand what does ((time_t)-1) mean when I execute
    > "man 2 time"


    Referencing "man 2" is probably a hint that the question really belongs
    in comp.unix.programmer.

    However, you've provided more or less enough information for us to
    answer the question in terms of what is regarded as "on-topic" in
    comp.lang.c

    (time_t)-1 is an example of a "cast" operation.

    We assume that somewhere (in a header, probably called "time.h") a
    typedef has declared an integer type called "time_t". (time_t)-1 is -1
    expressed as one of these types.

    > RETURN VALUE
    > On success, the value of time in seconds since the Epoch is
    > returned.
    > On error, ((time_t)-1) is returned, and errno is set
    > appropriately


    So when you call time(), if it is successful it returns a positive
    time_t value but if it is unsuccessful, it returns (time_t)-1;
    (The reference to "errno" here is off-topic for comp.lang.c, I think)

    > And, is my way of checking return value correct?


    > time_t now;
    >
    > time(&now);
    > if (!now) {


    The error path would only be taken if "now" was (time_t)0 - wouldn't it?

    In addition, the manual page specified that time() _returns (time_t)-1
    for an error, not that it sets the time_t passed to it to (time_t)-1.

    The natural way of using this function is probably something like :-

    time_t now;
    if (time(&now) == (time_t)-1) {
    fprintf(stderr, "Unable to fetch time information: %s\n",
    strerror(errno));
    return 1;
    }


    The cast is, probably, unnecessary - but a) it could conceivably be
    necessary (see Eric's comments) and b) it makes things nice and explicit.
     
    Mark Bluemel, Oct 24, 2007
    #11
  12. loudking

    Richard Guest

    loudking <> writes:

    > Hello, all
    >
    > I don't quite understand what does ((time_t)-1) mean when I execute
    > "man 2 time"
    >
    > RETURN VALUE
    > On success, the value of time in seconds since the Epoch is
    > retu
    > rned.
    > On error, ((time_t)-1) is returned, and errno is set
    > appropriately
    > .
    > Could anybody tell me its meaning please?
    >
    > And, is my way of checking return value correct?
    >
    > time_t now;
    >
    > time(&now);


    You didn't check the "return value" - however "now" does contain a copy
    of the return value....


    > if (!now) {


    "!now" is not the same as checking for a time_t of -1 .....

    > fprintf(stderr, "Unable to fetch time information: %s
    > \n",
    > strerror(errno));
    > return 1;
    > }
    >
    > Thanks for answering!


    I would think something like

    if(time(&now)==(time_t)-1){
    error
    }

    is better ... as the man page says.
     
    Richard, Oct 24, 2007
    #12
  13. Kenny McCormack wrote:
    >
    > In article <>,
    > loudking <> wrote:
    > >Hello, all
    > >
    > >I don't quite understand what does ((time_t)-1) mean when I execute
    > >"man 2 time"
    > >

    >
    > This (perfectly legal) program says that the answer to your question is 5:
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > int time_t = 6;
    > printf("The result is %d\n",((time_t)-1));
    > return 0;
    > }


    Is it legal to declare a variable named "time_t"? True, you have
    not included <time.h> which is why it "works".

    Are you allowed to repurpose a type/variable/macro defined by the
    standard, as long as you don't use anything related to the way
    the standard defines them?

    If, for example, defining your own printf() function invokes UB,
    even if you never make reference (direct or otherwise) to the
    standard's printf() function, doesn't the same hold true for the
    time_t typedef?

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Oct 24, 2007
    #13
  14. loudking

    Richard Guest

    Kenneth Brody <> writes:

    > Kenny McCormack wrote:
    >>
    >> In article <>,
    >> loudking <> wrote:
    >> >Hello, all
    >> >
    >> >I don't quite understand what does ((time_t)-1) mean when I execute
    >> >"man 2 time"
    >> >

    >>
    >> This (perfectly legal) program says that the answer to your question is 5:
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> int time_t = 6;
    >> printf("The result is %d\n",((time_t)-1));
    >> return 0;
    >> }

    >
    > Is it legal to declare a variable named "time_t"? True, you have
    > not included <time.h> which is why it "works".
    >
    > Are you allowed to repurpose a type/variable/macro defined by the
    > standard, as long as you don't use anything related to the way
    > the standard defines them?
    >
    > If, for example, defining your own printf() function invokes UB,
    > even if you never make reference (direct or otherwise) to the
    > standard's printf() function, doesn't the same hold true for the
    > time_t typedef?


    If you don't include the definition then why isn't it perfectly ok?
    Stupid, but ok.
     
    Richard, Oct 24, 2007
    #14
  15. On Wed, 24 Oct 2007 16:21:26 +0100,
    Mark Bluemel <> wrote:
    > loudking wrote:
    >> Hello, all
    >>
    >> I don't quite understand what does ((time_t)-1) mean when I execute
    >> "man 2 time"

    >
    > Referencing "man 2" is probably a hint that the question really belongs
    > in comp.unix.programmer.


    The type time_t is defined in the C language standard. The fact that
    time() on the OP's system is in section 2 of the manual pages does not
    change that.

    Martien
    --
    |
    Martien Verbruggen | Think of the average person. Half of the
    | people out there are dumber.
    |
     
    Martien Verbruggen, Oct 24, 2007
    #15
  16. loudking

    santosh Guest

    Richard wrote:

    > Kenneth Brody <> writes:
    >
    >> Kenny McCormack wrote:
    >>>
    >>> In article <>,
    >>> loudking <> wrote:
    >>> >Hello, all
    >>> >
    >>> >I don't quite understand what does ((time_t)-1) mean when I execute
    >>> >"man 2 time"
    >>> >
    >>>
    >>> This (perfectly legal) program says that the answer to your question
    >>> is 5:
    >>>
    >>> #include <stdio.h>
    >>> int main(void)
    >>> {
    >>> int time_t = 6;
    >>> printf("The result is %d\n",((time_t)-1));
    >>> return 0;
    >>> }

    >>
    >> Is it legal to declare a variable named "time_t"? True, you have
    >> not included <time.h> which is why it "works".
    >>
    >> Are you allowed to repurpose a type/variable/macro defined by the
    >> standard, as long as you don't use anything related to the way
    >> the standard defines them?
    >>
    >> If, for example, defining your own printf() function invokes UB,
    >> even if you never make reference (direct or otherwise) to the
    >> standard's printf() function, doesn't the same hold true for the
    >> time_t typedef?

    >
    > If you don't include the definition then why isn't it perfectly ok?
    > Stupid, but ok.


    I think it's UB to redefine Standard symbols.
     
    santosh, Oct 25, 2007
    #16
  17. On Thu, 25 Oct 2007 09:09:01 +0530, santosh wrote:
    > I think it's UB to redefine Standard symbols.


    Redefining external symbols is a bad idea, but defining your own symbols
    which happen to have the same name as standard symbols is allowed.
     
    =?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=, Oct 25, 2007
    #17
  18. loudking

    Mark Bluemel Guest

    Kenneth Brody wrote:
    > Kenny McCormack wrote:
    >> In article <>,
    >> loudking <> wrote:
    >>> Hello, all
    >>>
    >>> I don't quite understand what does ((time_t)-1) mean when I execute
    >>> "man 2 time"
    >>>

    >> This (perfectly legal) program says that the answer to your question is 5:
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> int time_t = 6;
    >> printf("The result is %d\n",((time_t)-1));
    >> return 0;
    >> }

    >
    > Is it legal to declare a variable named "time_t"? True, you have
    > not included <time.h> which is why it "works".


    time_t is not part of any C standard, AFAIK.

    The compiler knows nothing about it, unless you include the <time.h> header.

    time() (as discussed here) is part of POSIX, not part of C.
     
    Mark Bluemel, Oct 25, 2007
    #18
  19. loudking

    Mark Bluemel Guest

    Mark Bluemel wrote:
    > Kenneth Brody wrote:
    >> Kenny McCormack wrote:
    >>> In article <>,
    >>> loudking <> wrote:
    >>>> Hello, all
    >>>>
    >>>> I don't quite understand what does ((time_t)-1) mean when I execute
    >>>> "man 2 time"
    >>>>
    >>> This (perfectly legal) program says that the answer to your question
    >>> is 5:
    >>>
    >>> #include <stdio.h>
    >>> int main(void)
    >>> {
    >>> int time_t = 6;
    >>> printf("The result is %d\n",((time_t)-1));
    >>> return 0;
    >>> }

    >>
    >> Is it legal to declare a variable named "time_t"? True, you have
    >> not included <time.h> which is why it "works".


    Please ignore my comments below...

    > time_t is not part of any C standard, AFAIK.
    >
    > The compiler knows nothing about it, unless you include the <time.h>
    > header.
    >
    > time() (as discussed here) is part of POSIX, not part of C.
     
    Mark Bluemel, Oct 25, 2007
    #19
  20. loudking

    Mark Bluemel Guest

    Martien Verbruggen wrote:
    > On Wed, 24 Oct 2007 16:21:26 +0100,
    > Mark Bluemel <> wrote:
    >> loudking wrote:
    >>> Hello, all
    >>>
    >>> I don't quite understand what does ((time_t)-1) mean when I execute
    >>> "man 2 time"

    >> Referencing "man 2" is probably a hint that the question really belongs
    >> in comp.unix.programmer.

    >
    > The type time_t is defined in the C language standard. The fact that
    > time() on the OP's system is in section 2 of the manual pages does not
    > change that.


    That will teach me to trust the manual pages rather than reading the
    standard...
     
    Mark Bluemel, Oct 25, 2007
    #20
    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,668
    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,712
    Luke Driscoll
    Jul 3, 2003
  3. Li Ma
    Replies:
    1
    Views:
    2,288
    Roedy Green
    Mar 9, 2009
  4. Rahul
    Replies:
    4
    Views:
    585
    Robert Kern
    Apr 7, 2009
  5. C Barrington-Leigh
    Replies:
    1
    Views:
    1,214
    Tim Leslie
    Sep 10, 2010
Loading...

Share This Page