By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,017 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

Checking time_t addition for overflow

P: n/a
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!

--
================================================== ======================
Ian Pilcher i.*******@comcast.net
================================================== ======================
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Ian Pilcher wrote:
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...
--
WWW: http://haywire.csuhayward.edu/~jwells2
spinmaster ._________________________________________________ ______.
@ |When you are right you cannot be too radical; |
gmail.com |when you are wrong, you cannot be too conservative. MLK|
Nov 14 '05 #2

P: n/a
Jason Wells wrote:

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.

--
================================================== ======================
Ian Pilcher i.*******@comcast.net
================================================== ======================
Nov 14 '05 #3

P: n/a
"Ian Pilcher" <i.*******@comcast.net> wrote in message
news:X-********************@comcast.com...
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
Nov 14 '05 #4

P: n/a
Ian Pilcher wrote:
Jason Wells wrote:

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.

But if time() can return -1 on error, you get a signed int...
--
WWW: http://haywire.csuhayward.edu/~jwells2
spinmaster ._________________________________________________ ______.
@ |When you are right you cannot be too radical; |
gmail.com |when you are wrong, you cannot be too conservative. MLK|
Nov 14 '05 #5

P: n/a
On Mon, 07 Feb 2005 04:26:50 +0000, Jason Wells wrote:
Ian Pilcher wrote:
Jason Wells wrote:

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.

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


Nov 14 '05 #6

P: n/a
On Sun, 06 Feb 2005 15:33:28 -0600, Ian Pilcher wrote:
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
Nov 14 '05 #7

P: n/a


Ian Pilcher wrote:
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);
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #8

P: n/a
Al Bowers wrote:
Arithmetic on two time_t variables or adding other values to a time_t
is not the intent of Standard C.


Thanks!

--
================================================== ======================
Ian Pilcher i.*******@comcast.net
================================================== ======================
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.