Recent

Author Topic: DateUtils *Span and *Between functions  (Read 435 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 588
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
DateUtils *Span and *Between functions
« on: March 25, 2025, 04:24:02 pm »
I don't like doing TDateTime calculations like (B - A) * OneSecond or whatsoever, I like so much DateUtils so I would like to stick to this basic functions.

Apparently *Span and *Between functions only tell absolute distance between supplied dates, regardless of absolute ordering of time (no quantum mechanics or general relativity here, just classical world).

Is there in DateUtils or somewhere else equivalent functions that return the difference but with sign? I did not find anything.

I did my own but I find strange they don't exist in standard libraries.

Code: Pascal  [Select][+][-]
  1. function SecondsDifference(A, B: TDateTime): Double;
  2. begin
  3.   if CompareDateTime(A, B) <= 0 then
  4.     Result := DateUtils.SecondSpan(A, B);
  5.   else
  6.     Result := -DateUtils.SecondSpan(B, A);
  7. end;
  8.  
« Last Edit: March 25, 2025, 04:26:45 pm by Чебурашка »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Thaddy

  • Hero Member
  • *****
  • Posts: 16763
  • Ceterum censeo Trump esse delendam
Re: DateUtils *Span and *Between functions
« Reply #1 on: March 25, 2025, 11:07:30 pm »
Before you use such functions beware that if the times are not UTC, they won't work reliably, so in your case you must make sure that A and B are UTC in all cases. After that, A and B can be converted back to local times, even if they are in different time zones.
The reason is that e.g. SecondsDifference between time zones and or daylight saving time MUST be UTC to guarantee the function result is a simple subtraction. The seconds can be between normal local time and daylight savings time... Let alone if A and B are in different time zones.

So convert A and B to UTC, after calling the function convert back to timezone A and timezone B.
(Or write a more complicated function that take the timezones of A and B as parameters and do the conversion in the function body!)

Do not trust dateutils without checks.
Changing servers. thaddy.com may be temporary unreachable but restored when the domain name transfer is done.

Чебурашка

  • Hero Member
  • *****
  • Posts: 588
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: DateUtils *Span and *Between functions
« Reply #2 on: March 26, 2025, 12:13:14 pm »
Before you use such functions beware that ...

Thanks Thaddy for this additional suggestions, I will use for sure.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

 

TinyPortal © 2005-2018