datetime - time difference's seems like working odd? -
i going find difference between 2 times not getting want!!! have 2 timeedit components in form here code:
void __fastcall tform1::button1click(tobject *sender) { ttime time1=strtotime(t1->text); ttime time2=strtotime(t2->text); //t1->text=time2-strtotime("3:00"); showmessage((time2-time1).timestring()); }
if set t1 = 02:00 , set t2 = 01:00
it shows 1:00
but expect 23:00 01:00 - 02:00 should 23:00
where wrong ?
you not taking account how ttime
encoded. tdatetime
double
, integral portion contains number of days since dec 30 1899, , fractional portion contains percentage of 24-hour day (this information stated in c++builder documentation). ttime
fractional portion of tdatetime
integral portion ignored. because of encoding, performing such seemingly simple mathematical operations on date/time values not produce kind of result expecting.
02:00
(2 am) represented 0.083333333333
, , 01:00
(1 am) represented 0.041666666667
. subtracting 2 1 am, expecting subtract 2 hours produce 11 pm (which represented 0.958333333333333
). subtracting 0.083333333333
0.041666666667
produces -0.041666666667
. ignoring integral portion (the date), fractional portion positive value 0.0
(midnight), -0.041666666667
equivilent 0.041666666667
, 1 am.
in order subtraction work correctly, starting time needs positive integral portion (date) attached result contains correct fractional portion, eg:
void __fastcall tform1::button1click(tobject *sender) { ttime t = (1.0 + strtotime("01:00")) - strtotime("02:00"); // (1.0 + 0.041666666667) - 0.083333333333 // = 1.041666666667 - 0.083333333333 // = 0.95833333333 showmessage(t.timestring()); }
Comments
Post a Comment