473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

rolling back 24 hour times

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
Nov 13 '05 #1
5 1994
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

Nov 13 '05 #2
"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.
Nov 13 '05 #3


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

Nov 13 '05 #4

"Al Bowers" <xa******@rapidsys.com> wrote in message
news:bo*************@ID-169908.news.uni-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: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


That was very useful, thanks all.
Nov 13 '05 #5
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.

--
Er*********@sun.com
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Marcus | last post by:
Hello, Say I have a function with 5 MySQL queries that all insert or update the database. Now let's assume that during execution, the first 3 queries are executed fine but then the 4th one dies...
0
by: M Wells | last post by:
Hi All, I don't know if this is a truly esoteric question, or not, but I'm wondering how (or even if) you handle a timeout on a transaction within a stored procedure executed in a Stored...
1
by: Andy Hall | last post by:
Hi, I am rolling back a transaction that (up to the rollback command) has inserted into two tables, both of which are type InnoDB. I am getting the warning: "Warning: Some non-transactional...
5
by: Tom wilson | last post by:
I'm developing an application that is a benefits enrolment website. The benefits can be of any type in any order as stored in a SQL server. On each page is a Back and Next button. At a certain...
1
by: mike | last post by:
Is it possible to undo a transaction after it has completed (using 8 beta1) ---------------------------(end of broadcast)--------------------------- TIP 7: don't forget to increase your free...
1
by: cricketweb | last post by:
I have a stored procedure that calls another stored procedure with the first stored procedure opening a transaction: BEGIN SET XACT_ABORT ON BEGIN TRANSACTION does various updates/inserts
4
by: brendan.wong | last post by:
hello. i'm trying to incorporate error handling into my application, but i've run into a dilemma. i've already performed 10 successful INSERTS, but on the 11th INSERT, the application fails for...
8
by: valentin tihomirov | last post by:
Once file is open, it should be closed: Stream fs = new FileStream(filename, FileMode.Create); try { // write, exceptions may raise here } finally { fs.Close(); }
1
by: sreemati | last post by:
Hi Everyone, In brief, I am trying to get the whole block of code done in one go or not to do at all. Here is a piece of code I am trying: BEGIN TRANSACTION INSERT INTO authors...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.