Duration accumulator

I store on MySQL / MariaDB database a duration of type Time.

I read it from Databaserow this way:
Self.duration = row.Column("duration").DateTimeValue

I show the value on my list control this way:

Var items() As Entities.TimesheetEntity = Entities.TimesheetEntity.Read
Var list As WebListBox = TimeSheetListBox

Var acc As ?? //pseudo code

list.RemoveAllRows

For each item As Entities.TimesheetEntity  in items
  list.AddRow (item.duration.ToString(DateTime.FormatStyles.None, DateTime.FormatStyles.Short) 
  acc = acc + duration //pseudo code
)
Next
mylabel.text = "total duration: " acc.toString //pseudo code

My problem is how can I make an accumulator and display the total duration after my loop?

datetime and dateinterval class?

I don’t know if this is exactly what you’re looking for, but if I want to measure the time duration of a particular operation, I do the following:

// get duration in milliseconds
// set the start value before the code to be measured
Var start As Double = System.Microseconds / 1000

//  <== code to time goes here ==>

// now calculate the duration and display it
Var duration As Double = ((System.Microseconds / 1000) - start) / 1000
System.DebugLog(CurrentMethodName + " Code execution took: " + duration.ToString(Locale.Raw, "#0.000š˜“"))

Note: the above code was copied from elsewhere on the forum, and sorry I don’t remember from who for the credit.

Sorry @Scott_C I saw this code but it’s not responding to my question. My start value is a TIME Type from MYSQL

Sorry, I didn’t understand that part.

So is item.duration a period of time (like number of seconds), or just a date time stamp?

Assuming that the entries are in order by time, you would just use the first entry and last entry to see how much time has passed between the two. No need to add them up in a loop.

dim totalDuration as DateInterval = lastDateTime - firstDateTime

If they aren’t in order, then you can figure that part out in your loop.

dim firstDateTime as DateTime
dim lastDateTime as DateTime

for each item as Entities.TimesheetEntity in items
  
  if firstDateTime = nil or item.duration < firstDateTime then
    firstDateTime = item.duration
  end
  
  if lastDateTime = nil or item.duration > lastDateTime then
    lastDateTime = item.duration
  end
  
next

I think OP is saving timed events and wants to sum all.

Hmmm, if that’s the case then the duration of each event can be retrieved from the database in seconds rather than as a DateTime

SELECT TIME_TO_SEC(your_time_column) AS total_seconds FROM your_table

Then the seconds can be added up in the loop

2 Likes

i guess it read the date as 1.1.1970
and all seconds start from there with property .SecondsFrom1970

to have a date to add durations i would start with zero datetime.
Constructor secondsFrom1970 As Double, timeZone As TimeZone = Nil

1 Like

Thanks @Jared_Feder