delphi - Round TTime to nearest 15 minutes -


i have following function i'm led believe should round time nearest 15 minutes.

function tdmdata.roundtime(t: ttime): ttime; var h, m, s, ms : word; begin   decodetime(t, h, m, s, ms);   m := (m div 15) * 15;   s := 0;   result := encodetime(h, m, s, ms); end; 

to test function have put tbutton , tedit on form , @ click of button do:

begin   edit1.text := roundtime('12:08:27'); end; 

i error when compiling : 'incompatible types ttime , string'

any great.

thanks,

the error causes compilation failure passing string function needs ttime parameter.
once fixed, edit1.text needs string type function returns ttime.

using strtotime , timetostr can obtain desired conversion , string type.

your function can called this:

begin   edit1.text := timetostr(roundtime(strtotime('12:08:27')); end; 

stealing gabr user's answer - in delphi: how round tdatetime closest second, minute, five-minute etc? - can obtain date rounded arbitrary nearest value assigned interval parameter:

function roundtonearest(time, interval: tdatetime): tdatetime; var   time_sec, int_sec, rounded_sec: int64; begin   time_sec := round(time * secsperday);   int_sec := round(interval * secsperday);   rounded_sec := (time_sec div int_sec) * int_sec;   if ((rounded_sec + int_sec - time_sec) - (time_sec - rounded_sec)) > 0     rounded_sec := rounded_sec + time_sec + int_sec;   result := rounded_sec / secsperday; end; 

 

begin   edit1.text := timetostr(roundtonearest(strtotime('12:08:27'), strtotime('0:0:15'))); end; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -