### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [SOLVED] Summing up the hours  (Read 420 times)

#### Pe3s

• Full Member
• Posts: 215
##### [SOLVED] Summing up the hours
« on: July 03, 2022, 09:46:18 pm »
Hello, I have a question about how you can add hours in format such as 07:00 + 15:00
« Last Edit: July 10, 2022, 11:58:51 am by Pe3s »

#### MarkMLl

• Hero Member
• Posts: 4783
##### Re: Summing up the hours
« Reply #1 on: July 03, 2022, 10:57:15 pm »
I can't see why you've posted this to the LCL part of the forum.

You'll find the runtime library described at https://www.freepascal.org/docs.html , including RTL -> DateUtils.

However if you are confident that you know that the format will always be hh:mm (or perhaps hh.dd for the "dismal hours" beloved by American corporates) you might find it more efficient to parse the input yourself to minutes or seconds relative to some arbitrary datum.

I'd throw in that I think there's an important point to be learnt from (SQL) databases here: hh:mm can either represent a time or an interval. You can add a time and an interval (giving you another time), you can add two intervals (giving you another interval), but you can't add two times.

Taking that into account, a time needs a datum and you need to consider what you do if it exceeds 24 hours, while an interval doesn't need a datum and can exceed 24 hours.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### wp

• Hero Member
• Posts: 9923
##### Re: Summing up the hours
« Reply #2 on: July 03, 2022, 11:54:23 pm »
how you can add hours in format such as 07:00 + 15:00
I am assuming that these times are something like "work times" and you want to get the total work time. Usually times are stored in TTime variables (floating point numbers < 1 which represents the fraction of a day (0.25 --> 6:00 hours). (If you have them as strings use the StrToTime function to get a TTime variable). You can simply add these numbers to get the total time. Eventually the sum will become > 1, and the integer part will be understood as full days. To represent the sum as a string use the FormatDateTime function with the format mask '[h]:nn' or '[h]:nn:ss' where the square bracket symbol allows hour values greater than 23 if the format option [fdoInterval] is used.

Code: Pascal  [Select][+][-]
1. program Project1;
2. uses
3.   SysUtils;
4. var
5.   times: array[0..4] of TTime;
6.   total: TTime;
7.   i: Integer;
8. begin
9.   times[0] := StrToTime('8:25');
10.   times[1] := StrToTime('7:45');
11.   times[2] := StrToTime('8:19');
12.   times[3] := StrToTime('7:22');
13.   times[4] := StrToTime('7:56');
14.   total := 0.0;
15.   for i := Low(times) to High(times) do
16.     total := total + times[i];
17.   WriteLn('Total time: ', FormatDateTime('[h]:nn', total, [fdoInterval]));
19. end.

#### y.ivanov

• Sr. Member
• Posts: 450
##### Re: Summing up the hours
« Reply #3 on: July 04, 2022, 01:40:36 am »
Hello, I have a question about how you can add hours in format such as 07:00 + 15:00
Are these string values represent time of the day or a duration? StrToTime() works in time of the day range an will raise an exception if >=24:00. If it is a duration, it is better to parse the two numbers by yourself and construct a TTime:

Code: Pascal  [Select][+][-]
1. function StrToDur(S: String): TTime;
2. begin // Assumes HH:MM and does not check validity nor MM is less than 60
3.   Result := StrToInt(Copy(S, 1, 2)) / 24.0 + StrToInt(Copy(S, 4, 2)) / 24.0 / 60.0;
4. end;
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

#### Pe3s

• Full Member
• Posts: 215
##### Re: [SOLVED] Summing up the hours
« Reply #4 on: July 04, 2022, 05:56:59 pm »
Thank you for your help