Checking time_t addition for overflow

Discussion in 'C Programming' started by Ian Pilcher, Feb 6, 2005.

  1. Ian Pilcher

    Ian Pilcher Guest

    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
    ========================================================================
    Ian Pilcher, Feb 6, 2005
    #1
    1. Advertising

  2. Ian Pilcher

    Jason Wells Guest

    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|
    Jason Wells, Feb 6, 2005
    #2
    1. Advertising

  3. Ian Pilcher

    Ian Pilcher Guest

    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
    ========================================================================
    Ian Pilcher, Feb 6, 2005
    #3
  4. Ian Pilcher

    Alex Fraser Guest

    "Ian Pilcher" <> wrote in message
    news:...
    > 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
    Alex Fraser, Feb 6, 2005
    #4
  5. Ian Pilcher

    Jason Wells Guest

    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|
    Jason Wells, Feb 7, 2005
    #5
  6. 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
    Lawrence Kirby, Feb 7, 2005
    #6
  7. 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
    Lawrence Kirby, Feb 7, 2005
    #7
  8. Ian Pilcher

    Al Bowers Guest

    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/
    Al Bowers, Feb 7, 2005
    #8
  9. Ian Pilcher

    Ian Pilcher Guest

    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
    ========================================================================
    Ian Pilcher, Feb 7, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor
    Replies:
    4
    Views:
    582
    James Hu
    Sep 23, 2003
  2. Andy
    Replies:
    34
    Views:
    1,357
    Kelsey Bjarnason
    Jan 1, 2004
  3. Arin Chaudhuri

    checking for overflow

    Arin Chaudhuri, Jun 5, 2004, in forum: C Programming
    Replies:
    3
    Views:
    994
    Eric Sosman
    Jun 7, 2004
  4. jacob navia

    Checking overflow

    jacob navia, Jul 8, 2004, in forum: C Programming
    Replies:
    0
    Views:
    266
    jacob navia
    Jul 8, 2004
  5. Replies:
    0
    Views:
    349
Loading...

Share This Page