temporal-fmt 0.9.3 → 0.9.32

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.
Files changed (91) hide show
  1. package/README.md +36 -8
  2. package/dist/calendarUtils.cjs +259 -4
  3. package/dist/calendarUtils.cjs.map +1 -1
  4. package/dist/calendarUtils.js +33 -1
  5. package/dist/chunk-4N3WCGEY.js +183 -0
  6. package/dist/{chunk-SSPABEUR.js.map → chunk-4N3WCGEY.js.map} +1 -1
  7. package/dist/chunk-5UTV5VP6.js +984 -0
  8. package/dist/chunk-5UTV5VP6.js.map +1 -0
  9. package/dist/chunk-ACKKWH5Z.js +578 -0
  10. package/dist/{chunk-2JHEV7ZF.js.map → chunk-ACKKWH5Z.js.map} +1 -1
  11. package/dist/chunk-DR62Z6GV.js +224 -0
  12. package/dist/{chunk-ZGMTY52O.js.map → chunk-DR62Z6GV.js.map} +1 -1
  13. package/dist/chunk-EGUUO6LE.js +439 -0
  14. package/dist/chunk-EGUUO6LE.js.map +1 -0
  15. package/dist/chunk-GPB4JHGI.js +65 -0
  16. package/dist/chunk-GPB4JHGI.js.map +1 -0
  17. package/dist/chunk-H7YQX25T.js +170 -0
  18. package/dist/{chunk-J2AQNXF2.js.map → chunk-H7YQX25T.js.map} +1 -1
  19. package/dist/chunk-HXY2GUUM.js +182 -0
  20. package/dist/{chunk-EHLRZULM.js.map → chunk-HXY2GUUM.js.map} +1 -1
  21. package/dist/chunk-L24NPTAJ.js +328 -0
  22. package/dist/{chunk-GMGZZG6I.js.map → chunk-L24NPTAJ.js.map} +1 -1
  23. package/dist/chunk-M3GDTHMT.js +169 -0
  24. package/dist/chunk-M3GDTHMT.js.map +1 -0
  25. package/dist/chunk-MKPDLQKD.js +51 -0
  26. package/dist/chunk-MKPDLQKD.js.map +1 -0
  27. package/dist/chunk-P3G4JRYC.js +205 -0
  28. package/dist/{chunk-4XRE37WT.js.map → chunk-P3G4JRYC.js.map} +1 -1
  29. package/dist/chunk-RIZ3HN37.js +181 -0
  30. package/dist/{chunk-UJR6DHXY.js.map → chunk-RIZ3HN37.js.map} +1 -1
  31. package/dist/chunk-TSLPIURC.js +242 -0
  32. package/dist/{chunk-VQXUMFB2.js.map → chunk-TSLPIURC.js.map} +1 -1
  33. package/dist/chunk-UOPAV3WX.js +75 -0
  34. package/dist/chunk-UOPAV3WX.js.map +1 -0
  35. package/dist/chunk-YEUASGDK.js +9 -0
  36. package/dist/chunk-YEUASGDK.js.map +1 -0
  37. package/dist/chunk-ZQZF5EXW.js +29 -0
  38. package/dist/chunk-ZQZF5EXW.js.map +1 -0
  39. package/dist/duration.cjs +638 -26
  40. package/dist/duration.cjs.map +1 -1
  41. package/dist/duration.js +28 -1
  42. package/dist/format.cjs +833 -35
  43. package/dist/format.cjs.map +1 -1
  44. package/dist/format.js +20 -1
  45. package/dist/index.cjs +6905 -163
  46. package/dist/index.cjs.map +1 -1
  47. package/dist/index.js +3091 -58
  48. package/dist/index.js.map +1 -1
  49. package/dist/interval.cjs +1015 -35
  50. package/dist/interval.cjs.map +1 -1
  51. package/dist/interval.js +41 -1
  52. package/dist/localeRegistry.cjs +375 -32
  53. package/dist/localeRegistry.cjs.map +1 -1
  54. package/dist/localeRegistry.js +11 -1
  55. package/dist/parse.cjs +1797 -70
  56. package/dist/parse.cjs.map +1 -1
  57. package/dist/parse.js +20 -1
  58. package/dist/recurrence.cjs +452 -20
  59. package/dist/recurrence.cjs.map +1 -1
  60. package/dist/recurrence.js +20 -1
  61. package/dist/relativeTime.cjs +268 -21
  62. package/dist/relativeTime.cjs.map +1 -1
  63. package/dist/relativeTime.js +14 -1
  64. package/dist/timezone.cjs +387 -20
  65. package/dist/timezone.cjs.map +1 -1
  66. package/dist/timezone.js +26 -1
  67. package/package.json +4 -6
  68. package/scripts/cli.mjs +17 -5
  69. package/dist/chunk-2JHEV7ZF.js +0 -7
  70. package/dist/chunk-3MZLTVP3.js +0 -2
  71. package/dist/chunk-3MZLTVP3.js.map +0 -1
  72. package/dist/chunk-4JDAJSEM.js +0 -35
  73. package/dist/chunk-4JDAJSEM.js.map +0 -1
  74. package/dist/chunk-4XRE37WT.js +0 -2
  75. package/dist/chunk-5U5WJ465.js +0 -36
  76. package/dist/chunk-5U5WJ465.js.map +0 -1
  77. package/dist/chunk-C6ZZ62ET.js +0 -2
  78. package/dist/chunk-C6ZZ62ET.js.map +0 -1
  79. package/dist/chunk-EHLRZULM.js +0 -5
  80. package/dist/chunk-GMGZZG6I.js +0 -2
  81. package/dist/chunk-J2AQNXF2.js +0 -2
  82. package/dist/chunk-NPKJ7QFK.js +0 -7
  83. package/dist/chunk-NPKJ7QFK.js.map +0 -1
  84. package/dist/chunk-R4YEFOVE.js +0 -2
  85. package/dist/chunk-R4YEFOVE.js.map +0 -1
  86. package/dist/chunk-SSPABEUR.js +0 -11
  87. package/dist/chunk-UJR6DHXY.js +0 -2
  88. package/dist/chunk-VQXUMFB2.js +0 -21
  89. package/dist/chunk-XH46JNPK.js +0 -2
  90. package/dist/chunk-XH46JNPK.js.map +0 -1
  91. package/dist/chunk-ZGMTY52O.js +0 -2
package/README.md CHANGED
@@ -1,9 +1,12 @@
1
1
  # temporal-fmt 🥶🔥
2
2
 
