What does ((time_t)-1) mean?

L

loudking

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!
 
P

Pietro Cerutti

loudking said:
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;
}
 
M

Martin Wells

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
 
L

loudking

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

Martin Wells

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
 
P

Pietro Cerutti

loudking said:
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).
 
J

James Kuyper Jr.

Martin said:
loudking:
....
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.
 
J

James Kuyper

Pietro said:
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.
 
E

Eric Sosman

Pietro said:
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?)
 
K

Kenny McCormack

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;
}
 
M

Mark Bluemel

loudking said:
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.
 
R

Richard

loudking said:
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.
 
K

Kenneth Brody

Kenny said:
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:[email protected]>
 
R

Richard

Kenneth Brody said:
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.
 
M

Martien Verbruggen

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
 
?

=?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=

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

Mark Bluemel

Kenneth said:
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.
 
M

Mark Bluemel

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

Mark Bluemel

Martien said:
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...
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top