Velocity Reviews > What does ((time_t)-1) mean?

What does ((time_t)-1) mean?

loudking
Guest
Posts: n/a

 10-24-2007
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;
}

Pietro Cerutti
Guest
Posts: n/a

 10-24-2007
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;
> }
>
>

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

Martin Wells
Guest
Posts: n/a

 10-24-2007
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

loudking
Guest
Posts: n/a

 10-24-2007
> 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) {
.......

Martin Wells
Guest
Posts: n/a

 10-24-2007
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

Pietro Cerutti
Guest
Posts: n/a

 10-24-2007
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

James Kuyper Jr.
Guest
Posts: n/a

 10-24-2007
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
Guest
Posts: n/a

 10-24-2007
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.

Eric Sosman
Guest
Posts: n/a

 10-24-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid

Kenny McCormack
Guest
Posts: n/a

 10-24-2007
In article <(E-Mail Removed). com>,
loudking <(E-Mail Removed)> 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;
}