Rocky_Mountain_Vending/.pnpm-store/v10/files/b1/1dded45cd3080c80efdccaddc9b89d0723f7d548d2fa39b6df23d5db2c8a23b680b310bdeed53bcaf697e7deb84d301c34e65e3189ab4d2ba30196c552679c
DMleadgen 46d973904b
Initial commit: Rocky Mountain Vending website
Next.js website for Rocky Mountain Vending company featuring:
- Product catalog with Stripe integration
- Service areas and parts pages
- Admin dashboard with Clerk authentication
- SEO optimized pages with JSON-LD structured data

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-12 16:22:15 -07:00

66 lines
2.4 KiB
Text

import { getTimezoneOffsetInMilliseconds } from "./_lib/getTimezoneOffsetInMilliseconds.js";
import { millisecondsInDay } from "./constants.js";
import { toDate } from "./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.
*
* @param intervalLeft - The first interval to compare.
* @param intervalRight - The second interval to compare.
* @param options - An object with options
*
* @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
*/
export function getOverlappingDaysInIntervals(intervalLeft, intervalRight) {
const [leftStart, leftEnd] = [
+toDate(intervalLeft.start),
+toDate(intervalLeft.end),
].sort((a, b) => a - b);
const [rightStart, rightEnd] = [
+toDate(intervalRight.start),
+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 - getTimezoneOffsetInMilliseconds(overlapLeft);
const overlapRight = rightEnd > leftEnd ? leftEnd : rightEnd;
const right = overlapRight - getTimezoneOffsetInMilliseconds(overlapRight);
// Ceil the number to include partial days too.
return Math.ceil((right - left) / millisecondsInDay);
}
// Fallback for modularized imports:
export default getOverlappingDaysInIntervals;