@stacksjs/datetime 0.75.227 → 0.75.229
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/dist/calendar.d.ts +36 -0
- package/dist/calendar.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/zone.d.ts +10 -3
- package/dist/zone.js +1 -1
- package/package.json +2 -2
package/dist/calendar.d.ts
CHANGED
|
@@ -1,2 +1,38 @@
|
|
|
1
1
|
/** Add calendar months, clamping the day to the target month's last day. Input is not mutated. */
|
|
2
2
|
export declare function addCalendarMonths(date: Date, months: number, options?: { utc?: boolean }): Date;
|
|
3
|
+
/** True for a real `YYYY-MM-DD` calendar date. `2026-02-31` and `2026-2-3` are not. */
|
|
4
|
+
export declare function isCalendarDate(value: unknown): value is string;
|
|
5
|
+
/** True for an IANA time zone this runtime knows (`Europe/Berlin`, `UTC`). */
|
|
6
|
+
export declare function isTimeZone(value: unknown): value is string;
|
|
7
|
+
/**
|
|
8
|
+
* Today's date in `timeZone`, as `YYYY-MM-DD`.
|
|
9
|
+
*
|
|
10
|
+
* The day a person or a site is living through, which is not the server's day
|
|
11
|
+
* for some hours of every day. Throws a `RangeError` for an unknown zone; to
|
|
12
|
+
* fall back through several (a site's, then its organization's, then UTC),
|
|
13
|
+
* use `localDateTime(now, ...zones).date`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* todayIn('Pacific/Auckland', new Date('2026-10-18T12:00:00Z')) // '2026-10-19'
|
|
17
|
+
*/
|
|
18
|
+
export declare function todayIn(timeZone: string, now?: Date): string;
|
|
19
|
+
/** `ymd` moved by `days` calendar days (negative goes back). Month and year ends roll over. */
|
|
20
|
+
export declare function addDays(ymd: string, days: number): string;
|
|
21
|
+
/** Whole calendar days from `from` to `to`: positive when `to` is later, 0 for the same day. */
|
|
22
|
+
export declare function daysBetween(from: string, to: string): number;
|
|
23
|
+
/** The Monday that starts the ISO week containing `ymd` (a Monday is its own start). */
|
|
24
|
+
export declare function startOfIsoWeek(ymd: string): string;
|
|
25
|
+
/**
|
|
26
|
+
* Minutes from `now` until clocks in `timeZone` read `time` on `date`.
|
|
27
|
+
*
|
|
28
|
+
* Negative once that moment has passed. The difference is between real
|
|
29
|
+
* instants, so a reminder an hour before a class the morning after a DST
|
|
30
|
+
* change is an hour, not the 0 or 120 minutes that subtracting two wall-clock
|
|
31
|
+
* readings gives. Ambiguous and skipped readings follow `zonedTimeToUtc`.
|
|
32
|
+
* The result keeps the seconds of `now` as a fraction; round it where you
|
|
33
|
+
* display or compare it.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* minutesUntil('2026-10-25', '09:00', 'Europe/Berlin', new Date('2026-10-24T22:00:00Z')) // 600
|
|
37
|
+
*/
|
|
38
|
+
export declare function minutesUntil(date: string, time: string, timeZone: string, now?: Date): number;
|
package/dist/calendar.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export function addCalendarMonths(date,months,options={}){if(!Number.isFinite(date.getTime())||!Number.isSafeInteger(months))throw Error("Calendar arithmetic requires a valid date and integer month count");const result=new Date(date);if(options.utc){const day=result.getUTCDate();result.setUTCDate(1);result.setUTCMonth(result.getUTCMonth()+months);result.setUTCDate(Math.min(day,new Date(Date.UTC(result.getUTCFullYear(),result.getUTCMonth()+1,0)).getUTCDate()))}else{const day=result.getDate();result.setDate(1);result.setMonth(result.getMonth()+months);result.setDate(Math.min(day,new Date(result.getFullYear(),result.getMonth()+1,0).getDate()))}return result}
|
|
1
|
+
import{zonedTimeToUtc}from"./zone";export function addCalendarMonths(date,months,options={}){if(!Number.isFinite(date.getTime())||!Number.isSafeInteger(months))throw Error("Calendar arithmetic requires a valid date and integer month count");const result=new Date(date);if(options.utc){const day=result.getUTCDate();result.setUTCDate(1);result.setUTCMonth(result.getUTCMonth()+months);result.setUTCDate(Math.min(day,new Date(Date.UTC(result.getUTCFullYear(),result.getUTCMonth()+1,0)).getUTCDate()))}else{const day=result.getDate();result.setDate(1);result.setMonth(result.getMonth()+months);result.setDate(Math.min(day,new Date(result.getFullYear(),result.getMonth()+1,0).getDate()))}return result}const DAY_MS=86400000,YMD=/^\d{4}-\d{2}-\d{2}$/,CLOCK=/^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$/;export function isCalendarDate(value){if(typeof value!=="string"||!YMD.test(value))return!1;const time=Date.parse(`${value}T00:00:00Z`);return Number.isFinite(time)&&new Date(time).toISOString().slice(0,10)===value}export function isTimeZone(value){if(typeof value!=="string"||!value.trim()||value.length>64)return!1;try{new Intl.DateTimeFormat("en-US",{timeZone:value});return!0}catch{return!1}}function dayNumber(ymd,name){if(!isCalendarDate(ymd))throw RangeError(`${name} expects a YYYY-MM-DD calendar date, got "${String(ymd)}"`);return Date.parse(`${ymd}T00:00:00Z`)/DAY_MS}function fromDayNumber(day){const result=new Date(day*DAY_MS).toISOString();if(result.length!==24)throw RangeError("Calendar date out of range");return result.slice(0,10)}export function todayIn(timeZone,now=new Date){if(!isTimeZone(timeZone))throw RangeError(`Unknown time zone "${String(timeZone)}"`);if(!Number.isFinite(now.getTime()))throw RangeError("todayIn expects a valid instant");const parts=new Intl.DateTimeFormat("en-US",{timeZone,year:"numeric",month:"2-digit",day:"2-digit"}).formatToParts(now),get=(type)=>parts.find((part)=>part.type===type)?.value??"";return`${get("year").padStart(4,"0")}-${get("month")}-${get("day")}`}export function addDays(ymd,days){if(!Number.isSafeInteger(days))throw RangeError("addDays expects a whole number of days");return fromDayNumber(dayNumber(ymd,"addDays")+days)}export function daysBetween(from,to){return dayNumber(to,"daysBetween")-dayNumber(from,"daysBetween")}export function startOfIsoWeek(ymd){const day=dayNumber(ymd,"startOfIsoWeek"),weekday=(day%7+7+3)%7;return fromDayNumber(day-weekday)}export function minutesUntil(date,time,timeZone,now=new Date){if(!isCalendarDate(date))throw RangeError(`minutesUntil expects a YYYY-MM-DD date, got "${String(date)}"`);if(typeof time!=="string"||!CLOCK.test(time))throw RangeError(`minutesUntil expects HH:mm or HH:mm:ss, got "${String(time)}"`);if(!isTimeZone(timeZone))throw RangeError(`Unknown time zone "${String(timeZone)}"`);if(!Number.isFinite(now.getTime()))throw RangeError("minutesUntil expects a valid instant");return(zonedTimeToUtc(`${date}T${time}`,timeZone).getTime()-now.getTime())/60000}
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,6 @@ export { parse } from './parse';
|
|
|
14
14
|
export { format as dateFormat } from './format';
|
|
15
15
|
// Wall-clock time in a named zone <-> the real instant (event feeds, schema.org).
|
|
16
16
|
export { isoInZone, zonedTimeToUtc, zoneOffsetMinutes } from './zone';
|
|
17
|
-
export { addCalendarMonths } from './calendar';
|
|
17
|
+
export { addCalendarMonths, addDays, daysBetween, isCalendarDate, isTimeZone, minutesUntil, startOfIsoWeek, todayIn } from './calendar';
|
|
18
18
|
export { localDateTime } from './local';
|
|
19
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";export*from"./schedule";
|
|
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,addDays,daysBetween,isCalendarDate,isTimeZone,minutesUntil,startOfIsoWeek,todayIn}from"./calendar";export{localDateTime}from"./local";export*from"./schedule";
|
package/dist/zone.d.ts
CHANGED
|
@@ -7,9 +7,16 @@ export declare function zoneOffsetMinutes(instant: Date, timeZone: string): numb
|
|
|
7
7
|
* with no offset. A string that carries its own offset is already an instant,
|
|
8
8
|
* so it is rejected rather than silently reinterpreted.
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* Around a DST change the rule is the same in every zone (it is RFC 5545's and
|
|
11
|
+
* Temporal's `'compatible'` disambiguation):
|
|
12
|
+
*
|
|
13
|
+
* - A time that happens twice (the fall-back hour) resolves to the EARLIER of
|
|
14
|
+
* the two instants, which is what a box office means by it. Berlin's
|
|
15
|
+
* `2026-10-25T02:30` is `00:30Z` (`+02:00`), not `01:30Z` (`+01:00`).
|
|
16
|
+
* - A time that never happens (skipped by a spring-forward) is read with the
|
|
17
|
+
* offset in force before the gap, so it lands as far past the gap as it was
|
|
18
|
+
* into it. Berlin's `2026-03-29T02:30` is `01:30Z`, which clocks there show
|
|
19
|
+
* as `03:30`; New York's `2026-03-08T02:30` likewise shows as `03:30`.
|
|
13
20
|
*
|
|
14
21
|
* @example
|
|
15
22
|
* zonedTimeToUtc('2026-10-01T20:00:00', 'America/New_York').toISOString()
|
package/dist/zone.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const WALL_CLOCK=/^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;export function zoneOffsetMinutes(instant,timeZone){const parts=new Intl.DateTimeFormat("en-US",{timeZone,hourCycle:"h23",year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit"}).formatToParts(instant),get=(type)=>Number(parts.find((p)=>p.type===type)?.value??0),
|
|
1
|
+
const DAY_MS=86400000,WALL_CLOCK=/^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;export function zoneOffsetMinutes(instant,timeZone){const parts=new Intl.DateTimeFormat("en-US",{timeZone,hourCycle:"h23",year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit"}).formatToParts(instant),get=(type)=>Number(parts.find((p)=>p.type===type)?.value??0),reading=new Date(Date.UTC(2000,get("month")-1,get("day"),get("hour"),get("minute"),get("second")));reading.setUTCFullYear(get("year"));const asUtc=reading.getTime(),whole=Math.floor(instant.getTime()/1000)*1000;return Math.round((asUtc-whole)/60000)}export function zonedTimeToUtc(wallClock,timeZone){const match=WALL_CLOCK.exec(wallClock.trim());if(!match)throw TypeError(`zonedTimeToUtc expects a wall-clock time like 2026-10-01T20:00:00, got "${wallClock}"`);const[,y,mo,d,h="0",mi="0",s="0"]=match,reading=new Date(Date.UTC(2000,Number(mo)-1,Number(d),Number(h),Number(mi),Number(s)));reading.setUTCFullYear(Number(y));const local=reading.getTime(),before=zoneOffsetMinutes(new Date(local-DAY_MS),timeZone),after=zoneOffsetMinutes(new Date(local+DAY_MS),timeZone),shows=(offset)=>{const instant=local-offset*60000;return zoneOffsetMinutes(new Date(instant),timeZone)===offset?instant:null},valid=[shows(before),shows(after)].filter((instant)=>instant!==null);if(valid.length)return new Date(Math.min(...valid));return new Date(local-before*60000)}export function isoInZone(instant,timeZone){const offset=zoneOffsetMinutes(instant,timeZone),local=new Date(Math.floor(instant.getTime()/1000)*1000+offset*60000),sign=offset<0?"-":"+",abs=Math.abs(offset),pad=(n)=>String(n).padStart(2,"0");return`${local.toISOString().slice(0,19)}${sign}${pad(Math.floor(abs/60))}:${pad(abs%60)}`}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/datetime",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.75.
|
|
4
|
+
"version": "0.75.229",
|
|
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.
|
|
62
|
+
"@stacksjs/utils": "0.75.229"
|
|
63
63
|
},
|
|
64
64
|
"sideEffects": false
|
|
65
65
|
}
|