How can I calculate the number of days between two dates without taking seasonal time shifts into account?

Problem description:

J’aimerais calculer le nombre de jours écoulés entre deux dates, sans tenir compte du décalage que pourraient causer les changements entre heure d’hiver et heure d’été. Quelle serait la méthode la plus appropriée ?

Solution:

The following method is available in the date function library for Groovy scripts:

double getIntervalInDaysIgnoringDaylightSaving(Date date1, Date date2)

This function returns the number of days between two dates while ignoring daylight saving time changes (it uses the times displayed in the preparation editor — that is, the server’s timezone — and treats them as if they were universal time (UTC) or local time without a timezone… since we are calculating a difference, this makes no difference to the result).

Example :

The difference in days between 2024-03-24 19:00:00 and 2024-04-03 19:00:00 (as displayed in the preparation editor) is 10.0 because the dates are reinterpreted from their textual form, without a timezone (= local date). Therefore, the daylight saving transition between the two dates is ignored.

Note

With the method getIntervalInDays(Date date1, Date date2), the result would be 9.95833 for the same input dates.

Warning

If one of the two dates falls exactly within the daylight saving transition period, the behavior is undefined. For example, March 31, 2024, at 2:30 a.m. is an hour that does not exist in the locale of the server. The probability is low, but it’s worth being aware of.