News:

  • October 18, 2026, 07:51:08 PM

Login with username, password and session length

Author Topic: Averaging TimeStamps  (Read 7147 times)

Bolt

  • Hero Member
  • *****
  • Posts: 598
Averaging TimeStamps
« on: January 15, 2021, 07:25:39 PM »
Say I have a MemoryBlock[V0].StopTime, an unix timestamp.  I would like to calculate the average .StopTime.  I can create a Task, Index Loop through the MemoryBlock, do an EPOCH2DT, calculate the (UDT0.Hours x 3600 + UDT0.Minute x 60 + UDT0.Seconds), and create an average "seconds into day" of that number.  It's kind of a kludge, especially when you want to do something with that number, need to do a bunch of modulus math to put it back into UDT1.Hours, Minutes, Seconds.  Also becomes even more tricky when sometimes are before midnight, and others after midnight.

Is there an easier way to do this?  Anybody have any tricks up their sleeves?
« Last Edit: January 15, 2021, 07:41:52 PM by Bolt »

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3850
    • Host Engineering
Re: Averaging TimeStamps
« Reply #1 on: January 16, 2021, 09:20:04 AM »
To be accurate, this must be done with integer MATH.

Take the first one and save it off as a base value.  Create a block of signed DWORDS the same size as your UDT.  Iterate through your UDT block, subtracting off the base value and storing each adjusted value in the corresponding index of the new block.

SUM up the new block, divide by the count to get THAT average as a signed integer.

Add that integer average back to your base value.  That is now the average unix date/time value.

Convert that EPOCH 2DT to get an actual time stamp

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3850
    • Host Engineering
Re: Averaging TimeStamps
« Reply #2 on: January 16, 2021, 09:25:41 AM »
This only works because your unix values should all be close together, hence the offset values will all be relatively small, able to sum them up as a signed DWORD with 32 bits of precision. If you  tried to sump up the original values, you would have to use floating pont, which only has 24 bits of precision.

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: Averaging TimeStamps
« Reply #3 on: January 16, 2021, 11:44:32 PM »
I guess I should clarify I'm looking for the average time of day, not average Unix time. I see where I mislead you, sorry. Each MemoryBlock.StopTime is daily, if you will. Say all around 23:00. Trying to guesstimate the next StopTime