how to sum hours?

jeronimovilar2

Member
Licensed User
Longtime User
how to sum hours?
I need to sum the hours day by day:
10:00:00
08:10:00
08:50:00
20:10:00
----------
47:10:00

how can i do this?

sorry my english
 

gregwa

Member
Licensed User
Longtime User
While this isn't the most elegant or scalable method you can use the following code. With a little bit of effort, you could modify it into a very nice subroutine set that will work with as many time entries as you need.

Call the DoIt routine.

I'll try to come up with something nicer in a day or two for you.

B4X:
Sub parse(timein As String) As Map
   Dim h,mi,s As String
   Dim m As Map
   h = timein.SubString2(0,2)
   mi = timein.SubString2(3,5)
   s = timein.SubString(6)
   m.Initialize
   m.Put("Hours",h)
   m.Put("Minutes",mi)
   m.Put("Seconds",s)
   Return m
End Sub
Sub DoIt
   Dim TotalTime As String
   Dim hours, Minutes, seconds As Int
   Dim m As Map
   
   m.Initialize
   m = parse("10:00:00")
   hours = m.Get("Hours")
   Minutes = m.Get("Minutes")
   seconds = m.Get("Seconds")
   m.Initialize
   m = parse("08:10:00")
   hours = hours + m.Get("Hours")
   Minutes = Minutes + m.Get("Minutes")
   seconds = seconds + m.Get("Seconds")
   If seconds > 60 Then
      seconds = seconds-60
      Minutes = Minutes + 1
   End If
   If Minutes > 60 Then
      Minutes = Minutes - 60
      hours = hours + 1
   End If
   m.Initialize
   m = parse("08:50:00")
   hours = hours + m.Get("Hours")
   Minutes = Minutes + m.Get("Minutes")
   seconds = seconds + m.Get("Seconds")
   If seconds > 60 Then
      seconds = seconds-60
      Minutes = Minutes + 1
   End If
   If Minutes > 60 Then
      Minutes = Minutes - 60
      hours = hours + 1
   End If   
   m.Initialize
   m = parse("20:10:00")
   hours = hours + m.Get("Hours")
   Minutes = Minutes + m.Get("Minutes")
   seconds = seconds + m.Get("Seconds")
   If seconds > 60 Then
      seconds = seconds-60
      Minutes = Minutes + 1
   End If
   If Minutes > 60 Then
      Minutes = Minutes - 60
      hours = hours + 1
   End If   
   TotalTime = NumberFormat(hours,2,0) & ":" & NumberFormat(Minutes,2,0) & ":" & NumberFormat(seconds,2,0)
   Log("TotalTime=" & TotalTime)
End Sub

how to sum hours?
I need to sum the hours day by day:
10:00:00
08:10:00
08:50:00
20:10:00
----------
47:10:00

how can i do this?

sorry my english
 
Upvote 0

gregwa

Member
Licensed User
Longtime User
Actually, I did something that is pretty much workable...

You can create a list of your times. Then using the gross logic from DoIt2, you can parse the list and keep a running total. That should work for just about anything you might need.

B4X:
Sub Activity_Create(FirstTime As Boolean)
   DoIt2
End Sub

Sub AddTwoTimes(time1 As String, time2 As String) As String
   Dim m As Map
   Dim hr1,mi1,se1,hr2,mi2,se2,thr,tmi,tse As String
   
   m.Initialize
   m = parse(time1)
   hr1 = m.Get("Hours")
   mi1 = m.Get("Minutes")
   se1 = m.Get("Seconds")
   m.Initialize
   m = parse(time2)
   hr2 = m.Get("Hours")
   mi2 = m.Get("Minutes")
   se2 = m.Get("Seconds")
   thr = hr1+hr2
   tmi = mi1+mi2
   tse = se1+se2
   If tse > 60 Then
      tse = tse-60
      tmi = tmi+1
   End If
   If tmi > 60 Then
      tmi = tmi-60
      thr = thr+1
   End If
   Return NumberFormat(thr,2,0) & ":" & NumberFormat(tmi,2,0) & ":" & NumberFormat(tse,2,0)
End Sub

Sub DoIt2
   Dim t1,t2,t3 As String
   t1 = AddTwoTimes("10:00:00","08:10:00")
   t2 = AddTwoTimes(t1,"08:50:00")
   t3 = AddTwoTimes(t2,"20:10:00")
   Log(t1)
   Log(t2)
   Log(t3)
End Sub

Sub parse(timein As String) As Map
   Dim h,mi,s As String
   Dim m As Map
   h = timein.SubString2(0,2)
   mi = timein.SubString2(3,5)
   s = timein.SubString(6)
   m.Initialize
   m.Put("Hours",h)
   m.Put("Minutes",mi)
   m.Put("Seconds",s)
   Return m
End Sub
 
Upvote 0
Top