Problem with MOD and Integer.

Posted on 2004-08-05
Last Modified: 2010-08-05
Anyone have a solution to this problem..

Function Tform1.BuildTime(TimeVal: Longint): string;
 BuildStr: String;
 RunTime: Longint;
 RSec,Rhours,Rmins,Rdays: Integer;
     RDays:=Trunc(RunTime/60/60/24) mod 24 ;
     If (Rdays>1) or (Rdays=0) Then
        BuildStr:=BuildStr+' '+IntToStr(Rdays)+' days, '
        BuildStr:=BuildStr+' '+IntToStr(Rdays)+' day, ';
     RHours:=Trunc(RunTime/60/60) mod 24;
      If (RHours>1) or (RHours=0) Then
        BuildStr:=BuildStr+IntToStr(Rhours)+' Hours, '
        BuildStr:=BuildStr+IntToStr(Rhours)+' Hour, ';
     RMins:=Trunc(RunTime/60) mod 60;
       If (RMins>1) or (RMins=0) Then
       BuildStr:=BuildStr+IntToStr(RMins)+' Minutes, '
       BuildStr:=BuildStr+IntToStr(RMins)+' Minute, ';
    RSec:=Trunc(RunTime) Mod 60;
    If (RSec>1) or (RSec=0) Then
     BuildStr:=BuildStr+IntToStr(RSec)+' Seconds.'
     BuildStr:=BuildStr+IntToStr(RSec)+' Second.';

//some button
//this gets how long your machine has been running for from
//get tick count

Problem is, after 22 days or so, MOD fails and goes back to 0

try  60 * 60 * 24 * 30  //60 seconds * 60 minutes * 24 hours * 30 days

And the result should be 30 day, the routine screws up giving something like 4 days, anyone know how solve this?
Question by:tobjectpascal
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 26

Assisted Solution

by:Russell Libby
Russell Libby earned 100 total points
ID: 11725864

Seems like RDays should be the following

RDays:=Trunc(RunTime / 60 / 60 / 24) ;

Where instead, you are dividing by 24 and then mod'ing by 24 (dividing will get the number of days, mod'ing will return the hours)

LVL 12

Accepted Solution

esoftbg earned 25 total points
ID: 11726264
as rllibby wrote above:

function  Tform1.BuildTime(TimeVal: Int64): string;
  BuildStr:      string;
  RunTime:       Int64;
  Rmins,Rdays:   Int64;
  BuildStr := '';
  RunTime := TimeVal;
  RDays := Trunc(RunTime/60/60/24);
  if (Rdays=1) then
    BuildStr := BuildStr+' '+IntToStr(Rdays)+' day, '
    BuildStr := BuildStr+' '+IntToStr(Rdays)+' days, ';
  RHours := Trunc(RunTime/60/60) mod 24;
  if (RHours=1) then
    BuildStr := BuildStr+IntToStr(Rhours)+' Hour, '
    BuildStr := BuildStr+IntToStr(Rhours)+' Hours, ';
  RMins := Trunc(RunTime/60) mod 60;
  if (RMins=1) then
    BuildStr := BuildStr+IntToStr(RMins)+' Minute, '
    BuildStr := BuildStr+IntToStr(RMins)+' Minutes, ';
  RSec := Trunc(RunTime/1) mod 60;
  if (RSec=1) then
    BuildStr := BuildStr+IntToStr(RSec)+' Second.'
    BuildStr := BuildStr+IntToStr(RSec)+' Seconds.';
  Result := BuildStr;

The poits are for rllibby, I just wrote it as code

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question