| Server IP : 185.11.201.71 / Your IP : 192.168.7.18 Web Server : Apache/2.4.29 (Ubuntu) System : Linux tech-virtual-machine 4.15.0-213-generic #224-Ubuntu SMP Mon Jun 19 13:30:12 UTC 2023 x86_64 User : tech ( 1000) PHP Version : 7.4.28 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /comunica/app_ojos_/frontend/node_modules/date-fns/ |
Upload File : |
"use strict";
exports.getOverlappingDaysInIntervals = getOverlappingDaysInIntervals;
var _index = require("./_lib/getTimezoneOffsetInMilliseconds.js");
var _index2 = require("./constants.js");
var _index3 = require("./toDate.js");
/**
* @name getOverlappingDaysInIntervals
* @category Interval Helpers
* @summary Get the number of days that overlap in two time intervals
*
* @description
* Get the number of days that overlap in two time intervals. It uses the time
* between dates to calculate the number of days, rounding it up to include
* partial days.
*
* Two equal 0-length intervals will result in 0. Two equal 1ms intervals will
* result in 1.
*
* @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).
*
* @param intervalLeft - The first interval to compare.
* @param intervalRight - The second interval to compare.
*
* @returns The number of days that overlap in two time intervals
*
* @example
* // For overlapping time intervals adds 1 for each started overlapping day:
* getOverlappingDaysInIntervals(
* { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
* { start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
* )
* //=> 3
*
* @example
* // For non-overlapping time intervals returns 0:
* getOverlappingDaysInIntervals(
* { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
* { start: new Date(2014, 0, 21), end: new Date(2014, 0, 22) }
* )
* //=> 0
*/
function getOverlappingDaysInIntervals(intervalLeft, intervalRight) {
const [leftStart, leftEnd] = [
+(0, _index3.toDate)(intervalLeft.start),
+(0, _index3.toDate)(intervalLeft.end),
].sort((a, b) => a - b);
const [rightStart, rightEnd] = [
+(0, _index3.toDate)(intervalRight.start),
+(0, _index3.toDate)(intervalRight.end),
].sort((a, b) => a - b);
// Prevent NaN result if intervals don't overlap at all.
const isOverlapping = leftStart < rightEnd && rightStart < leftEnd;
if (!isOverlapping) return 0;
// Remove the timezone offset to negate the DST effect on calculations.
const overlapLeft = rightStart < leftStart ? leftStart : rightStart;
const left =
overlapLeft - (0, _index.getTimezoneOffsetInMilliseconds)(overlapLeft);
const overlapRight = rightEnd > leftEnd ? leftEnd : rightEnd;
const right =
overlapRight - (0, _index.getTimezoneOffsetInMilliseconds)(overlapRight);
// Ceil the number to include partial days too.
return Math.ceil((right - left) / _index2.millisecondsInDay);
}