Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
btpoole
Level 7

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
0 Kudos
3 Replies
RokuMarkn
Level 7

Re: Time Rounding

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.
0 Kudos
btpoole
Level 7

Re: Time Rounding

Thank you RokuMarkn had used the hours/mins but realized takes too much.
0 Kudos
EnTerr
Level 8

Re: Time Rounding

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
0 Kudos