PascalTZ icon indicating copy to clipboard operation
PascalTZ copied to clipboard

Extend PascalTZ for time operations

Open JoshyFun opened this issue 8 years ago • 1 comments

Do you think that it will be interesting to extend library to perform precise time operations (to the second precision) so it can calculate seconds between two arbitrary times/dates ?

It could be important when calculating time (seconds) between to given moments when one is a few seconds before time change and second moment is just after time change as regular operation will result in wrong real time elapsed.

I have this problem in a project, fortunatly it is not a serious problem, but I have event time and duration and I must write the event finish time, evertyhing in local time. I'm just operating at GMT time instead the local time but some helper functions like "IncSecondsLocalTime" could be a good help for some people and specially the SecondsBetweenLocalTime(DateTime1,DateTime2).

fpc date operations most time work in an approximation style, so months/days elapsed between two dates sometimes do not return the exact value due its floating point nature, so things like months between 2017.01.31 and 2017.02.28 (if I recall correctly) returns 0.98 which is not 1.00 month.

JoshyFun avatar Apr 08 '17 19:04 JoshyFun

I think it depends on the nature of these operations. If they are related to timezone conversion, then yes, otherwise they might fit better somewhere like DateUtils unit.

The operations which you have mentioned are simple helper functions, but they can be added for convenience.

SecondsBetweenLocalTime:

SecondsBetween(LocalTimeToGMT(DateTime1, TimeZone1), LocalTimeToGMT(DateTime2, TimeZone2));

IncSecondsLocalTime:

GMTToLocalTime(IncSeconds(LocalTimeToGMT(DateTime, TimeZone), NumSeconds), TimeZone);

In relation to month span between 2017.01.31 00:00:00 and 2017.02.28 00:00:00, I expect the standard answer to be 28 days, not 1 month. For example, FPC and PHP agree here.

dezlov avatar Apr 18 '17 13:04 dezlov