import 'package:jiffy/jiffy.dart'; class DTInterval { late Jiffy start; late Jiffy end; DTInterval(this.start, this.end); @override String toString() => "<${start.format(pattern: "ddMMMyy HH:mm")} - ${end.format(pattern: "ddMMMyy HH:mm")}>"; bool include(Jiffy x) { return x.isSameOrAfter(start) && x.isSameOrBefore(end); } bool isOverlap(DTInterval x) { return x.start.isSameOrBefore(end) && x.end.isSameOrAfter(start); } bool contains(DTInterval x) { return include(x.start) && include(x.end); } DTInterval? intersection(DTInterval x) { if (!isOverlap(x)) { return null; } else { return DTInterval(start.max(x.start), end.min(x.end)); } } List intersectionmany(List x) { return x.map((e) => intersection(e)).nonNulls.toList(); } List minus(DTInterval x) { if (!isOverlap(x)) { return [this]; } else if (x.include(start) && x.include(end)) { return []; } else if (x.include(start)) { return [DTInterval(x.end, end)]; } else if (x.include(end)) { return [DTInterval(start, x.start)]; } else { return [DTInterval(start, x.start), DTInterval(x.end, end)]; } } // List minus(DTInterval other) { // List result = []; // if (start.isBefore(other.start) && end.isAfter(other.start)) { // result.add(DTInterval(start, other.start)); // } // if (start.isBefore(other.end) && end.isAfter(other.end)) { // result.add(DTInterval(other.end, end)); // } // return result; // } Duration get duration => end.dateTime.difference(start.dateTime); // Duration get duration => Duration( // milliseconds: end.diff(start, unit: Unit.millisecond).abs().ceil()); bool isEmpty() { return start.isSameOrAfter(end); } DTInterval toUtc() { return DTInterval(start.toUtc(), end.toUtc()); } } extension JiffyExtensions on Jiffy? { Jiffy? latest(Jiffy? val2) { if (this == null) return val2; if (val2 == null) return this; if (this!.isBefore(val2)) { return val2; } else { return this; } } Jiffy? earliset(Jiffy? val2) { if (this == null) return val2; if (val2 == null) return this; if (this!.isAfter(val2)) { return val2; } else { return this; } } Jiffy max(Jiffy val2) { if (this!.isBefore(val2)) { return val2; } else { return this!; } } Jiffy min(Jiffy val2) { if (this!.isAfter(val2)) { return val2; } else { return this!; } } }