Midpoint of two time intervals

Thread Starter

RG23

Joined Dec 6, 2010
304
How to find the Midpoint of two time intervals?

For example if first time is 9:00:01 and second time is 10:00:02 then it should calculate the midpoint accurately
 

Thread Starter

RG23

Joined Dec 6, 2010
304
HRS: 13
MINS:48
SECS:54

HRS1:13
MINS1:17
SECS1:39

Then the midpoint as per excel sheet comes out to be 13:33:16

When you said add them and divide by two how will it work in this case?
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,225
But you must use sexagesimal (base 60) numbers when you do this.
Rich (BB code):
Example
  10:02:00
 - 8:56:45
-----------
   1:05:15

  10:07:22
 + 8:56:45
-----------
   19:04:07

19:04:07 / 2 = 09:28:22.5 = 09:32:03:30

I've expressed the half second as 30 jiffies
to be consistent with the sexagesimal representation.
http://en.wikipedia.org/wiki/Jiffy_(time)
see the electronics sub-heading
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,225
HRS: 13
MINS:48
SECS:54

HRS1:13
MINS1:17
SECS1:39

Then the midpoint as per excel sheet comes out to be 13:33:16

When you said add them and divide by two how will it work in this case?
Your answer is rounded up. Excel knows how to do this. It converts date and time fields to an internal representation, does the arithmetic, and then converts back.

By hand I get 13:33:16:30 where the last 30 jiffies is the half second.

Rich (BB code):
Step 1:  Add the two times together using base 60 arrithmetic
   13:48:54
 + 13:17:39
------------
   26:65:93  sums should be modulo 60 with a carry
 = 27:06:33

Step 2: Divide by 2
27/2 : 06/2 : 33/2 = 13.5:03:16.5

Step 3: Convert fractions to the next lower group

13.5 hrs   = 13:30:00:00
03 mins    = 00:03:00:00
16.5 secs  = 00:00:16:30

Step 4: Add 'em up

Result = 13:33:16:30
Sorry for the delay I took my wife on a date this afternoon
 
Last edited:

Thread Starter

RG23

Joined Dec 6, 2010
304
By hand I get 13:33:16:30 where the last 30 jiffies is the half second

How did you do the calculation?

I am not clear
 

THE_RB

Joined Feb 11, 2008
5,438
Alternatively (if you don't want to do base 60 math);

1. convert both numbers from HHMMSS to seconds (some C compilers have a fuinction)
2. get the difference between the two numbers in seconds
3. halve the difference
4. add it to the low number
5. convert the result back to HHMMSS (if required)
 

Thread Starter

RG23

Joined Dec 6, 2010
304
I am using DS1307 RTC where the contents of time registers are in BCD format

So the numbers that get stored in the SECS and SECS5 registers are actually hex
When I try to perform arithmetic operations on these registers I am getting wrong results

Actually I want to treat those hex numbers as decimal to get desired results

Part of the computation for seconds
movlw h'59'
movwf SECS5

movlw h'58'
movwf SECS

movf SECS5,0
addwf SECS,1
movf SECS,0
movwf SECS7

movlw h'60'
subwf SECS7,0
btfsc CARRY
incf MINS,1
 

Papabravo

Joined Feb 24, 2006
21,225
@papabravo

I am not clear with the example you posted
Reread the example in the previous post since I made an edit to it rather than creating a new post. The general rule for adding seconds and minutes is that whenever you get a number greater than 60, you generate a carry of 1 minute or 1 hour as appropriate and reduce the sum by 60. For example if you add 50 seconds to 43 seconds and get 93 seconds that is the same as (93-60=33 seconds) plus a carry of 1 minute. A similar operation happens with borrow when you borrow from the hours column that is equivalent to 60 minutes.

I think you're just going to have to stare at it until you see it.
 

Thread Starter

RG23

Joined Dec 6, 2010
304
i did understand your example papabravo

thank you for that

But now I am having an issue

I am using DS1307 wherein the contents of time registers are in BCD format

So the data is actually in the hex format and somehow I want to interpret those hex values as decimal to perform further computations

I couldn't figure it out
 

Papabravo

Joined Feb 24, 2006
21,225
OK. Yeah BCD and Hex look exactly the same on paper because the representation of the digits is identical. The problem comes in when you use a binary ALU (Arithmetic Logic Unit). In the datasheet look for a "half carry" flag OR a "decimal adjust accumulator" instruction. These things will help you do BCD Arithmetic on a binary machine.

I found the following link which you might find helpful
http://mathforum.org/library/drmath/view/51901.html

BTW - what processor are you using? If I knew I could give more targeted advice.
 
Last edited:

John P

Joined Oct 14, 2008
2,026
Just be prepared to do something intelligent in the case where the earlier time is before midnight, and the later time is after it.
 
Top