Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
btpoole
Channel Surfer

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 3
RokuMarkn
Visitor

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
Channel Surfer

Re: Time Rounding

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

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