Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!cs.utexas.edu!uunet!mcsun!hp4nl!dutrun!winfave
From: winfave@dutrun.UUCP (Alexander Verbraeck)
Newsgroups: comp.lang.pascal
Subject: Re: Algorithm for average time of day?
Message-ID: <898@dutrun.UUCP>
Date: 8 Sep 89 11:50:50 GMT
References: <1780003@hpcc01.HP.COM>
Reply-To: winfave@dutrun.UUCP (A.Verbraeck)
Organization: Delft University of Technology, The Netherlands
Lines: 53

In article <1780003@hpcc01.HP.COM> azarian@hpcc01.HP.COM (Randy Azarian) writes:
>Does anyone have an algorithm available that will calculate
>an average time of day?
>
>For example:
>   
>     4:30
>     5:30
>     -> average would be 5:00

I do not have a readily available algorithm, but I don't think it would
be very difficult writing one. You need two functions, one that
translates the TOD to the number of minutes since midnight and one that
translates the number of minutes to the TOD using div and mod functions.
It would look something like this:

{ Suppose the time is on a 24 hour clock (00:00 to 23:59)
  and it is coded as an hour field and a minutes field.
  If it is coded as a string, first split in hour and minutes.
}

function TODtoMinutes(Hour,Min:integer):integer;

begin
  TODtoMinutes:=Hour*60+Min;
end;

procedure MinutesToTOD(Input:integer;var Hour,Min:integer);

begin
  Hour := Input div 60;
  Min  := Input mod 60;
  { didn't think too much on this... am not sure it is right...
    always have problems with div and mod... }
end;

procedure AverageTimes(H1,M1,  H2,M2 : integer; var HO,MO:integer);

var T1,T2,T3 : integer;

begin
  T1:=TODtoMinutes(H1,M1);
  T2:=TODtoMinutes(H2,M2);
  T3:=(T1+T2) div 2;
  MinutesToTOD(T3,HO,MO);
end;

---------------------------------------------------------------------
Alexander Verbraeck                            e-mail:
Delft University of Technology                 winfave@hdetud1.bitnet
Department of Information Systems              winfave@dutrun.uucp
PO Box 356, 2600 AJ  The Netherlands
---------------------------------------------------------------------