Delphi hours operation

3

I was handling hours in Delphi and came across the following situation:

CASE 1

horafinal := strtotime('08:00');
horaInicial := strtotime('17:00');

horaInicial := horaInicial - horafinal; //09:00
horafinal := horafinal - horafinal; //00:00

diferenca := horafinal - horaIncial; // 09:00

CASE 2

horafinal := strtotime('08:00');
horaInicial := strtotime('17:00');

horaInicial := horaInicial - horafinal; //09:00
horafinal := strtotime('23:59') + strtotime('00:01'); //00:00

diferenca := horafinal - horaIncial; // 15:00

CASE 3

horafinal := strtotime('08:00');
horaInicial := strtotime('17:00');

horaInicial := horaInicial - horafinal; //09:00
horafinal := strtotime('00:00'); //00:00

diferenca := horafinal - horaIncial; // 15:00

In my understanding this occurs because internally it should use a datetime. Case 1 deadline = today 00:00 and Case 2 Tomorrow 00:00 and Case 3 deadline = Tomorrow 00:00

Is this thought correct? How does Delphi work with hours? Because in case 3 the delphi behaves the same as Case 2 and not equal to case 1?

    
asked by anonymous 06.03.2015 / 20:58

1 answer

5

Well, in all three cases you used a variable horaInicial and then horaIncial . I'll assume that the i missing is a silly typing error only and not a different variable.

See on this page for these details:

  

The date is set to 30 dec 1899, one day short of the 19th century.

     

Warning : the date value is set to 1 day short of the end of the 19th century. Exactly why is unclear.

Translating to Portuguese:

  

The date is set for 30 Dec 1899, one day before the end of the 19th century.

     

Caution : The date value is set to one day before the end of the 19th century. It is unclear why.

See this one :

  

It seems that the reason for Delphi starting at 30 Dec 1899 is to make it as compatible with Excel while at the same time adopting Excel's incorrectness about dates.

     

Because TDateTime is actually a double, you can perform calculations on it as if it were a number. This is useful for calculations such as the difference between two dates.

Translating to Portuguese:

  

It seems that the reason for Delphi to start on 30 Dec 1899 is to make it as compatible as possible with Excel without however adopting Excel's inaccuracy about dates.

     

Since TDateTime is actually a double, you can perform calculations on it as if it were a number. This is useful for calculations such as differences between two dates.

That is, a TDateTime is a double where the integer part is a number of days since 12/30/1899 and the fractional part is fractions of days (hours, minutes, seconds, etc.).

Let's replace your variables with a , b , c and d to understand what's going on and track the value of double s:

In your case 1, how the question was originally :

a := strtotime('08:00'); // Valor double: 8/24
b := strtotime('17:00'); // Valor double: 17/24

c := a - a; // 00:00 // Zero
d := b - c; // 09:00 // 17/24 - 0 = 17/24. Definitivamente não é 09:00!

diferenca := c - d; // -17/24, ou seja, 17 horas antes da meia-noite do dia 30/12/1899, portanto 09:00 do dia 29/12/1899.

In case 1, as the question is now:

a := strtotime('08:00'); // Valor double: 8/24
b := strtotime('17:00'); // Valor double: 17/24

c := b - a; // 09:00 // 17/24 - 8/24 = 9/24
d := a - a; // 00:00

diferenca := d - c; // -9/24, ou seja, 9 horas antes da meia-noite do dia 30/12/1899, portanto 15:00 do dia 29/12/1899.

In case 2:

a := strtotime('08:00'); // Valor double: 8/24
b := strtotime('17:00'); // Valor double: 17/24

c := b - a; // 09:00 // 17/24 - 8/24 = 9/24
d := strtotime('23:59') + strtotime('00:01'); // 00:00 // Mas no dia 31/12/1899. Valor double: 1.

diferenca := d - c; // 15:00 // Isso dá (1 - 9/24) = 15/24, ou seja, 15:00 do dia 30/12/1899.

In case 3:

a := strtotime('08:00'); // Valor double: 8/24
b := strtotime('17:00'); // Valor double: 17/24

c := b - a; // 09:00 // 17/24 - 8/24 = 9/24
d := strtotime('00:00'); // 00:00

diferenca := d - c; // 15:00 // Isso dá (0 - 9/24) = -9/24, ou seja, 9 horas antes da meia-noite do dia 30/12/1899, portanto 15:00 do dia 29/12/1899.

Conclusion: The way the question is now, the 3 cases give a 15:00 answer. However in case 1 of the question as it was originally, the result was 09:00 because you were doing operations in a different way.

    
06.03.2015 / 21:51