@stacksjs/datetime 0.75.115 → 0.75.117

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -93,3 +93,23 @@ UTC, and returns `{ date, time, timeZone }` from one instant. Supply site and
93
93
  organization zones in that order. Missing/invalid zones fall through; an
94
94
  invalid instant throws. The output uses YYYY-MM-DD and HH:mm and is suitable
95
95
  for comparing local schedule dates and end times, not for elapsed durations.
96
+
97
+ ## Recurrences, coverage and local reporting periods
98
+
99
+ The pure schedule entry exports parseWeeklySchedule, expandWeeklySchedule,
100
+ clockMinutes, addClockMinutes and timeWindowsCover. Recurrence validation
101
+ rejects invalid weekday entries; expansion refuses excessive ranges and
102
+ midnight-crossing durations instead of silently truncating them. Adjacent
103
+ windows can cover one continuous slot. ScheduleInputError carries status 422.
104
+
105
+ localDateRange(from, to, ...zones) converts inclusive calendar days to an
106
+ exclusive-end UTC instant range in a selected valid zone, including DST days.
107
+ Its supported reporting dates are 0101-01-01 through 9999-12-30; unsupported
108
+ boundaries raise ScheduleInputError rather than normalizing to another year.
109
+ netHoursBetween(start, end, breaks) retains full precision; apply display or
110
+ monetary rounding at the final boundary. It performs duration arithmetic,
111
+ not jurisdiction-specific compensation or overtime decisions.
112
+
113
+ localTimeSpan(date, start, end, ...zones) converts a clock interval, including
114
+ next-day ends, to actual instants for cross-site/overnight overlap checks.
115
+ Equal endpoints and non-positive DST-normalized intervals are rejected.
package/dist/index.d.ts CHANGED
@@ -16,3 +16,4 @@ export { format as dateFormat } from './format';
16
16
  export { isoInZone, zonedTimeToUtc, zoneOffsetMinutes } from './zone';
17
17
  export { addCalendarMonths } from './calendar';
18
18
  export { localDateTime } from './local';
