| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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<DTInterval> intersectionmany(List<DTInterval> x) {
- return x.map((e) => intersection(e)).nonNulls.toList();
- }
- List<DTInterval> 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<DTInterval> minus(DTInterval other) {
- // List<DTInterval> 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!;
- }
- }
- }
|