Solved

Summing up Time (eq. TIME + TIME = TOTAL TIME)

Posted on 2004-08-31
21
502 Views
Last Modified: 2010-04-05
How can i sum up time?
eq.

 8:00 + 8:00 + 8:00 = 24:00 hrs.
 8:30 + 8:10 + 8:10 = 24:50 hrs.
 8:45 + 8:15 + 8:00 = 25:00 hrs.
 8:30 + 8:30 + 8:30 = 25:30 hrs.

Any body can help?
0
Comment
Question by:girlswants_me
  • 6
  • 3
  • 2
  • +6
21 Comments
 

Author Comment

by:girlswants_me
Comment Utility
Anybody?
0
 

Author Comment

by:girlswants_me
Comment Utility
By the way, prioritize this one....

 a,b,c:Float;
 Total:String;

 a=strtotime('8:30);
 b=strtotime('8:30');
 c=strtotime('8:30');

 if a+b+c the answer should be '25:30 hrs' In the TOTAL

 The problem is this, I want to get the total hours rendered by an employee for a week.
 example: if an employee works 8 hours a day and 40 hours a week. this should be
  8:00 + 8:00 + 8:00 + 8:00 + 8:00 = 40:00 HOURS.

aNYBODY?
0
 

Author Comment

by:girlswants_me
Comment Utility
What becomes 8:00 in time 0.333333333333333 in float? Why is 8:00 = 0.333333333333333 in float? How does 0.333333333333333 becomes 8:00 in time?
0
 
LVL 5

Expert Comment

by:webtrans
Comment Utility
well i don't know any delphi but i will search google for you
but for the time being
can't u just split the time 8:30=8 and 30
and then 8:30=8+(30/60)
and so on
i guess this can solve ur problem for the time being at least
0
 
LVL 5

Expert Comment

by:webtrans
Comment Utility
did it help?
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type

  TMyTime = record
    Hours   : DWord;
    Minutes : DWord;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function SumTimes(Time1, Time2 : TMyTime) : TMyTime;
var
  tmp    : DWord;
begin
  Result.Minutes := Time1.Minutes + Time2.Minutes;
  tmp := Result.Minutes div 60;
  Result.Hours := Time1.Hours + Time2.Hours;
  if tmp > 0 then
    Result.Hours := Result.Hours + tmp;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Time1, Time2, Time3 : TMyTime;
begin
  Time1.Hours   := 8;   Time2.Hours   := 8;
  Time1.Minutes := 0;   Time2.Minutes := 0;
  Time3 := SumTimes(Time1, Time2);
  ShowMessage(IntToStr(Time3.Hours) + ':' + IntToStr(Time3.Minutes));
end;

end.
0
 
LVL 12

Expert Comment

by:Ivanov_G
Comment Utility
opps, I implemented it for 2 times, but the same idea you can use to sum 3 times...
0
 

Author Comment

by:girlswants_me
Comment Utility
What becomes 8:00 in time 0.333333333333333 in float? Why is 8:00 = 0.333333333333333 in float? How does 0.333333333333333 becomes 8:00 in time?
0
 

Author Comment

by:girlswants_me
Comment Utility
I have this

 HRS      MINS
  72       108
 
How will i convert the 108 mins to hours? is it
 108 / 60 = 1.8
Where is the hours there and the minute?
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility

8 H = 8/24 = 1/3 = 0.33333333333333333
1.00 = 1 Day = 24 Hours

procedure TForm1.Button1Click(Sender: TObject);
var t1,t2: TDateTime;
begin
  t1 := 8/24 +30/(24*60);   This is 8:30
  t2 := 8/24 +45/(24*60);   This is 8:45
  Label1.Caption := TimeToStr(t1+t2)
end;
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:mottor
Comment Utility
108 M = 108/(24*60) Days
and then use TimeToStr
if you want hours only  trunc(t1*24)


Best Regards
Mottor
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
unit Unit1_Q_21113312;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    EditSumTime: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private    { Private declarations }
  public     { Public declarations }
    TimeArray: array of string;
    function Sum_Up_Minutes: string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.Sum_Up_Minutes: string;
var
  B:     Boolean;
  C:     Integer;
  I:     Integer;
  P:     Integer;
  HI:    Integer;
  MI:    Integer;
  MINS:  Integer;
  HS:    string;
  MS:    string;
begin
  MINS := 0;
  try
    I := Length(TimeArray);
    for I := 0 to I-1 do
    begin
      P := Pos(':', TimeArray[I]);
      HS := Trim(Copy(TimeArray[I], 1, P-1));
      MS := Trim(Copy(TimeArray[I], P+1, 2));
      Val(HS, HI, C);
      B := (C=0);
      if B then
      begin
        Val(MS, MI, C);
        B := (C=0);
        if B then
        begin
          MINS := MINS + HI*60 + MI;
        end;
      end;
    end;
  finally
    Result := IntToStr(MINS div 60) + ':' + IntToStr(MINS mod 60);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetLength(TimeArray, 3);
  TimeArray[0] := '08:30';
  TimeArray[1] := '08:10';
  TimeArray[2] := '08:10';
  EditSumTime.Text := Sum_Up_Minutes;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SetLength(TimeArray, 0);
end;

end.
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
Look also the help for
DecodeTime
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
have some routines at home, will provide it later
0
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 250 total points
Comment Utility

The strToTime function returns a variable of type TDateTime. A TDatetime type variable in delphi represents time as follows:
- The integer part is the number of days that have passed since 30-Dec-1899
- The fractional part is the fraction of a 24 hour day which has passed

So, 8 hours is represented by a TDateTime value of 0.33333333333333 because 8 / 24 = 0.333333333333

So, if you want to know the time elapsed between 2 DateTime variables you could do something like this:

procedure TForm1.Button2Click(Sender: TObject);
var Date1,Date2: TDateTime;
    s: string;
    days,hr,min: integer;
begin
  Date1:= Now - 1.258; //i.e. 1 and a quarter days earlier
  Date2:= Now;
  days:= Trunc(Date2-Date1);
  hr:= Round(Frac(Date2-Date1)*24);
  min:= Round(Frac(Date2-Date1)*24*60)-hr*60;
  s:= IntToStr(days)+' days '
               + IntToStr(Round(Frac(Date2-Date1)*24*60))+'Minutes';
  s:= IntToStr(Trunc(Date2-Date1) )+' days '#13
               + IntToStr(hr)+' hours'#13
               + IntToStr(min)+ 'minutes';
  ShowMessage(s);
end;
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
> HRS      MINS
>  72       108

var
  MINSUM: Word;
begin
  MINSUM := HRS * 60 + MINS;
  S := (MINSUM div 60) + ':' + (MINSUM mod 60);
end;
0
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
why do it the easy way when you can complicate it right ? ugh ... might as well write it in asm

anyway:

uses DateUtils;

procedure TForm1.SomeMethod;
var TimeAll, TimeOne, TimeTwo, TimeThree: TTime;
    Hours, Minutes: Integer;
begin
     // just some random times
     TimeOne:=EncodeTime(Random(9), Random(60), 0, 0);
     TimeTwo:=EncodeTime(Random(9), Random(60), 0, 0);
     TimeThree:=EncodeTime(Random(9), Random(60), 0, 0);
     // just add them up
     TimeAll:=TimeOne + TimeTwo + TimeThree;
     Hours:=HoursBetween(TimeAll, 0);
     Minutes:=MinutesBetween(TimeAll, 0) - (Hours * 60);
     Caption:=Format('%d:%.2d', [Hours, Minutes]);
end;
0
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
Convert the times to TDateTime types, add them up, then convert the result back to string... Here's a function to sum up a list of times:

function SumOfTimes( const Times: array of string ): string;
var
  I: Integer;
  Total: TDateTime;
  H, M, S, MS: Word;
begin
  Total := 0;
  for I := Low( Times ) to High( Times ) do
    Total := Total + StrToTime( Times[ I ] );
  if ( Total >= 1 ) then begin
    DecodeTime( Total, H, M, S, MS );
    H := H + 24 * Trunc( Total );
    if ( M < 10 ) then begin
      Result := IntToStr( H ) + ':0' + IntToStr( M );
    end
    else begin
      Result := IntToStr( H ) + ':' + IntToStr( M );
    end;
  end
  else begin
    Result := TimeToStr( Frac( Total ) ) + ' hrs.';
  end;
end;

Usage: S := SumOfTimes( [ '8:00', '8:00', '8:00' ] );

Quite simple, actually.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
well, my small sample
just to say, that i don't use the TDatetimeType,
because the FracPart is not exact enough for Hours and Minutes,
under some conditions a minute is missed or added

unit TimeAdding_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


//converts a Double to an TimeString
//following Conversion before decimal = Hour, frac = Minutes
//fillto adds '0' before
Function FloatToTime(AFloat : Double; fillTo : Integer = 0) : String;
var
  v : Double;
  s : String;
begin
  Result := IntToStr(Trunc(AFloat))+':';
  while length(Result)-1 < fillTo do result := '0'+result;
  v := (frac(AFloat)+0.001) * 60;
  s := IntToStr(Trunc(v));
  while length(s) < 2 do s := '0'+s;
  result := result + s;
end;

//converts a TimeString into a double
//following Conversion before decimal = Hour, frac = Minutes
//Check24 disallows higher Values than 24, if set to true
Function TimeToFloat(AString : String; Check24 : Boolean = false) : Double;
var
  p : integer;
  v : Double;
begin
  If AString = '' then
    result := 0
  else
  begin
    p := pos(':',AString);
    if p = 0 then
    try
      result := StrToFloat(AString);
    except
      raise exception.create('Invalid Timeformat');
    end
    else
    begin
      try
        result := StrToFloat(Copy(AString,1,p-1));
      except
        raise exception.create('Invalid Timeformat');
      end;
      try
        v := StrToFloat(Copy(AString,p+1,2));
        if v > 59 then
          raise exception.create('Invalid Timeformat');
        result := result + (v/60);
        if (check24) and (result > 24) then
          raise exception.create('Invalid Timeformat');
      except
        raise exception.create('Invalid Timeformat');
      end;
    end;
  end;
end;

//usage sample
procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit5.text := FloatToTime(
                  TimeToFloat(edit1.text)+
                  TimeToFloat(edit2.text)+
                  TimeToFloat(edit3.text)+
                  TimeToFloat(edit4.text)
                 );
end;

end.

meikl ;-)
0
 

Author Comment

by:girlswants_me
Comment Utility
Thanks a lot. You got it right there!

So, 8 hours is represented by a TDateTime value of 0.33333333333333 because 8 / 24 = 0.333333333333

I will sum up all real numbers (TIME) then multiply it by 24 and i will get the total hours rendered. Trancate the Hours then the remaining decimals will be divided by 60.


0
 
LVL 14

Expert Comment

by:Pierre Cornelius
Comment Utility
You've got it. You could also take it further into seconds.

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now