subtractDuration function

dynamic subtractDuration(
  1. dynamic date,
  2. Duration duration
)

Subtract the date value with duration without daylight saving value

eg., if local time zone as British Summer Time, and its daylight saving enabled on 29 March, 2020 to 25 October, 2020. subtract one day to the date(Oct 26, 2020) using subtract() method in DateTime, it return Oct 25, 2020 1.00.00 instead of Oct 25, 2020 00.00.00 because this method consider location and daylight saving.

So check whether the current date time zone offset(+2) is equal to previous date time zone offset(+1). if both are not equal then calculate the difference date.timeZoneOffset - currentDate.timeZoneOffset) and add the offset difference(1-2) to current date.

Implementation

dynamic subtractDuration(dynamic date, Duration duration) {
  dynamic currentDate = date.subtract(duration);
  if (date.timeZoneOffset != currentDate.timeZoneOffset) {
    currentDate =
        currentDate.add(date.timeZoneOffset - currentDate.timeZoneOffset);
  }

  return currentDate;
}