Checking time_t addition for overflow

I

Ian Pilcher

When adding two values of type time_t, how can I check for overflow?
Maybe I'm just brain-cramped today, but I can't figure out how to do it.

Thanks!
 
J

Jason Wells

Ian said:
When adding two values of type time_t, how can I check for overflow?
Maybe I'm just brain-cramped today, but I can't figure out how to do it.

Thanks!
Shouldn't the result be negative if you are just adding two time_h vals that
overflow?

Thinking back, the only time I've done time_t addition has been adding
sec/mins/days to a valid time_t...
 
I

Ian Pilcher

Jason said:
Shouldn't the result be negative if you are just adding two time_h vals that
overflow?

The standard says only that time_t is an arithmetic type, so it could
be a signed or unsigned integer type or a floating point type.
 
A

Alex Fraser

Ian Pilcher said:
When adding two values of type time_t, how can I check for overflow?
Maybe I'm just brain-cramped today, but I can't figure out how to do it.

What meaning does the result of adding two values of type time_t have? Seems
to me it is rather like adding two pointers.

If what you actually want to do is add a number of seconds (a value of some
arithmetic type) to a value of type time_t to produce a new value of type
time_t (if possible), then you can use the companion function to difftime
called addtime. Oh, wait, no such function exists.

Fortunately, writing an addtime function (including checking for overflow)
is trivial for many implementations. Non-portable, of course, but since it
is easily isolated that shouldn't be a problem.

Alex
 
J

Jason Wells

Ian said:
The standard says only that time_t is an arithmetic type, so it could
be a signed or unsigned integer type or a floating point type.
But if time() can return -1 on error, you get a signed int...
 
L

Lawrence Kirby

But if time() can return -1 on error, you get a signed int...

It isn't specified as returning -1, rather (time_t)(-1) which need not be
negative. Strictly speaking that means that you should test the return
value again (time_t)-1 and not just -1, although the situations where the
latter will fail are not particularly likely.

Lawrence
 
L

Lawrence Kirby

When adding two values of type time_t, how can I check for overflow?

In geeneral you can't. Alse the representation of time_t isn't specified
by C and adding teo time_t values together doesn't really make sense. The
way you can perform "time arithmetic" in C is by using a struct tm, which
you can create using localtime(), updating the fields as you require and
using mktime() to normalise them and create a corresponding time_t value.
mktime() will return (time_t)-1 if the result cannot be represented.

Lawrence
 
A

Al Bowers

Ian said:
When adding two values of type time_t, how can I check for overflow?
Maybe I'm just brain-cramped today, but I can't figure out how to do it.

Arithmetic on two time_t variables or adding other values to a time_t
is not the intent of Standard C. The Standard does not specify
the exact type or what its instrumentality. The Standard only states
it to be an arithmetic type capable of representing time. One would
need to consult the implementation documation to manipulate the
time_t variables as you mention. The resulting code would not
be portable. Standard C conceals having to do this by providing
shrouds in functions to manipulate time (eg. functions localtime,
mktime) and to determine the difference in time (function difftime).
For example, below is an attempt to add 10 secs to a time with
the += operator (tp->tm_sec += 10;). It would rarely be neccessary
for the need to check for overflow,underflow but you are
able to do so because tp->tm_sec is defined by the Standard to be a
type int.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void ExitTimeFailure(void);

int main(void)
{
struct tm *tp;
time_t now, then;

if((now = time(NULL)) == (time_t)-1) ExitTimeFailure();
tp = localtime(&now);
tp->tm_sec += 10; /* Add 10 sec */
if((then = mktime(tp)) == (time_t)-1) ExitTimeFailure();
printf("The difference in time between then and now"
" is %.1f seconds\n", difftime(then, now));
return EXIT_SUCCESS;
}

void ExitTimeFailure(void)
{
puts("Unable to determine a time");
exit(EXIT_FAILURE);
}
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top