Sum of time 
Author Message
 Sum of time

How can I sum time ?
i.e. I want to add 12:25:35 to 18:35:45
and get a result in  hhhh:mm:ss  format,
how can I do that
Thanks in advance for your help.
Henk



Fri, 24 Nov 2000 03:00:00 GMT  
 Sum of time

Quote:

> How can I sum time ?  i.e. I want to add 12:25:35 to 18:35:45 and get a
> result in hhhh:mm:ss format, how can I do that

I'm sure you'll find plenty of time oriented functions on The Oasis
(http://www.iag.net/~philb/), however, given the above spec, and not
including any error checking and the like, you should be able to get away
with something like (off the top of my head):

----------------------------------------------------------------------------
Function AddTime( cTime1, cTime2 )
Local nSecs := val( substr( cTime1, 7 ) ) + val( substr( cTime2, 7 ) )
Local nMins := val( substr( cTime1, 4 ) ) + val( substr( cTime2, 4 ) )
Local nHrs  := val( cTime1 )              + val( cTime2 )

   If nSecs > 59
      nMins += int( nSecs / 60 )
      nSecs %= 60
   EndIf

   If nMins > 59
      nHrs  += int( nMins / 60 )
      nMins %= 60
   EndIf

Return( alltrim( str( nHrs ) )        + ":" + ;
        padl( int( nMins ), 2, "0" )  + ":" + ;
        padl( int( nSecs ) , 2, "0" ) )
----------------------------------------------------------------------------

Note that the above expects to get time in a HH:MM:SS format, you'll
probably want to expand it so that it can take the result of the above and
add to it (which could be a HHH:MM:SS or HHHH:MM:SS format, etc, you get the
idea...).

You might also want to check the TIME.PRG example file that comes with
Clipper (you'll find it in the ${CLIPPER}\SOURCE\SAMPLES directory), this
contains some handy time oriented functions (including TimeIsValid() which
you'll probably find handy to complete the above code).

Hope this helps.

--
Take a look in Hagbard's World: |     w3ng - The WWW Norton Guide reader.
http://www.acemake.com/hagbard/ |  ng2html - The NG to HTML converter.
http://www.hagbard.demon.co.uk/ |       eg - Norton Guide reader for Linux.
Free software, including........|   dgscan - DGROUP scanner for Clipper.



Fri, 24 Nov 2000 03:00:00 GMT  
 Sum of time

Quote:

> How can I sum time ?
> i.e. I want to add 12:25:35 to 18:35:45
> and get a result in  hhhh:mm:ss  format,
> how can I do that

Henk, would you want the result to be

07:01:20

or

31:01:20 ?

If the latter; convert the times to seconds, add them and convert them back to
hh:mm:ss.

If the former; do the same but substract 24 from the hh part if it's > 23.
(or something along these lines anyway :)

Anne.



Fri, 24 Nov 2000 03:00:00 GMT  
 Sum of time

Quote:
>How can I sum time ?
>i.e. I want to add 12:25:35 to 18:35:45
>and get a result in  hhhh:mm:ss  format,
>how can I do that

The first think Ill make its a function to get hh,mm,ss from time string.

Then make the sum


s = s1+s2
m = m1+m2
t = t1+t2

Then if s1 or m1 ar greater than 60 you must carry the excess.

For example :
s = s % 60
m += int( s / 60    )

...

Un saludo desde Mallorca.



Fri, 24 Nov 2000 03:00:00 GMT  
 Sum of time

Quote:
Dave Pearson wrote...


Thank you very much Dave, I already incorparated your suggestion.
It works very fine.

Henk.

Quote:
>> How can I sum time ?  i.e. I want to add 12:25:35 to 18:35:45 and get a
>> result in hhhh:mm:ss format, how can I do that

>>--------------------------------------------------------------------------
--
>Function AddTime( cTime1, cTime2 )
>Local nSecs := val( substr( cTime1, 7 ) ) + val( substr( cTime2, 7 ) )
>Local nMins := val( substr( cTime1, 4 ) ) + val( substr( cTime2, 4 ) )
>Local nHrs  := val( cTime1 )              + val( cTime2 )

>   If nSecs > 59
>      nMins += int( nSecs / 60 )
>      nSecs %= 60
>   EndIf

>   If nMins > 59
>      nHrs  += int( nMins / 60 )
>      nMins %= 60
>   EndIf

>Return( alltrim( str( nHrs ) )        + ":" + ;
>        padl( int( nMins ), 2, "0" )  + ":" + ;
>        padl( int( nSecs ) , 2, "0" ) )
>---------------------------------------------------------------------------
-



Sat, 25 Nov 2000 03:00:00 GMT  
 Sum of time

Hi,

Try to convert each time into seconds, add the seconds, convert the seconds
back into a time. You can use 2 functions in CA-TOOLS.

Peter

Quote:

>How can I sum time ?
>i.e. I want to add 12:25:35 to 18:35:45
>and get a result in  hhhh:mm:ss  format,
>how can I do that
>Thanks in advance for your help.
>Henk



Sat, 25 Nov 2000 03:00:00 GMT  
 Sum of time

Quote:

> >How can I sum time ?
> >i.e. I want to add 12:25:35 to 18:35:45
> >and get a result in  hhhh:mm:ss  format,
> >how can I do that

> The first think Ill make its a function to get hh,mm,ss from time string.

> Then make the sum


> s = s1+s2
> m = m1+m2
> t = t1+t2

> Then if s1 or m1 ar greater than 60 you must carry the excess.

> For example :
> s = s % 60
> m += int( s / 60    )

> ...

> Un saludo desde Mallorca.

But you've reassigned s before the second statement which uses s:
s = s % 60
m += int( s / 60    )

Should be

m += int( s / 60    )
s = s % 60

I think.



Fri, 15 Dec 2000 03:00:00 GMT  
 Sum of time
Quote:
>But you've reassigned s before the second statement which uses s:
>s = s % 60
>m += int( s / 60    )

>Should be

>m += int( s / 60    )
>s = s % 60

>I think.

You are rigth!!

Im surprised that writing code by heart I dont made more mistakes ;-)

Un saludo desde Mallorca.



Mon, 25 Dec 2000 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Help Summing a Column of Times

2. #sum and #sum:

3. The sum of a time-field (@t1)

4. Using SUM with Date/Time datatype

5. Bug in time.c (was Time.times problems)

6. time and scheduling (was: bug report: [ #447945 ] time.time() is not non-decreasing)

7. time zones, daylight saving time, and universal time

8. Using expand and filling rows and columns with sums

9. Fun trick summing letters

10. Fun trick summing letters but?

11. Summing columns of matrices

12. Two-Person Zero-Sum Game

 

 
Powered by phpBB® Forum Software