dtinterval.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import 'package:jiffy/jiffy.dart';
  2. class DTInterval {
  3. late Jiffy start;
  4. late Jiffy end;
  5. DTInterval(this.start, this.end);
  6. @override
  7. String toString() =>
  8. "<${start.format(pattern: "ddMMMyy HH:mm")} - ${end.format(pattern: "ddMMMyy HH:mm")}>";
  9. bool include(Jiffy x) {
  10. return x.isSameOrAfter(start) && x.isSameOrBefore(end);
  11. }
  12. bool isOverlap(DTInterval x) {
  13. return x.start.isSameOrBefore(end) && x.end.isSameOrAfter(start);
  14. }
  15. bool contains(DTInterval x) {
  16. return include(x.start) && include(x.end);
  17. }
  18. DTInterval? intersection(DTInterval x) {
  19. if (!isOverlap(x)) {
  20. return null;
  21. } else {
  22. return DTInterval(start.max(x.start), end.min(x.end));
  23. }
  24. }
  25. List<DTInterval> intersectionmany(List<DTInterval> x) {
  26. return x.map((e) => intersection(e)).nonNulls.toList();
  27. }
  28. List<DTInterval> minus(DTInterval x) {
  29. if (!isOverlap(x)) {
  30. return [this];
  31. } else if (x.include(start) && x.include(end)) {
  32. return [];
  33. } else if (x.include(start)) {
  34. return [DTInterval(x.end, end)];
  35. } else if (x.include(end)) {
  36. return [DTInterval(start, x.start)];
  37. } else {
  38. return [DTInterval(start, x.start), DTInterval(x.end, end)];
  39. }
  40. }
  41. // List<DTInterval> minus(DTInterval other) {
  42. // List<DTInterval> result = [];
  43. // if (start.isBefore(other.start) && end.isAfter(other.start)) {
  44. // result.add(DTInterval(start, other.start));
  45. // }
  46. // if (start.isBefore(other.end) && end.isAfter(other.end)) {
  47. // result.add(DTInterval(other.end, end));
  48. // }
  49. // return result;
  50. // }
  51. Duration get duration => end.dateTime.difference(start.dateTime);
  52. // Duration get duration => Duration(
  53. // milliseconds: end.diff(start, unit: Unit.millisecond).abs().ceil());
  54. bool isEmpty() {
  55. return start.isSameOrAfter(end);
  56. }
  57. DTInterval toUtc() {
  58. return DTInterval(start.toUtc(), end.toUtc());
  59. }
  60. }
  61. extension JiffyExtensions on Jiffy? {
  62. Jiffy? latest(Jiffy? val2) {
  63. if (this == null) return val2;
  64. if (val2 == null) return this;
  65. if (this!.isBefore(val2)) {
  66. return val2;
  67. } else {
  68. return this;
  69. }
  70. }
  71. Jiffy? earliset(Jiffy? val2) {
  72. if (this == null) return val2;
  73. if (val2 == null) return this;
  74. if (this!.isAfter(val2)) {
  75. return val2;
  76. } else {
  77. return this;
  78. }
  79. }
  80. Jiffy max(Jiffy val2) {
  81. if (this!.isBefore(val2)) {
  82. return val2;
  83. } else {
  84. return this!;
  85. }
  86. }
  87. Jiffy min(Jiffy val2) {
  88. if (this!.isAfter(val2)) {
  89. return val2;
  90. } else {
  91. return this!;
  92. }
  93. }
  94. }