getOverlappingDaysInIntervals.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. exports.getOverlappingDaysInIntervals = getOverlappingDaysInIntervals;
  3. var _index = require("./_lib/getTimezoneOffsetInMilliseconds.js");
  4. var _index2 = require("./constants.js");
  5. var _index3 = require("./toDate.js");
  6. /**
  7. * @name getOverlappingDaysInIntervals
  8. * @category Interval Helpers
  9. * @summary Get the number of days that overlap in two time intervals
  10. *
  11. * @description
  12. * Get the number of days that overlap in two time intervals. It uses the time
  13. * between dates to calculate the number of days, rounding it up to include
  14. * partial days.
  15. *
  16. * Two equal 0-length intervals will result in 0. Two equal 1ms intervals will
  17. * result in 1.
  18. *
  19. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  20. *
  21. * @param intervalLeft - The first interval to compare.
  22. * @param intervalRight - The second interval to compare.
  23. *
  24. * @returns The number of days that overlap in two time intervals
  25. *
  26. * @example
  27. * // For overlapping time intervals adds 1 for each started overlapping day:
  28. * getOverlappingDaysInIntervals(
  29. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  30. * { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
  31. * )
  32. * //=> 3
  33. *
  34. * @example
  35. * // For non-overlapping time intervals returns 0:
  36. * getOverlappingDaysInIntervals(
  37. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
  38. * { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
  39. * )
  40. * //=> 0
  41. */
  42. function getOverlappingDaysInIntervals(intervalLeft, intervalRight) {
  43. const [leftStart, leftEnd] = [
  44. +(0, _index3.toDate)(intervalLeft.start),
  45. +(0, _index3.toDate)(intervalLeft.end),
  46. ].sort((a, b) => a - b);
  47. const [rightStart, rightEnd] = [
  48. +(0, _index3.toDate)(intervalRight.start),
  49. +(0, _index3.toDate)(intervalRight.end),
  50. ].sort((a, b) => a - b);
  51. // Prevent NaN result if intervals don't overlap at all.
  52. const isOverlapping = leftStart < rightEnd && rightStart < leftEnd;
  53. if (!isOverlapping) return 0;
  54. // Remove the timezone offset to negate the DST effect on calculations.
  55. const overlapLeft = rightStart < leftStart ? leftStart : rightStart;
  56. const left =
  57. overlapLeft - (0, _index.getTimezoneOffsetInMilliseconds)(overlapLeft);
  58. const overlapRight = rightEnd > leftEnd ? leftEnd : rightEnd;
  59. const right =
  60. overlapRight - (0, _index.getTimezoneOffsetInMilliseconds)(overlapRight);
  61. // Ceil the number to include partial days too.
  62. return Math.ceil((right - left) / _index2.millisecondsInDay);
  63. }