Forum Discussion

btpoole's avatar
btpoole
Channel Surfer
9 years ago

Time Rounding

What would be the best method of round time, convert to Unix epoch first? Looking to round to nearest half hour thought maybe there would be a way with epoch versus getting the hour, minute etc.
Thanks

3 Replies

  • A half hour is 1800 seconds. So just round the Unix epoch time to the nearest multiple of 1800. This would be much more difficult to do using the time split up into date, hours and minutes etc. due to the problem of finding the date of the next or previous day if the rounded time crosses over into a different day.
  • btpoole's avatar
    btpoole
    Channel Surfer
    Thank you RokuMarkn had used the hours/mins but realized takes too much.
  • This is fun, thanks RokuMarkn for the pointer!
    Here is a function for it (disclaimer: typed but not tested), with optional custom hump value. Non-destructive, though you may see mutator sub more befitting. Depending which way you want to lean when right on the 15-min cusp, may have to fix `fix()` to `cint()`:
    function round_time(tm as object, cog = 1800) as object   ' roDateTime -> roDateTime '
      secs = tm.asSecodns()
      secs = fix(secs / cog + 0.5) * cog
      res = createObject("roDateTime")
      res.fromSeconds(secs)
      return res
    end function