add method

  1. @useResult
ZonedDateTime add(
  1. Duration duration
)

Returns a copy of this with the duration added.

This function adds an exact number of microseconds unlike +. It may produce surprising results when added to a ZonedDateTime near a DST transition.

// DST occurs at 2023-03-12 02:00
// https://www.timeanddate.com/time/change/usa/detroit?year=2023

final foo = ZonedDateTime('America/Detroit', 2023, 3, 12);

foo.add(Duration(days: 1)); // 2023-03-13T01:00-04:00[America/Detroit]
foo + Period(days: 1);      // 2023-03-13T00:00-04:00[America/Detroit]

Implementation

@useResult ZonedDateTime add(Duration duration) => ZonedDateTime.fromEpochMicroseconds(
  timezone,
  epochMicroseconds + duration.inMicroseconds,
);