splitByMonth function

List<(DateTime, DateTime)> splitByMonth(
  1. DateTime start,
  2. DateTime end
)

Splits start..end into consecutive months. Returns list of (monthStart, monthEnd).

Boundaries are built in the same kind as start: UTC input yields UTC boundaries, local input yields local ones.

Mixed kinds: start's kind wins. When end is the other kind it is first converted to the same instant in start's kind (toUtc() / toLocal()), so every value in the result shares one kind and months are judged in start's calendar. Example: a UTC start with a local end of Jan 31 23:00 in US Eastern (Feb 1 04:00Z) yields a February segment. Audited: 2026-06-12 11:26 EDT

Implementation

List<(DateTime, DateTime)> splitByMonth(DateTime start, DateTime end) {
  // Defined mixed-kind behavior: an end of the other kind used to leak into the
  // last segment unconverted, with its month read from its own-zone fields.
  final DateTime sameKindEnd = start.isUtc ? end.toUtc() : end.toLocal();
  return _splitSameKind(start, sameKindEnd);
}