Ooops... little overflow bug
My computer has reached 33 days of uptime today and with

var
    t, d, h, m, s: Integer;

you switch to the negative side ...
I got a display like : -8:-4:-52

Replacing with QWord solved the problem (I imagine DWord would just allow for something like 66 days)
var
   t, d, h, m, s: QWord;

I found this bug because incidentally I had the idea to have a look at the Ubuntu version of uptime which is installed on my system with Windows Subsystem for Linux. This uptime doesn't match the value of GetTickCount64, you just get the uptime since you clicked on the Ubuntu icon in Windows ...

Here is the modified code :

program uptime;

function GetTickCount64: QWord; stdcall; external 'kernel32.dll';

function _2d( _i: Integer): String;
begin
     Str( _i, _2d);
     if Length(_2d) < 2 then _2d:= '0'+_2d;

end;

function FormatUpTime( _tc: QWord): String;
var
   t, d, h, m, s: QWord;
   sd: String;
begin
     t:= _tc div 1000;
     s:= t mod 60;
     m:= (t div 60) mod 60;
     h:= (t div 3600) mod 24;
     d:= (t div 86400);

     FormatUpTime:= _2d(h)+':'+_2d( m)+':'+_2d( s);
     if 0 = d then exit;

     Str( d, sd);
     FormatUpTime:= sd+' '+FormatUpTime;
end;

begin
     WriteLn( FormatUpTime( GetTickCount64));
end.


_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to