19
+ export * from './schedule';
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export{DateTime,now}from"./now";export{format}from"./format";export{parse}from"./parse";export{format as dateFormat}from"./format";export{isoInZone,zonedTimeToUtc,zoneOffsetMinutes}from"./zone";export{addCalendarMonths}from"./calendar";export{localDateTime}from"./local";
1
+ export{DateTime,now}from"./now";export{format}from"./format";export{parse}from"./parse";export{format as dateFormat}from"./format";export{isoInZone,zonedTimeToUtc,zoneOffsetMinutes}from"./zone";export{addCalendarMonths}from"./calendar";export{localDateTime}from"./local";export*from"./schedule";
@@ -0,0 +1,21 @@
1
+ /** A real canonical calendar date, not a date that Date silently normalizes. */
2
+ export declare function calendarDay(value: string): number;
3
+ export declare function clockMinutes(value: string): number;
4
+ /** Same-day slots must retain their full duration. Crossing midnight is an error. */
5
+ export declare function addClockMinutes(start: string, duration: number): string;
6
+ /** Invalid weekday entries invalidate the entire recurrence, rather than changing its meaning. */
7
+ export declare function parseWeeklySchedule(value: unknown): WeeklySchedule | null;
8
+ export declare function expandWeeklySchedule(spec: WeeklySchedule, from: string, to: string, duration: number, maxDays?: number): Array<{ session_date: string, start_time: string, end_time: string }>;
9
+ /** Adjacent/overlapping windows together cover a slot; gaps remain unavailable. */
10
+ export declare function timeWindowsCover(windows: readonly TimeWindow[], start: string, end: string): boolean;
11
+ /** Inclusive local dates become a half-open instant range, including 23/25-hour DST days. */
12
+ export declare function localDateRange(from: string, to: string, ...zones: unknown[]): { start: Date, end: Date, timeZone: string };
13
+ /** Keep full elapsed-time precision until money or display formatting is applied. */
14
+ export declare function netHoursBetween(start: Date | string | null, end: Date | string | null, breakMinutes?: number): number;
15
+ /** A location's clock interval as real instants; an earlier end time belongs to the next day. */
16
+ export declare function localTimeSpan(date: string, start: string, end: string, ...zones: unknown[]): { start: Date, end: Date, timeZone: string };
17
+ export declare interface WeeklySchedule { weekdays: number[], start_time: string }
18
+ export declare interface TimeWindow { start_time: string, end_time: string }
19
+ export declare class ScheduleInputError extends RangeError {
20
+ readonly status: number;
21
+ }
@@ -0,0 +1 @@
1
+ import{localDateTime}from"./local";import{zonedTimeToUtc}from"./zone";export class ScheduleInputError extends RangeError{status=422}const DAY=86400000;export function calendarDay(value){if(typeof value!=="string"||!/^\d{4}-\d{2}-\d{2}$/.test(value))throw new ScheduleInputError("Use yyyy-mm-dd dates");const date=new Date(`${value}T00:00:00Z`);if(!Number.isFinite(date.getTime())||date.toISOString().slice(0,10)!==value)throw new ScheduleInputError("Invalid calendar date");return date.getTime()/DAY}export function clockMinutes(value){if(typeof value!=="string"||!/^\d{2}:\d{2}$/.test(value))throw new ScheduleInputError("Use HH:MM times");const[hour,minute]=value.split(":").map(Number);if(hour>23||minute>59)throw new ScheduleInputError("Invalid clock time");return hour*60+minute}export function addClockMinutes(start,duration){if(!Number.isSafeInteger(duration)||duration<=0)throw new ScheduleInputError("Duration must be positive whole minutes");const end=clockMinutes(start)+duration;if(end>=1440)throw new ScheduleInputError("Session must end before midnight; shorten it or move its start time");return`${String(Math.floor(end/60)).padStart(2,"0")}:${String(end%60).padStart(2,"0")}`}export function parseWeeklySchedule(value){try{const data=typeof value==="string"?JSON.parse(value):value;if(!data||typeof data!=="object"||Array.isArray(data)||!Array.isArray(data.weekdays)||!data.weekdays.length)return null;const weekdays=data.weekdays.map((day)=>{if(typeof day!=="number"&&(typeof day!=="string"||!/^[0-6]$/.test(day.trim())))throw new ScheduleInputError("Invalid weekday");const number=Number(day);if(!Number.isInteger(number)||number<0||number>6)throw new ScheduleInputError("Invalid weekday");return number});clockMinutes(data.start_time);return{weekdays:[...new Set(weekdays)],start_time:data.start_time}}catch(error){if(error instanceof SyntaxError||error instanceof ScheduleInputError)return null;throw error}}export function expandWeeklySchedule(spec,from,to,duration,maxDays=800){const first=calendarDay(from),last=calendarDay(to);if(!Number.isSafeInteger(maxDays)||maxDays<1)throw new ScheduleInputError("Invalid date-range limit");if(last<first)throw new ScheduleInputError("from must be on or before to");if(last-first+1>maxDays)throw new ScheduleInputError(`Generate at most ${maxDays} days at a time`);const schedule=parseWeeklySchedule(spec);if(!schedule)throw new ScheduleInputError("Invalid recurrence");const end=addClockMinutes(schedule.start_time,duration),days=new Set(schedule.weekdays),slots=[];for(let day=first;day<=last;day++){const date=new Date(day*DAY);if(days.has(date.getUTCDay()))slots.push({session_date:date.toISOString().slice(0,10),start_time:schedule.start_time,end_time:end})}return slots}export function timeWindowsCover(windows,start,end){const first=clockMinutes(start),last=clockMinutes(end);if(first>=last)return!1;const spans=windows.map((window)=>({start:clockMinutes(window.start_time),end:clockMinutes(window.end_time)})).sort((a,b)=>a.start-b.start);let covered=first;for(const span of spans){if(span.end<=span.start)throw new ScheduleInputError("Availability must end after it starts");if(span.start>covered)break;covered=Math.max(covered,span.end);if(covered>=last)return!0}return!1}export function localDateRange(from,to,...zones){const first=calendarDay(from),last=calendarDay(to);if(last<first)throw new ScheduleInputError("from must be on or before to");if(Number(from.slice(0,4))<101||to>="9999-12-31")throw new ScheduleInputError("Local ranges support dates from 0101-01-01 through 9999-12-30");const timeZone=localDateTime(new Date(first*DAY),...zones).timeZone,next=new Date((last+1)*DAY).toISOString().slice(0,10);return{start:zonedTimeToUtc(`${from}T00:00:00`,timeZone),end:zonedTimeToUtc(`${next}T00:00:00`,timeZone),timeZone}}export function netHoursBetween(start,end,breakMinutes=0){if(start==null||end==null||!Number.isFinite(breakMinutes))return 0;const first=start instanceof Date?start.getTime():Date.parse(start),last=end instanceof Date?end.getTime():Date.parse(end);if(!Number.isFinite(first)||!Number.isFinite(last)||last<=first)return 0;return Math.max(0,(last-first)/3600000-Math.max(0,breakMinutes)/60)}export function localTimeSpan(date,start,end,...zones){const day=calendarDay(date),first=clockMinutes(start),last=clockMinutes(end);if(first===last)throw new ScheduleInputError("An interval must have distinct start and end times");if(Number(date.slice(0,4))<101||date>="9999-12-31")throw new ScheduleInputError("Unsupported local interval date");const timeZone=localDateTime(new Date(day*DAY),...zones).timeZone,endDate=last<first?new Date((day+1)*DAY).toISOString().slice(0,10):date,interval={start:zonedTimeToUtc(`${date}T${start}:00`,timeZone),end:zonedTimeToUtc(`${endDate}T${end}:00`,timeZone),timeZone};if(interval.end<=interval.start)throw new ScheduleInputError("The local interval has no positive duration in this timezone");return interval}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/datetime",
3
3
  "type": "module",
4
- "version": "0.75.115",
4
+ "version": "0.75.117",
5
5
  "description": "The Stacks datetime helpers.",
6
6
  "author": "Chris Breuer",
7
7
  "contributors": [
@@ -59,7 +59,7 @@
59
59
  },
60
60
  "devDependencies": {
61
61
  "better-dx": "^0.2.24",
62
- "@stacksjs/utils": "0.75.115"
62
+ "@stacksjs/utils": "0.75.117"
63
63
  },
64
64
  "sideEffects": false
65
65
  }