rolling back 24 hour times

Discussion in 'C Programming' started by marty, Nov 12, 2003.

  1. marty

    marty Guest

    As part of an assignment I need to calculate a 24 hour time by subtracting a
    one time from another e.g. if the user enters 1.00 as the time and 1.30 as
    the time to subtract the returned time is 23.30 the previous day.

    This is straightforward when dealing with positive times. But when negative
    times are introduced (as above) it all gets tricky.

    I'd originally thought that a lookup table of times against a corresponding
    negative time might be useful. But this wouldn't be flexible enough to
    handle anything later than 24 hours.

    Is there a mathematical way to calculate this? I don't want the code written
    for me, just a few ideas.

    TIA
     
    marty, Nov 12, 2003
    #1
    1. Advertising

  2. On Wed, 12 Nov 2003 08:48:14 +0000, marty wrote:

    > Is there a mathematical way to calculate this? I don't want the code written
    > for me, just a few ideas.


    Look at the modulo operator (%) in your textbook.

    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up
     
    Nils Petter Vaskinn, Nov 12, 2003
    #2
    1. Advertising

  3. marty

    Simon Biber Guest

    "marty" <rmartin@n^t^l^w^o^r^l^d^.^c^o^m^> wrote:
    > As part of an assignment I need to calculate a 24 hour time by
    > subtracting a one time from another e.g. if the user enters 1.00
    > as the time and 1.30 as the time to subtract the returned time
    > is 23.30 the previous day.

    [snip]
    > Is there a mathematical way to calculate this? I don't want the
    > code written for me, just a few ideas.


    You can regard both the time and the offset as an integer number of
    minutes before or after midnight, and just add the two integers.

    Then normalise the result into hours and minutes (first adding 24 hours
    to cancel out any negative values).

    Brought to you by the numbers 60 and 1440, and the operators +, *, /
    and especially %.

    --
    Simon.
     
    Simon Biber, Nov 12, 2003
    #3
  4. marty

    Al Bowers Guest

    marty wrote:
    > As part of an assignment I need to calculate a 24 hour time by subtracting a
    > one time from another e.g. if the user enters 1.00 as the time and 1.30 as
    > the time to subtract the returned time is 23.30 the previous day.
    >
    > This is straightforward when dealing with positive times. But when negative
    > times are introduced (as above) it all gets tricky.
    >
    > I'd originally thought that a lookup table of times against a corresponding
    > negative time might be useful. But this wouldn't be flexible enough to
    > handle anything later than 24 hours.
    >
    > Is there a mathematical way to calculate this? I don't want the code written
    > for me, just a few ideas.
    >


    You might be able to substract or add to bring the values in range.

    double time = 1.00; /* represents 1 am */

    time-=50.50; /* substact 50.5 hours */
    while(time < 24.0) time+=24.0;
    while(time > 24.0) time -=24.0;

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Nov 12, 2003
    #4
  5. marty

    marty Guest

    "Al Bowers" <> wrote in message
    news:botqjr$1hn89g$-berlin.de...
    >
    >
    > marty wrote:
    > > As part of an assignment I need to calculate a 24 hour time by

    subtracting a
    > > one time from another e.g. if the user enters 1.00 as the time and 1.30

    as
    > > the time to subtract the returned time is 23.30 the previous day.
    > >
    > > This is straightforward when dealing with positive times. But when

    negative
    > > times are introduced (as above) it all gets tricky.
    > >
    > > I'd originally thought that a lookup table of times against a

    corresponding
    > > negative time might be useful. But this wouldn't be flexible enough to
    > > handle anything later than 24 hours.
    > >
    > > Is there a mathematical way to calculate this? I don't want the code

    written
    > > for me, just a few ideas.
    > >

    >
    > You might be able to substract or add to bring the values in range.
    >
    > double time = 1.00; /* represents 1 am */
    >
    > time-=50.50; /* substact 50.5 hours */
    > while(time < 24.0) time+=24.0;
    > while(time > 24.0) time -=24.0;
    >
    > --
    > Al Bowers
    > Tampa, Fl USA
    > mailto: (remove the x to send email)
    > http://www.geocities.com/abowers822/
    >


    That was very useful, thanks all.
     
    marty, Nov 12, 2003
    #5
  6. marty

    Eric Sosman Guest

    Al Bowers wrote:
    >
    > marty wrote:
    > > As part of an assignment I need to calculate a 24 hour time by subtracting a
    > > one time from another e.g. if the user enters 1.00 as the time and 1.30 as
    > > the time to subtract the returned time is 23.30 the previous day.
    > >
    > > This is straightforward when dealing with positive times. But when negative
    > > times are introduced (as above) it all gets tricky.
    > >
    > > I'd originally thought that a lookup table of times against a corresponding
    > > negative time might be useful. But this wouldn't be flexible enough to
    > > handle anything later than 24 hours.
    > >
    > > Is there a mathematical way to calculate this? I don't want the code written
    > > for me, just a few ideas.
    > >

    >
    > You might be able to substract or add to bring the values in range.
    >
    > double time = 1.00; /* represents 1 am */
    >
    > time-=50.50; /* substact 50.5 hours */
    > while(time < 24.0) time+=24.0;
    > while(time > 24.0) time -=24.0;


    The fmod() function might be convenient here.

    Also, note that simple arithmetic will not always
    yield the correct result, because not all days are 24.0
    hours long. Many parts of the world observe a seasonal
    variation involving one 23-hour and one 25-hour day per
    year, and *all* of the world (except the POSIX committee,
    I've heard) admits the introduction or deletion of leap
    seconds twice yearly.

    --
     
    Eric Sosman, Nov 12, 2003
    #6
    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. Replies:
    2
    Views:
    882
    Hal Rosser
    Jun 1, 2007
  2. ssylee
    Replies:
    4
    Views:
    518
    Thad Smith
    Jan 1, 2008
  3. M Wells
    Replies:
    0
    Views:
    342
    M Wells
    Mar 3, 2004
  4. Chris Gunnels
    Replies:
    4
    Views:
    162
    Robert Klemme
    Sep 16, 2010
  5. Dan Jacobson

    add half an hour to all times in file

    Dan Jacobson, Sep 24, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    83
    John W. Krahn
    Sep 24, 2003
Loading...

Share This Page