title: In Delphi: How do I round a TDateTime to closest second, minute, five-minute etc?
body: <p>Does there exist a routine in Delphi that rounds a TDateTime value to the closest second, closest hour, closest 5-minute, closest half hour etc? </p>
  
  <p>UPDATE:</p>
  
  <p>Gabr provided an answer. There were some small errors, possibly due to the complete lack of testing ;-)</p>
  
  <p>I cleaned it up a bit and tested it, and here's the final(?) version:</p>
  
  <pre><code>function RoundDateTimeToNearestInterval(vTime : TDateTime; vInterval : TDateTime = 5*60/SecsPerDay) : TDateTime;
  var
    vTimeSec,vIntSec,vRoundedSec : int64;
  begin
    //Rounds to nearest 5-minute by default
    vTimeSec := round(vTime * SecsPerDay);
    vIntSec := round(vInterval * SecsPerDay);
  
    if vIntSec = 0 then exit(vTimeSec / SecsPerDay);
  
    vRoundedSec := round(vTimeSec / vIntSec) * vIntSec;
  
    Result := vRoundedSec / SecsPerDay;
  end;
  </code></pre>