Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Checking time_t addition for overflow

Reply
Thread Tools

Checking time_t addition for overflow

 
 
Ian Pilcher
Guest
Posts: n/a
 
      02-06-2005
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
================================================== ======================
 
Reply With Quote
 
 
 
 
Jason Wells
Guest
Posts: n/a
 
      02-06-2005
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|
 
Reply With Quote
 
 
 
 
Ian Pilcher
Guest
Posts: n/a
 
      02-06-2005
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
================================================== ======================
 
Reply With Quote
 
Alex Fraser
Guest
Posts: n/a
 
      02-06-2005
"Ian Pilcher" <> wrote in message
news:X-2dnez3ip60EJvfRVn-...
> 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


 
Reply With Quote
 
Jason Wells
Guest
Posts: n/a
 
      02-07-2005
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|
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      02-07-2005
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




 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      02-07-2005
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
 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      02-07-2005


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: (remove the x to send email)
http://www.geocities.com/abowers822/

 
Reply With Quote
 
Ian Pilcher
Guest
Posts: n/a
 
      02-07-2005
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
================================================== ======================
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
valarray addition and overflow handling siddharth.vaghela@gmail.com C++ 0 01-13-2006 12:48 PM
Checking overflow jacob navia C Programming 0 07-08-2004 06:04 PM
checking for overflow Arin Chaudhuri C Programming 3 06-07-2004 02:11 PM
unsigned short addition/subtraction overflow Andy C Programming 34 01-01-2004 01:57 AM
checking for stack overflow in recursive function Victor C Programming 4 09-23-2003 02:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57