3
3
  ![coverage](https://img.shields.io/badge/coverage-100%25%20(c8)-brightgreen?style=flat-square)
4
+ [![format subpath size](https://img.shields.io/bundlephobia/minzip/temporal-fmt?path=format&label=format%20subpath)](https://bundlephobia.com/package/temporal-fmt)
5
+ [![parse subpath size](https://img.shields.io/bundlephobia/minzip/temporal-fmt?path=parse&label=parse%20subpath)](https://bundlephobia.com/package/temporal-fmt)
4
6
 
5
- Format `Temporal.PlainDate` / `PlainTime` / `PlainDateTime` / `ZonedDateTime` objects
6
- using date-fns-style token strings.
7
+ Format and parse `Temporal` values (`PlainDate`, `PlainTime`, `PlainDateTime`,
8
+ `ZonedDateTime`) using date-fns-style tokens, with real validation — bad input
9
+ throws instead of silently returning garbage. Locale-aware, no deps.
7
10
 
8
11
  Node 26 shipped native `Temporal` and then pointedly left out a custom-string
9
12
  formatter. TC39's take: use `Intl.DateTimeFormat` and leave string-token syntax
@@ -12,7 +15,8 @@ muscle memory from date-fns, moment, or dayjs, that's a rough adjustment. This
12
15
  library exists so you don't have to make it.
13
16
 
14
17
  Zero dependencies. Native on Node 26+, or bring your own via a polyfill or
15
- `setTemporal()`.
18
+ `setTemporal()`. Import from a subpath (`temporal-fmt/format`, `temporal-fmt/parse`,
19
+ etc.) to pull in only what you use — see [Subpath imports](#subpath-imports).
16
20
 
17
21
  Locale-aware tokens need Node 20+ regardless of which path you use — native
18
22
  on 26+, or falling back to the Temporal implementation's own
@@ -26,10 +30,27 @@ npm install temporal-fmt
26
30
 
27
31
  [View on npm](https://www.npmjs.com/package/temporal-fmt)
28
32
 
29
- This library is genuinely large — locales, recurrence, business calendars, timezone disambiguation, an analyzer, config layers, custom token extensibility, a CLI. A substantial amount of configuration and customization is packed in here. But none of that is required reading. The reason this library exists in the first place is formatting and parsing dates with token strings, and that part stays simple: `format(temporal, formatStr)` and `parse(formatStr, input)`, the same shape as date-fns or Day.js. Read [Providing `Temporal`](#providing-temporal) and [Formatting](#formatting)/[Parsing](#parsing), and you're covered for the common case — everything past that is there for when you actually need it, not before.
33
+ ## Get started
34
+
35
+ ```js
36
+ import { format } from 'temporal-fmt/format';
37
+ import { parse } from 'temporal-fmt/parse';
38
+
39
+ const date = Temporal.PlainDate.from('2026-08-04');
40
+ format(date, 'yyyy-MM-dd'); // "2026-08-04"
41
+
42
+ parse('yyyy-MM-dd HH:mm', '2026-08-04 15:45'); // Temporal.PlainDateTime
43
+ ```
44
+
45
+ That's the whole library for most use cases — `format(temporal, formatStr)` in, `parse(formatStr, input)` out, same shape as date-fns or Day.js. Import from the subpaths (`temporal-fmt/format`, `temporal-fmt/parse`) shown above, not the bare `temporal-fmt` package — a bundler only ships what you actually call that way. Measured with esbuild: ~27KB for `format` alone via the subpath, versus ~68KB for the same function pulled from the bare import.
46
+
47
+ Below Node 26, `Temporal` isn't global yet, so you'll need a polyfill first — see [Providing `Temporal`](#providing-temporal). On Node 26+ the snippet above just works.
48
+
49
+ The package looks large on npm — locales, recurrence, business calendars, timezone disambiguation, an analyzer, config layers, a CLI — but none of that is required reading or required bundle weight. It's there behind its own subpaths for when you need it; see [Subpath imports](#subpath-imports) for the full list and [Formatting](#formatting)/[Parsing](#parsing) for the details on the two functions above.
30
50
 
31
51
  ## Contents
32
52
 
53
+ - [Get started](#get-started)
33
54
  - [Providing `Temporal`](#providing-temporal)
34
55
  - [Formatting](#formatting)
35
56
  - [Parsing](#parsing)
@@ -71,7 +92,8 @@ Use a polyfill like [`temporal-polyfill`](https://github.com/fullcalendar/tempor
71
92
 
72
93
  ```js
73
94
  import 'temporal-polyfill/global'
74
- import { format, parse } from 'temporal-fmt';
95
+ import { format } from 'temporal-fmt/format';
96
+ import { parse } from 'temporal-fmt/parse';
75
97
 
76
98
  parse(...);
77
99
  ```
@@ -82,7 +104,9 @@ Set a Temporal implementation explicitly, once, before your app's first `format(
82
104
 
83
105
  ```js
84
106
  import { Temporal } from 'temporal-polyfill/full';
85
- import { setTemporal, format, parse } from 'temporal-fmt';
107
+ import { setTemporal } from 'temporal-fmt';
108
+ import { format } from 'temporal-fmt/format';
109
+ import { parse } from 'temporal-fmt/parse';
86
110
 
87
111
  setTemporal(Temporal); // once, before using format or parse
88
112
  ```
@@ -94,7 +118,7 @@ Anything that constructs a `Temporal` value from scratch needs this — `parse()
94
118
  ## Formatting
95
119
 
96
120
  ```js
97
- import { format } from 'temporal-fmt';
121
+ import { format } from 'temporal-fmt/format';
98
122
 
99
123
  const date = Temporal.PlainDate.from('2026-08-04');
100
124
  format(date, 'yyyy-MM-dd'); // "2026-08-04"
@@ -120,7 +144,7 @@ Try a token your input type doesn't support — `HH` on a `PlainDate`, say — a
120
144
  `parse()` builds a real `Temporal.PlainDate` / `PlainTime` / `PlainDateTime` / `ZonedDateTime` out of a string, picking whichever type fits the tokens present:
121
145
 
122
146
  ```js
123
- import { parse } from 'temporal-fmt';
147
+ import { parse } from 'temporal-fmt/parse';
124
148
 
125
149
  parse('yyyy-MM-dd HH:mm', '2026-08-04 15:45'); // Temporal.PlainDateTime
126
150
  parse('yyyy-MM', '2026-08-04T15:45:30'); // throws — shape doesn't match
@@ -836,6 +860,8 @@ A handful of functions exist specifically to feed editor tooling — autocomplet
836
860
 
837
861
  The CLI ships in this package (`scripts/cli.mjs`) and reads/writes stdin/stdout. Run it via `npm run cli` inside a checkout of this repo, or `node scripts/cli.mjs` directly. Called with a subcommand it runs once and exits, same as any Unix tool — fine for scripts and CI:
838
862
 
863
+ On Node 26+ the CLI uses native `Temporal` and needs nothing extra. Below that, it looks for a globally-installed [`temporal-polyfill`](https://github.com/fullcalendar/temporal-polyfill) and exits with an install hint if it can't find one — `temporal-fmt` itself ships with zero dependencies, so this one's on you: `npm install temporal-polyfill`.
864
+
839
865
  ```sh
840
866
  temporal-fmt format "2026-08-04T15:45:30" "yyyy-MM-dd HH:mm:ss"
841
867
  temporal-fmt parse "yyyy-MM-dd" "2026-08-04"
@@ -888,6 +914,8 @@ import { registerLocale } from 'temporal-fmt/locale';
888
914
 
889
915
  The rest of the API (arithmetic, comparison, rounding, intervals-adjacent helpers not listed above, business calendars, holidays, serialization, config, type guards, typed errors, the analyzer, and IDE tooling data) is only available from the main `temporal-fmt` entry point — there's no dedicated subpath for those yet.
890
916
 
917
+ `sideEffects: false` is set in `package.json`, so a bundler with tree-shaking enabled genuinely drops what you don't import. Measured with esbuild: `import { format } from 'temporal-fmt/format'` bundles to ~27KB, versus ~68KB for the same single function pulled from the bare `temporal-fmt` entry — the main entry point re-exports everything, so anything imported from it drags the whole graph along regardless of what you actually call. If bundle size matters for your use case, import from the subpath, not the package root.
918
+
891
919
  ## Migrating from Day.js or date-fns
892
920
 
893
921
  ### Token mapping
@@ -1,5 +1,260 @@
1
- "use strict";var s=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var V=(e,n)=>{for(var r in n)s(e,r,{get:n[r],enumerable:!0})},W=(e,n,r,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let a of F(n))!w.call(e,a)&&a!==r&&s(e,a,{get:()=>n[a],enumerable:!(t=x(n,a))||t.enumerable});return e};var Y=e=>W(s({},"__esModule",{value:!0}),e);var H={};V(H,{asDateFieldView:()=>h,dayOfYearHelper:()=>S,daysInMonth:()=>p,daysInYear:()=>E,endOf:()=>P,getMonth:()=>R,getQuarter:()=>L,getWeekday:()=>j,isLeapMonth:()=>_,isLeapYear:()=>M,monthsInYear:()=>I,startOf:()=>C,weekOfYear:()=>A,weekYear:()=>N});module.exports=Y(H);var g=[0,31,59,90,120,151,181,212,243,273,304,334];function d(e){return e%4===0&&e%100!==0||e%400===0}function u(e){return d(e)?366:365}function y(e,n,r){let t=g[n-1]+r;return n>2&&d(e)&&(t+=1),t}var l=2e3;function T(e){let n=0;if(e>=l)for(let t=l;t<e;t++)n+=u(t);else for(let t=e;t<l;t++)n-=u(t);return((5+n)%7+7)%7+1}function c(e,n,r,t){let m=y(e,n,r)+(4-t),i,f;m<1?(i=e-1,f=m+u(i)):m>u(e)?(i=e+1,f=m-u(e)):(i=e,f=m);let k=1+(4-T(i)+7)%7,O=1+Math.floor((f-k)/7);return{isoYear:i,week:O}}function o(e,n){for(let r of n)if(typeof e[r]!="number")throw new Error(`temporal-fmt: calendar helper requires "${String(r)}", which this value doesn't have. Pass a Temporal.PlainDate / PlainDateTime / ZonedDateTime.`)}function p(e){o(e,["year","month"]);let{year:n,month:r}=e,t=[31,28,31,30,31,30,31,31,30,31,30,31];return r===2&&d(n)?29:t[r-1]}function E(e){return o(e,["year"]),d(e.year)?366:365}function I(e){return 12}function M(e){return o(e,["year"]),d(e.year)}function _(e){return!1}function S(e){return o(e,["year","month","day"]),y(e.year,e.month,e.day)}function A(e){return o(e,["year","month","day","dayOfWeek"]),c(e.year,e.month,e.day,e.dayOfWeek).week}function N(e){return o(e,["year","month","day","dayOfWeek"]),c(e.year,e.month,e.day,e.dayOfWeek).isoYear}function U(e){if(!Number.isInteger(e)||e<1||e>12)throw new Error(`temporal-fmt: getQuarter's startMonth must be an integer from 1 to 12 (got ${e}).`)}function L(e,n={}){o(e,["month"]);let r=n.startMonth??1;if(U(r),r===1)return Math.ceil(e.month/3);let t=(e.month-r+12)%12+1;return Math.ceil(t/3)}function R(e){return o(e,["month"]),e.month}function j(e){return o(e,["dayOfWeek"]),e.dayOfWeek}function b(e){return e==="day"||e==="month"||e==="year"}function D(e){if(typeof e.dayOfWeek!="number")return;/* c8 ignore start @preserve -- unreachable: startOf()/endOf() both call
2
- * asDateFieldView() before this, which already throws if year/month/day
3
- * aren't all numbers — so by the time a value with a numeric dayOfWeek
4
- * reaches here, year/month/day are guaranteed present too. */if(typeof e.year!="number"||typeof e.month!="number"||typeof e.day!="number")return;/* c8 ignore stop @preserve */let n=new Date(Date.UTC(e.year,e.month-1,e.day)).getUTCDay();e.dayOfWeek=n===0?7:n}function C(e,n){let t={...h(e)};return n==="year"?(t.month=1,t.day=1):n==="month"&&(t.day=1),D(t),b(n)?(t.hour=0,t.minute=0,t.second=0,t.millisecond=0):n==="hour"?(t.minute=0,t.second=0,t.millisecond=0):n==="minute"?(t.second=0,t.millisecond=0):n==="second"&&(t.millisecond=0),t}function P(e,n){let t={...h(e)};return n==="year"?(t.month=12,t.day=p({year:t.year,month:12})):n==="month"&&(t.day=p({year:t.year,month:t.month})),D(t),b(n)?(t.hour=23,t.minute=59,t.second=59,t.millisecond=999):n==="hour"?(t.minute=59,t.second=59,t.millisecond=999):n==="minute"?(t.second=59,t.millisecond=999):n==="second"&&(t.millisecond=999),t}function h(e){if(typeof e!="object"||e===null)throw new Error(`temporal-fmt: expected a date-carrying Temporal value, got ${String(e)}.`);let n=e,r={};if(typeof n.year=="number"&&(r.year=n.year),typeof n.month=="number"&&(r.month=n.month),typeof n.day=="number"&&(r.day=n.day),typeof n.hour=="number"&&(r.hour=n.hour),typeof n.minute=="number"&&(r.minute=n.minute),typeof n.second=="number"&&(r.second=n.second),typeof n.millisecond=="number"&&(r.millisecond=n.millisecond),typeof n.dayOfWeek=="number"&&(r.dayOfWeek=n.dayOfWeek),typeof n.calendarId=="string"&&(r.calendarId=n.calendarId),r.year===void 0||r.month===void 0||r.day===void 0)throw new Error("temporal-fmt: value is missing year/month/day fields \u2014 pass a Temporal.PlainDate / PlainDateTime / ZonedDateTime.");return r}0&&(module.exports={asDateFieldView,dayOfYearHelper,daysInMonth,daysInYear,endOf,getMonth,getQuarter,getWeekday,isLeapMonth,isLeapYear,monthsInYear,startOf,weekOfYear,weekYear});
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/calendarUtils.ts
21
+ var calendarUtils_exports = {};
22
+ __export(calendarUtils_exports, {
23
+ asDateFieldView: () => asDateFieldView,
24
+ dayOfYearHelper: () => dayOfYearHelper,
25
+ daysInMonth: () => daysInMonth,
26
+ daysInYear: () => daysInYear2,
27
+ endOf: () => endOf,
28
+ getMonth: () => getMonth,
29
+ getQuarter: () => getQuarter,
30
+ getWeekday: () => getWeekday,
31
+ isLeapMonth: () => isLeapMonth,
32
+ isLeapYear: () => isLeapYear,
33
+ monthsInYear: () => monthsInYear,
34
+ startOf: () => startOf,
35
+ weekOfYear: () => weekOfYear,
36
+ weekYear: () => weekYear
37
+ });
38
+ module.exports = __toCommonJS(calendarUtils_exports);
39
+
40
+ // src/isoWeek.ts
41
+ var CUMULATIVE_DAYS_BY_MONTH = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
42
+ function isGregorianLeapYear(year) {
43
+ return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
44
+ }
45
+ function daysInYear(year) {
46
+ return isGregorianLeapYear(year) ? 366 : 365;
47
+ }
48
+ function dayOfYear(year, month, day) {
49
+ let doy = CUMULATIVE_DAYS_BY_MONTH[month - 1] + day;
50
+ if (month > 2 && isGregorianLeapYear(year)) doy += 1;
51
+ return doy;
52
+ }
53
+ var REFERENCE_YEAR = 2e3;
54
+ function dayOfWeekOfJan1(year) {
55
+ let offset = 0;
56
+ if (year >= REFERENCE_YEAR) {
57
+ for (let y = REFERENCE_YEAR; y < year; y++) offset += daysInYear(y);
58
+ } else {
59
+ for (let y = year; y < REFERENCE_YEAR; y++) offset -= daysInYear(y);
60
+ }
61
+ const zeroIndexed = ((6 - 1 + offset) % 7 + 7) % 7;
62
+ return zeroIndexed + 1;
63
+ }
64
+ function isoWeekYearAndWeek(year, month, day, dayOfWeek) {
65
+ const doy = dayOfYear(year, month, day);
66
+ const thursdayDoyRelative = doy + (4 - dayOfWeek);
67
+ let isoYear;
68
+ let thursdayDoy;
69
+ if (thursdayDoyRelative < 1) {
70
+ isoYear = year - 1;
71
+ thursdayDoy = thursdayDoyRelative + daysInYear(isoYear);
72
+ } else if (thursdayDoyRelative > daysInYear(year)) {
73
+ isoYear = year + 1;
74
+ thursdayDoy = thursdayDoyRelative - daysInYear(year);
75
+ } else {
76
+ isoYear = year;
77
+ thursdayDoy = thursdayDoyRelative;
78
+ }
79
+ const jan1Dow = dayOfWeekOfJan1(isoYear);
80
+ const firstThursdayDoy = 1 + (4 - jan1Dow + 7) % 7;
81
+ const week = 1 + Math.floor((thursdayDoy - firstThursdayDoy) / 7);
82
+ return { isoYear, week };
83
+ }
84
+
85
+ // src/calendarUtils.ts
86
+ function requireFields(view, fields) {
87
+ for (const f of fields) {
88
+ if (typeof view[f] !== "number") {
89
+ throw new Error(
90
+ `temporal-fmt: calendar helper requires "${String(f)}", which this value doesn't have. Pass a Temporal.PlainDate / PlainDateTime / ZonedDateTime.`
91
+ );
92
+ }
93
+ }
94
+ }
95
+ function daysInMonth(view) {
96
+ requireFields(view, ["year", "month"]);
97
+ const { year, month } = view;
98
+ const LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
99
+ if (month === 2 && isGregorianLeapYear(year)) return 29;
100
+ return LENGTHS[month - 1];
101
+ }
102
+ function daysInYear2(view) {
103
+ requireFields(view, ["year"]);
104
+ return isGregorianLeapYear(view.year) ? 366 : 365;
105
+ }
106
+ function monthsInYear(_view) {
107
+ return 12;
108
+ }
109
+ function isLeapYear(view) {
110
+ requireFields(view, ["year"]);
111
+ return isGregorianLeapYear(view.year);
112
+ }
113
+ function isLeapMonth(_view) {
114
+ return false;
115
+ }
116
+ function dayOfYearHelper(view) {
117
+ requireFields(view, ["year", "month", "day"]);
118
+ return dayOfYear(view.year, view.month, view.day);
119
+ }
120
+ function weekOfYear(view) {
121
+ requireFields(view, ["year", "month", "day", "dayOfWeek"]);
122
+ return isoWeekYearAndWeek(view.year, view.month, view.day, view.dayOfWeek).week;
123
+ }
124
+ function weekYear(view) {
125
+ requireFields(view, ["year", "month", "day", "dayOfWeek"]);
126
+ return isoWeekYearAndWeek(view.year, view.month, view.day, view.dayOfWeek).isoYear;
127
+ }
128
+ function validateStartMonth(startMonth) {
129
+ if (!Number.isInteger(startMonth) || startMonth < 1 || startMonth > 12) {
130
+ throw new Error(
131
+ `temporal-fmt: getQuarter's startMonth must be an integer from 1 to 12 (got ${startMonth}).`
132
+ );
133
+ }
134
+ }
135
+ function getQuarter(view, options = {}) {
136
+ requireFields(view, ["month"]);
137
+ const startMonth = options.startMonth ?? 1;
138
+ validateStartMonth(startMonth);
139
+ if (startMonth === 1) {
140
+ return Math.ceil(view.month / 3);
141
+ }
142
+ const shifted = (view.month - startMonth + 12) % 12 + 1;
143
+ return Math.ceil(shifted / 3);
144
+ }
145
+ function getMonth(view) {
146
+ requireFields(view, ["month"]);
147
+ return view.month;
148
+ }
149
+ function getWeekday(view) {
150
+ requireFields(view, ["dayOfWeek"]);
151
+ return view.dayOfWeek;
152
+ }
153
+ function touchesTime(unit) {
154
+ return unit === "day" || unit === "month" || unit === "year";
155
+ }
156
+ function recomputeDayOfWeek(view) {
157
+ if (typeof view.dayOfWeek !== "number") return;
158
+ /* c8 ignore start @preserve -- unreachable: startOf()/endOf() both call
159
+ * asDateFieldView() before this, which already throws if year/month/day
160
+ * aren't all numbers — so by the time a value with a numeric dayOfWeek
161
+ * reaches here, year/month/day are guaranteed present too. */
162
+ if (typeof view.year !== "number" || typeof view.month !== "number" || typeof view.day !== "number") return;
163
+ /* c8 ignore stop @preserve */
164
+ const jsDow = new Date(Date.UTC(view.year, view.month - 1, view.day)).getUTCDay();
165
+ view.dayOfWeek = jsDow === 0 ? 7 : jsDow;
166
+ }
167
+ function startOf(value, unit) {
168
+ const view = asDateFieldView(value);
169
+ const result = { ...view };
170
+ if (unit === "year") {
171
+ result.month = 1;
172
+ result.day = 1;
173
+ } else if (unit === "month") {
174
+ result.day = 1;
175
+ }
176
+ recomputeDayOfWeek(result);
177
+ if (touchesTime(unit)) {
178
+ result.hour = 0;
179
+ result.minute = 0;
180
+ result.second = 0;
181
+ result.millisecond = 0;
182
+ } else if (unit === "hour") {
183
+ result.minute = 0;
184
+ result.second = 0;
185
+ result.millisecond = 0;
186
+ } else if (unit === "minute") {
187
+ result.second = 0;
188
+ result.millisecond = 0;
189
+ } else if (unit === "second") {
190
+ result.millisecond = 0;
191
+ }
192
+ return result;
193
+ }
194
+ function endOf(value, unit) {
195
+ const view = asDateFieldView(value);
196
+ const result = { ...view };
197
+ if (unit === "year") {
198
+ result.month = 12;
199
+ result.day = daysInMonth({ year: result.year, month: 12 });
200
+ } else if (unit === "month") {
201
+ result.day = daysInMonth({ year: result.year, month: result.month });
202
+ }
203
+ recomputeDayOfWeek(result);
204
+ if (touchesTime(unit)) {
205
+ result.hour = 23;
206
+ result.minute = 59;
207
+ result.second = 59;
208
+ result.millisecond = 999;
209
+ } else if (unit === "hour") {
210
+ result.minute = 59;
211
+ result.second = 59;
212
+ result.millisecond = 999;
213
+ } else if (unit === "minute") {
214
+ result.second = 59;
215
+ result.millisecond = 999;
216
+ } else if (unit === "second") {
217
+ result.millisecond = 999;
218
+ }
219
+ return result;
220
+ }
221
+ function asDateFieldView(value) {
222
+ if (typeof value !== "object" || value === null) {
223
+ throw new Error(`temporal-fmt: expected a date-carrying Temporal value, got ${String(value)}.`);
224
+ }
225
+ const v = value;
226
+ const out = {};
227
+ if (typeof v.year === "number") out.year = v.year;
228
+ if (typeof v.month === "number") out.month = v.month;
229
+ if (typeof v.day === "number") out.day = v.day;
230
+ if (typeof v.hour === "number") out.hour = v.hour;
231
+ if (typeof v.minute === "number") out.minute = v.minute;
232
+ if (typeof v.second === "number") out.second = v.second;
233
+ if (typeof v.millisecond === "number") out.millisecond = v.millisecond;
234
+ if (typeof v.dayOfWeek === "number") out.dayOfWeek = v.dayOfWeek;
235
+ if (typeof v.calendarId === "string") out.calendarId = v.calendarId;
236
+ if (out.year === void 0 || out.month === void 0 || out.day === void 0) {
237
+ throw new Error(
238
+ `temporal-fmt: value is missing year/month/day fields \u2014 pass a Temporal.PlainDate / PlainDateTime / ZonedDateTime.`
239
+ );
240
+ }
241
+ return out;
242
+ }
243
+ // Annotate the CommonJS export names for ESM import in node:
244
+ 0 && (module.exports = {
245
+ asDateFieldView,
246
+ dayOfYearHelper,
247
+ daysInMonth,
248
+ daysInYear,
249
+ endOf,
250
+ getMonth,
251
+ getQuarter,
252
+ getWeekday,
253
+ isLeapMonth,
254
+ isLeapYear,
255
+ monthsInYear,
256
+ startOf,
257
+ weekOfYear,
258
+ weekYear
259
+ });
5
260
  //# sourceMappingURL=calendarUtils.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/calendarUtils.ts","../src/isoWeek.ts"],"sourcesContent":["// Calendar utility helpers. These are pure functions\n// over the TemporalLike shape — no Temporal namespace needed, same\n// approach as isoWeek.ts and the field-reading helpers in format.ts.\n// Letting callers compute dayOfYear/weekOfYear/etc. without going\n// through format() means they can build their own derived values\n// without committing to a string format.\n//\n// Calendar-sensitivity: the helpers in this module assume the\n// iso8601 (Gregorian) calendar — that's what TemporalLike fields\n// carry for the overwhelming majority of callers. Non-Gregorian\n// calendars (hebrew, islamic, etc.) need their own helpers; this\n// module doesn't try to be calendar-polymorphic the way Temporal\n// itself is. Documented limitation, not a design choice — see\n// VERIFICATION.md for the rationale.\n\nimport { isGregorianLeapYear, dayOfYear, isoWeekYearAndWeek } from './isoWeek.js';\n\n// A subset of TemporalLike that has the date fields these helpers need,\n// plus optional time fields. PlainTime isn't a DateFieldView (no\n// year/month/day), but PlainDateTime / ZonedDateTime / PlainDate all\n// match. Time fields are optional so callers can pass a PlainDate\n// to startOf(value, 'month') without having to populate hour/minute/etc.\n// Exported so the comparison/arithmetic modules can use the same\n// narrowing.\nexport interface DateFieldView {\n year?: number;\n month?: number;\n day?: number;\n hour?: number;\n minute?: number;\n second?: number;\n millisecond?: number;\n dayOfWeek?: number;\n calendarId?: string;\n}\n\nfunction requireFields(view: DateFieldView, fields: Array<keyof DateFieldView>): void {\n for (const f of fields) {\n if (typeof view[f] !== 'number') {\n throw new Error(\n `temporal-fmt: calendar helper requires \"${String(f)}\", which this value doesn't have. ` +\n `Pass a Temporal.PlainDate / PlainDateTime / ZonedDateTime.`\n );\n }\n }\n}\n\nexport function daysInMonth(view: DateFieldView): number {\n requireFields(view, ['year', 'month']);\n const { year, month } = view;\n // Standard Gregorian month lengths. February's length depends on\n // whether `year` is a leap year — the same isGregorianLeapYear check\n // isoWeek.ts uses for dayOfYear arithmetic.\n const LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];\n if (month === 2 && isGregorianLeapYear(year!)) return 29;\n return LENGTHS[(month! - 1)!]!;\n}\n\nexport function daysInYear(view: DateFieldView): 365 | 366 {\n requireFields(view, ['year']);\n return isGregorianLeapYear(view.year!) ? 366 : 365;\n}\n\nexport function monthsInYear(_view: DateFieldView): 12 {\n // Gregorian always has 12 months. Other calendars (Hebrew leap years\n // have 13) need calendar-aware logic this module doesn't carry — see\n // the file-level comment. The `_view` parameter is kept so the\n // signature mirrors the other helpers and a future calendar-aware\n // implementation can use it without changing call sites.\n return 12;\n}\n\nexport function isLeapYear(view: DateFieldView): boolean {\n requireFields(view, ['year']);\n return isGregorianLeapYear(view.year!);\n}\n\n// `isLeapMonth` would require knowing which month of a leap-year-aware\n// calendar is the leap month — Gregorian doesn't have one, so this\n// returns false unconditionally. Kept here so the public surface\n// matches the module's listing; non-Gregorian calendars need\n// a different implementation.\nexport function isLeapMonth(_view: DateFieldView): boolean {\n return false;\n}\n\nexport function dayOfYearHelper(view: DateFieldView): number {\n requireFields(view, ['year', 'month', 'day']);\n return dayOfYear(view.year!, view.month!, view.day!);\n}\n\n// ISO 8601 week and week-year. Delegates to isoWeek.ts's\n// isoWeekYearAndWeek, which does the full Thursday-of-week\n// computation to handle the year-boundary cases (Dec 29-31 belonging\n// to week 1 of next year, Jan 1-3 belonging to week 52/53 of the\n// previous year).\nexport function weekOfYear(view: DateFieldView): number {\n requireFields(view, ['year', 'month', 'day', 'dayOfWeek']);\n return isoWeekYearAndWeek(view.year!, view.month!, view.day!, view.dayOfWeek!).week;\n}\n\nexport function weekYear(view: DateFieldView): number {\n requireFields(view, ['year', 'month', 'day', 'dayOfWeek']);\n return isoWeekYearAndWeek(view.year!, view.month!, view.day!, view.dayOfWeek!).isoYear;\n}\n\n/**\n * Fiscal-quarter options. `startMonth` is the calendar month (1-12) the\n * fiscal year begins on — e.g. `7` for a fiscal year starting in July.\n * Omitted or `1` gives the calendar-quarter behavior getQuarter() has\n * always had (Jan-Mar = Q1, etc.), so existing callers passing nothing\n * see no change.\n */\nexport interface QuarterOptions {\n startMonth?: number;\n}\n\nfunction validateStartMonth(startMonth: number): void {\n if (!Number.isInteger(startMonth) || startMonth < 1 || startMonth > 12) {\n throw new Error(\n `temporal-fmt: getQuarter's startMonth must be an integer from 1 to 12 (got ${startMonth}).`\n );\n }\n}\n\nexport function getQuarter(view: DateFieldView, options: QuarterOptions = {}): number {\n requireFields(view, ['month']);\n const startMonth = options.startMonth ?? 1;\n validateStartMonth(startMonth);\n if (startMonth === 1) {\n // Mirrors the Q token: months 1-3 → Q1, 4-6 → Q2, 7-9 → Q3, 10-12 → Q4.\n return Math.ceil(view.month! / 3);\n }\n // Fiscal case: shift the month so startMonth becomes month 1 of the\n // fiscal year (mod 12, 1-indexed), then apply the same ceil(/3) rule.\n // E.g. startMonth=7 (fiscal year starts July): July→1, Aug→2, ...,\n // Dec→6, Jan→7, ..., June→12. Then Q1 = fiscal months 1-3 (Jul-Sep),\n // matching the common \"FY starts in July\" convention where Q1 is the\n // first quarter of the fiscal year, not a quarter numbered by which\n // calendar quarter it falls in.\n const shifted = ((view.month! - startMonth + 12) % 12) + 1;\n return Math.ceil(shifted / 3);\n}\n\n// `getMonth` / `getWeekday` look trivial (just read the field) but\n// they're kept as real functions for API surface consistency with the\n// rest of this module. They also normalize: getWeekday returns 1-7\n// (Mon-Sun, matching Temporal's spec) regardless of what numbering the\n// caller's underlying value uses.\nexport function getMonth(view: DateFieldView): number {\n requireFields(view, ['month']);\n return view.month!;\n}\n\nexport function getWeekday(view: DateFieldView): number {\n requireFields(view, ['dayOfWeek']);\n return view.dayOfWeek!;\n}\n\n// startOf / endOf return new field bags (not Temporal objects — this\n// module is polyfill-free) with the relevant fields zeroed/extended.\n// Callers can pass the result to a Temporal constructor if they want\n// a typed value.\nexport type StartOfUnit = 'day' | 'month' | 'year' | 'hour' | 'minute' | 'second';\n\n// Returns true if the given unit (when used with startOf/endOf) should\n// also touch the time fields. 'day', 'month', 'year' all imply a\n// resolution coarser than an hour, so startOf zeroes the time fields\n// and endOf maxes them. Sub-hour units (hour/minute/second) only touch\n// the fields finer than themselves.\nfunction touchesTime(unit: StartOfUnit): boolean {\n return unit === 'day' || unit === 'month' || unit === 'year';\n}\n\n// startOf/endOf reassign year/month/day, which invalidates any\n// dayOfWeek carried over from the input — a plain { ...view } spread\n// leaves the old value sitting there unchanged. Same failure mode\n// businessCalendar.ts's isBusinessDay() works around for add(); we\n// recompute here rather than trust the copied field.\nfunction recomputeDayOfWeek(view: DateFieldView): void {\n if (typeof view.dayOfWeek !== 'number') return;\n /* c8 ignore start @preserve -- unreachable: startOf()/endOf() both call\n * asDateFieldView() before this, which already throws if year/month/day\n * aren't all numbers — so by the time a value with a numeric dayOfWeek\n * reaches here, year/month/day are guaranteed present too. */\n if (typeof view.year !== 'number' || typeof view.month !== 'number' || typeof view.day !== 'number') return;\n /* c8 ignore stop @preserve */\n const jsDow = new Date(Date.UTC(view.year, view.month - 1, view.day)).getUTCDay(); // 0=Sun..6=Sat\n view.dayOfWeek = jsDow === 0 ? 7 : jsDow; // 1=Mon..7=Sun\n}\n\nexport function startOf(value: unknown, unit: StartOfUnit): DateFieldView {\n const view = asDateFieldView(value);\n const result: DateFieldView = { ...view };\n if (unit === 'year') {\n result.month = 1;\n result.day = 1;\n } else if (unit === 'month') {\n result.day = 1;\n }\n recomputeDayOfWeek(result);\n if (touchesTime(unit)) {\n result.hour = 0;\n result.minute = 0;\n result.second = 0;\n result.millisecond = 0;\n } else if (unit === 'hour') {\n result.minute = 0;\n result.second = 0;\n result.millisecond = 0;\n } else if (unit === 'minute') {\n result.second = 0;\n result.millisecond = 0;\n } else if (unit === 'second') {\n result.millisecond = 0;\n }\n return result;\n}\n\nexport function endOf(value: unknown, unit: StartOfUnit): DateFieldView {\n const view = asDateFieldView(value);\n const result: DateFieldView = { ...view };\n if (unit === 'year') {\n result.month = 12;\n result.day = daysInMonth({ year: result.year!, month: 12 });\n } else if (unit === 'month') {\n result.day = daysInMonth({ year: result.year!, month: result.month! });\n }\n recomputeDayOfWeek(result);\n if (touchesTime(unit)) {\n result.hour = 23;\n result.minute = 59;\n result.second = 59;\n result.millisecond = 999;\n } else if (unit === 'hour') {\n result.minute = 59;\n result.second = 59;\n result.millisecond = 999;\n } else if (unit === 'minute') {\n result.second = 59;\n result.millisecond = 999;\n } else if (unit === 'second') {\n result.millisecond = 999;\n }\n return result;\n}\n\n// Type-narrowing helpers used by the comparison/arithmetic modules.\n// Lets them accept any of the four date-carrying Temporal types without\n// importing Temporal itself.\nexport function asDateFieldView(value: unknown): DateFieldView {\n if (typeof value !== 'object' || value === null) {\n throw new Error(`temporal-fmt: expected a date-carrying Temporal value, got ${String(value)}.`);\n }\n // Temporal instances expose year/month/day/etc. as prototype getters,\n // not own enumerable properties — so `{ ...value }` would lose them.\n // Read them explicitly. Only the fields actually present on this\n // value type end up in the returned view.\n const v = value as Record<string, unknown>;\n const out: DateFieldView = {};\n if (typeof v.year === 'number') out.year = v.year;\n if (typeof v.month === 'number') out.month = v.month;\n if (typeof v.day === 'number') out.day = v.day;\n if (typeof v.hour === 'number') out.hour = v.hour;\n if (typeof v.minute === 'number') out.minute = v.minute;\n if (typeof v.second === 'number') out.second = v.second;\n if (typeof v.millisecond === 'number') out.millisecond = v.millisecond;\n if (typeof v.dayOfWeek === 'number') out.dayOfWeek = v.dayOfWeek;\n if (typeof v.calendarId === 'string') out.calendarId = v.calendarId;\n if (out.year === undefined || out.month === undefined || out.day === undefined) {\n throw new Error(\n `temporal-fmt: value is missing year/month/day fields — pass a Temporal.PlainDate / PlainDateTime / ZonedDateTime.`\n );\n }\n return out;\n}\n\n// Re-export the TemporalType alias so callers can import everything\n// from one place.\nexport type { TemporalType } from './tokenMetadata.js';\n// Re-export TemporalLike for the same reason.\nexport type { TemporalLike } from './tokens.js';","// ISO 8601 week numbering: a week runs Monday–Sunday, and week 1 of a year\n// is the week containing the year's first Thursday (equivalently, the week\n// containing January 4). This means late-December dates can fall in week 1\n// of the *next* year, and early-January dates can fall in week 52 or 53 of\n// the *previous* year. The \"ISO week-numbering year\" (what `RRRR` formats)\n// is that adjacent year, not the calendar year.\n//\n// Computed here from the date's own year/month/day plus its ISO dayOfWeek\n// (1=Mon..7=Sun, matching Temporal's numbering) using plain Gregorian\n// arithmetic — no Temporal factory needed. format() only has the fields\n// the caller already put on the object, and requiring a Temporal\n// implementation just for ISO week would be a regression for callers who\n// use format() without setTemporal() on a non-26 Node.\n\nconst DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];\nconst CUMULATIVE_DAYS_BY_MONTH = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];\n\nexport function isGregorianLeapYear(year: number): boolean {\n // Gregorian rule: divisible by 4, except centuries which must also be\n // divisible by 400. Temporal's iso8601 calendar is proleptic Gregorian\n // (no Julian cutover), so this applies for every year, including BCE.\n return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;\n}\n\nfunction daysInYear(year: number): 365 | 366 {\n return isGregorianLeapYear(year) ? 366 : 365;\n}\n\nexport function dayOfYear(year: number, month: number, day: number): number {\n let doy = CUMULATIVE_DAYS_BY_MONTH[month - 1]! + day;\n if (month > 2 && isGregorianLeapYear(year)) doy += 1;\n return doy;\n}\n\n// Jan 1, 2000 was a Saturday — ISO dayOfWeek 6. Anchoring day-of-week\n// computations to a known reference date is simpler and cheaper than\n// pulling in Zeller's congruence, and the reference never changes.\nconst REFERENCE_YEAR = 2000;\nconst REFERENCE_JAN1_DAY_OF_WEEK = 6;\n\nfunction dayOfWeekOfJan1(year: number): number {\n // Sum full-year deltas from the 2000 anchor rather than recomputing from\n // scratch each call — the per-call work is a single mod this way, and\n // the loop rarely runs far (a typical caller passes a current-era year).\n let offset = 0;\n if (year >= REFERENCE_YEAR) {\n for (let y = REFERENCE_YEAR; y < year; y++) offset += daysInYear(y);\n } else {\n for (let y = year; y < REFERENCE_YEAR; y++) offset -= daysInYear(y);\n }\n // Convert \"days since Jan 1, 2000\" into an ISO day-of-week (1=Mon..7=Sun).\n // Jan 1, 2000 was ISO 6 (Sat), so zero-indexed dow = (6-1 + offset) mod 7.\n const zeroIndexed = (((6 - 1 + offset) % 7) + 7) % 7;\n return zeroIndexed + 1;\n}\n\nexport interface IsoWeekDate {\n isoYear: number;\n week: number; // 1..53\n}\n\nexport function isoWeekYearAndWeek(year: number, month: number, day: number, dayOfWeek: number): IsoWeekDate {\n // Step 1: find the Thursday of the current ISO week. The ISO week-numbering\n // year is whichever calendar year that Thursday falls in. Computing it via\n // day-of-year offsets (rather than constructing a Temporal.PlainDate and\n // adding days) keeps this function pure-numeric.\n const doy = dayOfYear(year, month, day);\n const thursdayDoyRelative = doy + (4 - dayOfWeek); // may be <1 or >daysInYear\n\n let isoYear: number;\n let thursdayDoy: number;\n if (thursdayDoyRelative < 1) {\n isoYear = year - 1;\n thursdayDoy = thursdayDoyRelative + daysInYear(isoYear);\n } else if (thursdayDoyRelative > daysInYear(year)) {\n isoYear = year + 1;\n thursdayDoy = thursdayDoyRelative - daysInYear(year);\n } else {\n isoYear = year;\n thursdayDoy = thursdayDoyRelative;\n }\n\n // Step 2: locate the first Thursday of isoYear — its week is week 1. The\n // first Thursday's day-of-year depends on what weekday Jan 1 of isoYear is.\n const jan1Dow = dayOfWeekOfJan1(isoYear);\n const firstThursdayDoy = 1 + ((4 - jan1Dow + 7) % 7); // 1..7\n\n // Step 3: count full weeks between the two Thursdays.\n const week = 1 + Math.floor((thursdayDoy - firstThursdayDoy) / 7);\n return { isoYear, week };\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,EAAA,oBAAAC,EAAA,gBAAAC,EAAA,eAAAC,EAAA,UAAAC,EAAA,aAAAC,EAAA,eAAAC,EAAA,eAAAC,EAAA,gBAAAC,EAAA,eAAAC,EAAA,iBAAAC,EAAA,YA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AE,KACvC,OAAOA,EAAE,MAAS,WAAUC,EAAI,KAAOD,EAAE,MACzC,OAAOA,EAAE,QAAW,WAAUC,EAAI,OAASD,EAAE,QAC7C,OAAOA,EAAE,QAAW,WAAUC,EAAI,OAASD,EAAE,QAC7C,OAAOA,EAAE,aAAgB,WAAUC,EAAI,YAAcD,EAAE,aACvD,OAAOA,EAAE,WAAc,WAAUC,EAAI,UAAYD,EAAE,WACnD,OAAOA,EAAE,YAAe,WAAUC,EAAI,WAAaD,EAAE,YACrDC,EAAI,OAAS,QAAaA,EAAI,QAAU,QAAaA,EAAI,MAAQ,OACnE,MAAM,IAAI,MACR,wHACF,EAEF,OAAOA,CACT","names":["calendarUtils_exports","__export","asDateFieldView","dayOfYearHelper","daysInMonth","daysInYear","endOf","getMonth","getQuarter","getWeekday","isLeapMonth","isLeapYear","monthsInYear","startOf","weekOfYear","weekYear","__toCommonJS","CUMULATIVE_DAYS_BY_MONTH","isGregorianLeapYear","year","daysInYear","dayOfYear","month","day","doy","REFERENCE_YEAR","dayOfWeekOfJan1","year","offset","REFERENCE_YEAR","y","daysInYear","isoWeekYearAndWeek","month","day","dayOfWeek","thursdayDoyRelative","dayOfYear","isoYear","thursdayDoy","firstThursdayDoy","week","requireFields","view","fields","f","daysInMonth","year","month","LENGTHS","isGregorianLeapYear","daysInYear","monthsInYear","_view","isLeapYear","isLeapMonth","dayOfYearHelper","dayOfYear","weekOfYear","isoWeekYearAndWeek","weekYear","validateStartMonth","startMonth","getQuarter","options","shifted","getMonth","getWeekday","touchesTime","unit","recomputeDayOfWeek","jsDow","startOf","value","result","asDateFieldView","endOf","v","out"]}
1
+ {"version":3,"sources":["../src/calendarUtils.ts","../src/isoWeek.ts"],"sourcesContent":["// Calendar utility helpers. These are pure functions\n// over the TemporalLike shape — no Temporal namespace needed, same\n// approach as isoWeek.ts and the field-reading helpers in format.ts.\n// Letting callers compute dayOfYear/weekOfYear/etc. without going\n// through format() means they can build their own derived values\n// without committing to a string format.\n//\n// Calendar-sensitivity: the helpers in this module assume the\n// iso8601 (Gregorian) calendar — that's what TemporalLike fields\n// carry for the overwhelming majority of callers. Non-Gregorian\n// calendars (hebrew, islamic, etc.) need their own helpers; this\n// module doesn't try to be calendar-polymorphic the way Temporal\n// itself is. Documented limitation, not a design choice — see\n// VERIFICATION.md for the rationale.\n\nimport { isGregorianLeapYear, dayOfYear, isoWeekYearAndWeek } from './isoWeek.js';\n\n// A subset of TemporalLike that has the date fields these helpers need,\n// plus optional time fields. PlainTime isn't a DateFieldView (no\n// year/month/day), but PlainDateTime / ZonedDateTime / PlainDate all\n// match. Time fields are optional so callers can pass a PlainDate\n// to startOf(value, 'month') without having to populate hour/minute/etc.\n// Exported so the comparison/arithmetic modules can use the same\n// narrowing.\nexport interface DateFieldView {\n year?: number;\n month?: number;\n day?: number;\n hour?: number;\n minute?: number;\n second?: number;\n millisecond?: number;\n dayOfWeek?: number;\n calendarId?: string;\n}\n\nfunction requireFields(view: DateFieldView, fields: Array<keyof DateFieldView>): void {\n for (const f of fields) {\n if (typeof view[f] !== 'number') {\n throw new Error(\n `temporal-fmt: calendar helper requires \"${String(f)}\", which this value doesn't have. ` +\n `Pass a Temporal.PlainDate / PlainDateTime / ZonedDateTime.`\n );\n }\n }\n}\n\nexport function daysInMonth(view: DateFieldView): number {\n requireFields(view, ['year', 'month']);\n const { year, month } = view;\n // Standard Gregorian month lengths. February's length depends on\n // whether `year` is a leap year — the same isGregorianLeapYear check\n // isoWeek.ts uses for dayOfYear arithmetic.\n const LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];\n if (month === 2 && isGregorianLeapYear(year!)) return 29;\n return LENGTHS[(month! - 1)!]!;\n}\n\nexport function daysInYear(view: DateFieldView): 365 | 366 {\n requireFields(view, ['year']);\n return isGregorianLeapYear(view.year!) ? 366 : 365;\n}\n\nexport function monthsInYear(_view: DateFieldView): 12 {\n // Gregorian always has 12 months. Other calendars (Hebrew leap years\n // have 13) need calendar-aware logic this module doesn't carry — see\n // the file-level comment. The `_view` parameter is kept so the\n // signature mirrors the other helpers and a future calendar-aware\n // implementation can use it without changing call sites.\n return 12;\n}\n\nexport function isLeapYear(view: DateFieldView): boolean {\n requireFields(view, ['year']);\n return isGregorianLeapYear(view.year!);\n}\n\n// `isLeapMonth` would require knowing which month of a leap-year-aware\n// calendar is the leap month — Gregorian doesn't have one, so this\n// returns false unconditionally. Kept here so the public surface\n// matches the module's listing; non-Gregorian calendars need\n// a different implementation.\nexport function isLeapMonth(_view: DateFieldView): boolean {\n return false;\n}\n\nexport function dayOfYearHelper(view: DateFieldView): number {\n requireFields(view, ['year', 'month', 'day']);\n return dayOfYear(view.year!, view.month!, view.day!);\n}\n\n// ISO 8601 week and week-year. Delegates to isoWeek.ts's\n// isoWeekYearAndWeek, which does the full Thursday-of-week\n// computation to handle the year-boundary cases (Dec 29-31 belonging\n// to week 1 of next year, Jan 1-3 belonging to week 52/53 of the\n// previous year).\nexport function weekOfYear(view: DateFieldView): number {\n requireFields(view, ['year', 'month', 'day', 'dayOfWeek']);\n return isoWeekYearAndWeek(view.year!, view.month!, view.day!, view.dayOfWeek!).week;\n}\n\nexport function weekYear(view: DateFieldView): number {\n requireFields(view, ['year', 'month', 'day', 'dayOfWeek']);\n return isoWeekYearAndWeek(view.year!, view.month!, view.day!, view.dayOfWeek!).isoYear;\n}\n\n/**\n * Fiscal-quarter options. `startMonth` is the calendar month (1-12) the\n * fiscal year begins on — e.g. `7` for a fiscal year starting in July.\n * Omitted or `1` gives the calendar-quarter behavior getQuarter() has\n * always had (Jan-Mar = Q1, etc.), so existing callers passing nothing\n * see no change.\n */\nexport interface QuarterOptions {\n startMonth?: number;\n}\n\nfunction validateStartMonth(startMonth: number): void {\n if (!Number.isInteger(startMonth) || startMonth < 1 || startMonth > 12) {\n throw new Error(\n `temporal-fmt: getQuarter's startMonth must be an integer from 1 to 12 (got ${startMonth}).`\n );\n }\n}\n\nexport function getQuarter(view: DateFieldView, options: QuarterOptions = {}): number {\n requireFields(view, ['month']);\n const startMonth = options.startMonth ?? 1;\n validateStartMonth(startMonth);\n if (startMonth === 1) {\n // Mirrors the Q token: months 1-3 → Q1, 4-6 → Q2, 7-9 → Q3, 10-12 → Q4.\n return Math.ceil(view.month! / 3);\n }\n // Fiscal case: shift the month so startMonth becomes month 1 of the\n // fiscal year (mod 12, 1-indexed), then apply the same ceil(/3) rule.\n // E.g. startMonth=7 (fiscal year starts July): July→1, Aug→2, ...,\n // Dec→6, Jan→7, ..., June→12. Then Q1 = fiscal months 1-3 (Jul-Sep),\n // matching the common \"FY starts in July\" convention where Q1 is the\n // first quarter of the fiscal year, not a quarter numbered by which\n // calendar quarter it falls in.\n const shifted = ((view.month! - startMonth + 12) % 12) + 1;\n return Math.ceil(shifted / 3);\n}\n\n// `getMonth` / `getWeekday` look trivial (just read the field) but\n// they're kept as real functions for API surface consistency with the\n// rest of this module. They also normalize: getWeekday returns 1-7\n// (Mon-Sun, matching Temporal's spec) regardless of what numbering the\n// caller's underlying value uses.\nexport function getMonth(view: DateFieldView): number {\n requireFields(view, ['month']);\n return view.month!;\n}\n\nexport function getWeekday(view: DateFieldView): number {\n requireFields(view, ['dayOfWeek']);\n return view.dayOfWeek!;\n}\n\n// startOf / endOf return new field bags (not Temporal objects — this\n// module is polyfill-free) with the relevant fields zeroed/extended.\n// Callers can pass the result to a Temporal constructor if they want\n// a typed value.\nexport type StartOfUnit = 'day' | 'month' | 'year' | 'hour' | 'minute' | 'second';\n\n// Returns true if the given unit (when used with startOf/endOf) should\n// also touch the time fields. 'day', 'month', 'year' all imply a\n// resolution coarser than an hour, so startOf zeroes the time fields\n// and endOf maxes them. Sub-hour units (hour/minute/second) only touch\n// the fields finer than themselves.\nfunction touchesTime(unit: StartOfUnit): boolean {\n return unit === 'day' || unit === 'month' || unit === 'year';\n}\n\n// startOf/endOf reassign year/month/day, which invalidates any\n// dayOfWeek carried over from the input — a plain { ...view } spread\n// leaves the old value sitting there unchanged. Same failure mode\n// businessCalendar.ts's isBusinessDay() works around for add(); we\n// recompute here rather than trust the copied field.\nfunction recomputeDayOfWeek(view: DateFieldView): void {\n if (typeof view.dayOfWeek !== 'number') return;\n /* c8 ignore start @preserve -- unreachable: startOf()/endOf() both call\n * asDateFieldView() before this, which already throws if year/month/day\n * aren't all numbers — so by the time a value with a numeric dayOfWeek\n * reaches here, year/month/day are guaranteed present too. */\n if (typeof view.year !== 'number' || typeof view.month !== 'number' || typeof view.day !== 'number') return;\n /* c8 ignore stop @preserve */\n const jsDow = new Date(Date.UTC(view.year, view.month - 1, view.day)).getUTCDay(); // 0=Sun..6=Sat\n view.dayOfWeek = jsDow === 0 ? 7 : jsDow; // 1=Mon..7=Sun\n}\n\nexport function startOf(value: unknown, unit: StartOfUnit): DateFieldView {\n const view = asDateFieldView(value);\n const result: DateFieldView = { ...view };\n if (unit === 'year') {\n result.month = 1;\n result.day = 1;\n } else if (unit === 'month') {\n result.day = 1;\n }\n recomputeDayOfWeek(result);\n if (touchesTime(unit)) {\n result.hour = 0;\n result.minute = 0;\n result.second = 0;\n result.millisecond = 0;\n } else if (unit === 'hour') {\n result.minute = 0;\n result.second = 0;\n result.millisecond = 0;\n } else if (unit === 'minute') {\n result.second = 0;\n result.millisecond = 0;\n } else if (unit === 'second') {\n result.millisecond = 0;\n }\n return result;\n}\n\nexport function endOf(value: unknown, unit: StartOfUnit): DateFieldView {\n const view = asDateFieldView(value);\n const result: DateFieldView = { ...view };\n if (unit === 'year') {\n result.month = 12;\n result.day = daysInMonth({ year: result.year!, month: 12 });\n } else if (unit === 'month') {\n result.day = daysInMonth({ year: result.year!, month: result.month! });\n }\n recomputeDayOfWeek(result);\n if (touchesTime(unit)) {\n result.hour = 23;\n result.minute = 59;\n result.second = 59;\n result.millisecond = 999;\n } else if (unit === 'hour') {\n result.minute = 59;\n result.second = 59;\n result.millisecond = 999;\n } else if (unit === 'minute') {\n result.second = 59;\n result.millisecond = 999;\n } else if (unit === 'second') {\n result.millisecond = 999;\n }\n return result;\n}\n\n// Type-narrowing helpers used by the comparison/arithmetic modules.\n// Lets them accept any of the four date-carrying Temporal types without\n// importing Temporal itself.\nexport function asDateFieldView(value: unknown): DateFieldView {\n if (typeof value !== 'object' || value === null) {\n throw new Error(`temporal-fmt: expected a date-carrying Temporal value, got ${String(value)}.`);\n }\n // Temporal instances expose year/month/day/etc. as prototype getters,\n // not own enumerable properties — so `{ ...value }` would lose them.\n // Read them explicitly. Only the fields actually present on this\n // value type end up in the returned view.\n const v = value as Record<string, unknown>;\n const out: DateFieldView = {};\n if (typeof v.year === 'number') out.year = v.year;\n if (typeof v.month === 'number') out.month = v.month;\n if (typeof v.day === 'number') out.day = v.day;\n if (typeof v.hour === 'number') out.hour = v.hour;\n if (typeof v.minute === 'number') out.minute = v.minute;\n if (typeof v.second === 'number') out.second = v.second;\n if (typeof v.millisecond === 'number') out.millisecond = v.millisecond;\n if (typeof v.dayOfWeek === 'number') out.dayOfWeek = v.dayOfWeek;\n if (typeof v.calendarId === 'string') out.calendarId = v.calendarId;\n if (out.year === undefined || out.month === undefined || out.day === undefined) {\n throw new Error(\n `temporal-fmt: value is missing year/month/day fields — pass a Temporal.PlainDate / PlainDateTime / ZonedDateTime.`\n );\n }\n return out;\n}\n\n// Re-export the TemporalType alias so callers can import everything\n// from one place.\nexport type { TemporalType } from './tokenMetadata.js';\n// Re-export TemporalLike for the same reason.\nexport type { TemporalLike } from './tokens.js';","// ISO week stuff. week runs Mon-Sun, and week 1 is whichever week has\n// the year's first Thursday in it (same as saying \"the week with Jan 4\").\n// took me a minute to wrap my head around this but the upshot is late-Dec\n// dates can land in week 1 of NEXT year, and early-Jan dates can land in\n// week 52/53 of the PREVIOUS year. that adjacent year is what RRRR prints,\n// not the plain calendar year.\n//\n// doing this with plain year/month/day + dayOfWeek math instead of asking\n// Temporal for it, since format() only has whatever fields got handed in\n// and dragging in a whole Temporal implementation just for week numbers\n// felt like overkill (also breaks for people not on setTemporal()).\n\nconst DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];\nconst CUMULATIVE_DAYS_BY_MONTH = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];\n\nexport function isGregorianLeapYear(year: number): boolean {\n // divisible by 4, unless it's a century, then it also needs /400.\n // Temporal's iso8601 calendar never switches to Julian, so this rule\n // just applies all the way back, even for BCE years.\n return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;\n}\n\nfunction daysInYear(year: number): 365 | 366 {\n return isGregorianLeapYear(year) ? 366 : 365;\n}\n\nexport function dayOfYear(year: number, month: number, day: number): number {\n let doy = CUMULATIVE_DAYS_BY_MONTH[month - 1]! + day;\n if (month > 2 && isGregorianLeapYear(year)) doy += 1;\n return doy;\n}\n\n// Jan 1 2000 was a Saturday (ISO dow 6). picked this as an anchor point\n// since it's easier than reaching for Zeller's congruence every time,\n// and obviously this fact about Jan 1 2000 isn't going to change on us\nconst REFERENCE_YEAR = 2000;\nconst REFERENCE_JAN1_DAY_OF_WEEK = 6;\n\nfunction dayOfWeekOfJan1(year: number): number {\n // walk year by year from 2000 instead of doing this from scratch every\n // time — most callers pass a current-era year so this loop is short anyway\n let offset = 0;\n if (year >= REFERENCE_YEAR) {\n for (let y = REFERENCE_YEAR; y < year; y++) offset += daysInYear(y);\n } else {\n for (let y = year; y < REFERENCE_YEAR; y++) offset -= daysInYear(y);\n }\n // now turn \"days since Jan 1 2000\" into an ISO weekday. Jan 1 2000 was a 6\n // (Sat), so: zero-indexed = (6-1 + offset) mod 7, then bump back to 1-indexed\n const zeroIndexed = (((6 - 1 + offset) % 7) + 7) % 7;\n return zeroIndexed + 1;\n}\n\nexport interface IsoWeekDate {\n isoYear: number;\n week: number; // goes 1 to 53\n}\n\nexport function isoWeekYearAndWeek(year: number, month: number, day: number, dayOfWeek: number): IsoWeekDate {\n // step 1: figure out the Thursday of this week — whatever calendar year\n // that Thursday's in IS the ISO week-numbering year. doing this with\n // day-of-year offsets so we don't need an actual Temporal.PlainDate here\n const doy = dayOfYear(year, month, day);\n const thursdayDoyRelative = doy + (4 - dayOfWeek); // can go negative or past daysInYear, that's fine\n\n let isoYear: number;\n let thursdayDoy: number;\n if (thursdayDoyRelative < 1) {\n isoYear = year - 1;\n thursdayDoy = thursdayDoyRelative + daysInYear(isoYear);\n } else if (thursdayDoyRelative > daysInYear(year)) {\n isoYear = year + 1;\n thursdayDoy = thursdayDoyRelative - daysInYear(year);\n } else {\n isoYear = year;\n thursdayDoy = thursdayDoyRelative;\n }\n\n // step 2: find the first Thursday of isoYear, that one's week 1 by definition.\n // depends entirely on which weekday Jan 1 lands on\n const jan1Dow = dayOfWeekOfJan1(isoYear);\n const firstThursdayDoy = 1 + ((4 - jan1Dow + 7) % 7); // always lands somewhere 1-7\n\n // step 3: just count how many full weeks between the two Thursdays\n const week = 1 + Math.floor((thursdayDoy - firstThursdayDoy) / 7);\n return { isoYear, week };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACaA,IAAM,2BAA2B,CAAC,GAAG,IAAI,IAAI,IAAI,KAAK,KAAK,KAAK,KAAK,KAAK,KAAK,KAAK,GAAG;AAEhF,SAAS,oBAAoB,MAAuB;AAIzD,SAAQ,OAAO,MAAM,KAAK,OAAO,QAAQ,KAAM,OAAO,QAAQ;AAChE;AAEA,SAAS,WAAW,MAAyB;AAC3C,SAAO,oBAAoB,IAAI,IAAI,MAAM;AAC3C;AAEO,SAAS,UAAU,MAAc,OAAe,KAAqB;AAC1E,MAAI,MAAM,yBAAyB,QAAQ,CAAC,IAAK;AACjD,MAAI,QAAQ,KAAK,oBAAoB,IAAI,EAAG,QAAO;AACnD,SAAO;AACT;AAKA,IAAM,iBAAiB;AAGvB,SAAS,gBAAgB,MAAsB;AAG7C,MAAI,SAAS;AACb,MAAI,QAAQ,gB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;AAC3D,MAAI,OAAO,EAAE,cAAc,SAAU,KAAI,YAAY,EAAE;AACvD,MAAI,OAAO,EAAE,eAAe,SAAU,KAAI,aAAa,EAAE;AACzD,MAAI,IAAI,SAAS,UAAa,IAAI,UAAU,UAAa,IAAI,QAAQ,QAAW;AAC9E,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;","names":["daysInYear","daysInYear"]}
@@ -1,2 +1,34 @@
1
- import{a,b,c,d,e,f,g,h,i,j,k,l,m,n}from"./chunk-EHLRZULM.js";import"./chunk-C6ZZ62ET.js";export{n as asDateFieldView,f as dayOfYearHelper,a as daysInMonth,b as daysInYear,m as endOf,j as getMonth,i as getQuarter,k as getWeekday,e as isLeapMonth,d as isLeapYear,c as monthsInYear,l as startOf,g as weekOfYear,h as weekYear};
1
+ import {
2
+ asDateFieldView,
3
+ dayOfYearHelper,
4
+ daysInMonth,
5
+ daysInYear,
6
+ endOf,
7
+ getMonth,
8
+ getQuarter,
9
+ getWeekday,
10
+ isLeapMonth,
11
+ isLeapYear,
12
+ monthsInYear,
13
+ startOf,
14
+ weekOfYear,
15
+ weekYear
16
+ } from "./chunk-HXY2GUUM.js";
17
+ import "./chunk-MKPDLQKD.js";
18
+ export {
19
+ asDateFieldView,
20
+ dayOfYearHelper,
21
+ daysInMonth,
22
+ daysInYear,
23
+ endOf,
24
+ getMonth,
25
+ getQuarter,
26
+ getWeekday,
27
+ isLeapMonth,
28
+ isLeapYear,
29
+ monthsInYear,
30
+ startOf,
31
+ weekOfYear,
32
+ weekYear
33
+ };
2
34
  //# sourceMappingURL=calendarUtils.js.map