@nextera.one/tps-standard 0.5.1 → 0.5.3

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 (39) hide show
  1. package/README.md +91 -467
  2. package/dist/drivers/gregorian.d.ts +18 -0
  3. package/dist/drivers/gregorian.js +157 -0
  4. package/dist/drivers/gregorian.js.map +1 -0
  5. package/dist/drivers/hijri.d.ts +42 -0
  6. package/dist/drivers/hijri.js +281 -0
  7. package/dist/drivers/hijri.js.map +1 -0
  8. package/dist/drivers/holocene.d.ts +25 -0
  9. package/dist/drivers/holocene.js +132 -0
  10. package/dist/drivers/holocene.js.map +1 -0
  11. package/dist/drivers/julian.d.ts +33 -0
  12. package/dist/drivers/julian.js +225 -0
  13. package/dist/drivers/julian.js.map +1 -0
  14. package/dist/drivers/persian.d.ts +33 -0
  15. package/dist/drivers/persian.js +269 -0
  16. package/dist/drivers/persian.js.map +1 -0
  17. package/dist/drivers/tps.d.ts +55 -0
  18. package/dist/drivers/tps.js +235 -0
  19. package/dist/drivers/tps.js.map +1 -0
  20. package/dist/drivers/unix.d.ts +16 -0
  21. package/dist/drivers/unix.js +76 -0
  22. package/dist/drivers/unix.js.map +1 -0
  23. package/dist/index.d.ts +174 -41
  24. package/dist/index.js +803 -321
  25. package/dist/index.js.map +1 -0
  26. package/package.json +9 -3
  27. package/src/drivers/gregorian.ts +191 -0
  28. package/src/drivers/hijri.ts +322 -0
  29. package/src/drivers/holocene.ts +152 -0
  30. package/src/drivers/julian.ts +255 -0
  31. package/src/drivers/persian.ts +298 -0
  32. package/src/drivers/tps.ts +270 -0
  33. package/src/drivers/unix.ts +79 -0
  34. package/src/index.ts +959 -366
  35. package/dist/src/index.js +0 -681
  36. package/dist/test/src/index.js +0 -963
  37. package/dist/test/test/persian-calendar.test.js +0 -488
  38. package/dist/test/test/tps-uid.test.js +0 -295
  39. package/dist/test/tps-uid.test.js +0 -240
@@ -0,0 +1,270 @@
1
+ /**
2
+ * TPS calendar driver for canonical TPS time strings.
3
+ *
4
+ * TPS Calendar characteristics:
5
+ * - Epoch: August 11, 1999 (00:00 UTC)
6
+ * - Months: Always 28 days (12 months per year = 336 days)
7
+ * - Time offset: 7 hours ahead of Gregorian (00:00 Gregorian = 07:00 TPS)
8
+ *
9
+ * Conversion process:
10
+ * 1. Apply 7-hour offset to Gregorian date
11
+ * 2. Calculate day-of-year in offset date
12
+ * 3. Convert day-of-year to TPS month/day (each month = 28 days)
13
+ * 4. Preserve millennium/century/year structure
14
+ */
15
+ import { CalendarDriver, CalendarMetadata, TPSComponents, TPS } from "../index";
16
+ import { GregorianDriver } from "./gregorian";
17
+
18
+ export class TpsDriver implements CalendarDriver {
19
+ readonly code = "tps";
20
+ readonly name = "TPS Canonical";
21
+
22
+ // TPS Epoch: August 11, 1999, 00:00 UTC
23
+ private readonly TPS_EPOCH = new Date(Date.UTC(1999, 7, 11, 0, 0, 0, 0));
24
+ // TPS is 7 hours ahead of Gregorian
25
+ private readonly TPS_OFFSET_HOURS = 7;
26
+ // Each TPS month has 28 days
27
+ private readonly TPS_DAYS_PER_MONTH = 28;
28
+ // TPS has 12 months per year (12 * 28 = 336 days)
29
+ private readonly TPS_MONTHS_PER_YEAR = 12;
30
+
31
+ private readonly gregorian = new GregorianDriver();
32
+
33
+ /**
34
+ * Converts a Gregorian Date to TPS components.
35
+ * Applies 7-hour offset and converts day-of-year to TPS month/day (28-day months).
36
+ */
37
+ getComponentsFromDate(date: Date): Partial<TPSComponents> {
38
+ // Apply 7-hour TPS offset to the Gregorian date
39
+ const offsetMillis = this.TPS_OFFSET_HOURS * 60 * 60 * 1000;
40
+ const offsetDate = new Date(date.getTime() + offsetMillis);
41
+
42
+ // Get Gregorian components for the offset date
43
+ const gregComponents = this.gregorian.getComponentsFromDate(offsetDate);
44
+
45
+ // Calculate day-of-year (0-indexed) for the offset date
46
+ const yearStart = new Date(Date.UTC(offsetDate.getUTCFullYear(), 0, 1));
47
+ const dayOfYear = Math.floor(
48
+ (offsetDate.getTime() - yearStart.getTime()) / (24 * 60 * 60 * 1000),
49
+ );
50
+
51
+ // Convert day-of-year to TPS month/day (each month = 28 days)
52
+ const tpsMonth = Math.floor(dayOfYear / this.TPS_DAYS_PER_MONTH) + 1;
53
+ const tpsDay = (dayOfYear % this.TPS_DAYS_PER_MONTH) + 1;
54
+
55
+ return {
56
+ calendar: this.code,
57
+ millennium: gregComponents.millennium,
58
+ century: gregComponents.century,
59
+ year: gregComponents.year,
60
+ month: tpsMonth,
61
+ day: tpsDay,
62
+ hour: gregComponents.hour,
63
+ minute: gregComponents.minute,
64
+ second: gregComponents.second,
65
+ millisecond: gregComponents.millisecond,
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Converts TPS components to a Gregorian Date.
71
+ * Converts TPS month/day (28-day months) to day-of-year, then removes 7-hour offset.
72
+ */
73
+ getDateFromComponents(components: Partial<TPSComponents>): Date {
74
+ // Convert TPS month/day (28-day months) to day-of-year (0-indexed)
75
+ const tpsMonth = components.month ?? 1;
76
+ const tpsDay = components.day ?? 1;
77
+ const dayOfYear = (tpsMonth - 1) * this.TPS_DAYS_PER_MONTH + (tpsDay - 1);
78
+
79
+ // Reconstruct full Gregorian year from millennium/century/year
80
+ const m = components.millennium ?? 0;
81
+ const c = components.century ?? 1;
82
+ const y = components.year ?? 0;
83
+ const fullYear = (m - 1) * 1000 + (c - 1) * 100 + y;
84
+
85
+ // Create date at start of year and add day-of-year offset
86
+ const dateInYear = new Date(Date.UTC(fullYear, 0, 1));
87
+ dateInYear.setUTCDate(dateInYear.getUTCDate() + dayOfYear);
88
+
89
+ // Set time components
90
+ dateInYear.setUTCHours(components.hour ?? 0);
91
+ dateInYear.setUTCMinutes(components.minute ?? 0);
92
+ dateInYear.setUTCSeconds(components.second ?? 0);
93
+ dateInYear.setUTCMilliseconds(components.millisecond ?? 0);
94
+
95
+ // Remove 7-hour TPS offset to get back to Gregorian
96
+ const offsetMillis = this.TPS_OFFSET_HOURS * 60 * 60 * 1000;
97
+ return new Date(dateInYear.getTime() - offsetMillis);
98
+ }
99
+
100
+ getFromDate(date: Date): string {
101
+ const components = this.getComponentsFromDate(date) as TPSComponents;
102
+ return TPS.buildTimePart(components);
103
+ }
104
+
105
+ /**
106
+ * Parse a TPS date string: "YYYY-MM-DD" where MM is 01-12, DD is 01-28.
107
+ * Optional time: "YYYY-MM-DD HH:MM:SS.mmm"
108
+ */
109
+ parseDate(input: string, format?: string): Partial<TPSComponents> {
110
+ const s = input.trim();
111
+ const m = s.match(
112
+ /^(\d{4})-(\d{2})-(\d{2})(?:[ T](\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?)?$/,
113
+ );
114
+ if (!m) {
115
+ throw new Error(`TpsDriver.parseDate: unsupported format "${input}"`);
116
+ }
117
+
118
+ const year = parseInt(m[1], 10);
119
+ const month = parseInt(m[2], 10);
120
+ const day = parseInt(m[3], 10);
121
+
122
+ // Validate TPS month/day constraints
123
+ if (month < 1 || month > this.TPS_MONTHS_PER_YEAR) {
124
+ throw new Error(
125
+ `TpsDriver.parseDate: invalid TPS month ${month} (expected 1-12)`,
126
+ );
127
+ }
128
+ if (day < 1 || day > this.TPS_DAYS_PER_MONTH) {
129
+ throw new Error(
130
+ `TpsDriver.parseDate: invalid TPS day ${day} (expected 1-${this.TPS_DAYS_PER_MONTH})`,
131
+ );
132
+ }
133
+
134
+ const hour = m[4] !== undefined ? parseInt(m[4], 10) : undefined;
135
+ const minute = m[5] !== undefined ? parseInt(m[5], 10) : undefined;
136
+ const second = m[6] !== undefined ? parseInt(m[6], 10) : undefined;
137
+ const millisecond =
138
+ m[7] !== undefined ? parseInt((m[7] + "000").slice(0, 3), 10) : undefined;
139
+
140
+ const comp: Partial<TPSComponents> = {
141
+ calendar: this.code,
142
+ year,
143
+ month,
144
+ day,
145
+ };
146
+ if (hour !== undefined) comp.hour = hour;
147
+ if (minute !== undefined) comp.minute = minute;
148
+ if (second !== undefined) comp.second = second;
149
+ if (millisecond !== undefined) comp.millisecond = millisecond;
150
+ return comp;
151
+ }
152
+
153
+ /**
154
+ * Format TPS components to "YYYY-MM-DD" where MM is 01-12, DD is 01-28.
155
+ * With time: "YYYY-MM-DD THH:MM:SS.mmm"
156
+ */
157
+ format(components: Partial<TPSComponents>, format?: string): string {
158
+ const y =
159
+ components.year !== undefined
160
+ ? String(components.year).padStart(4, "0")
161
+ : "0000";
162
+ const mo =
163
+ components.month !== undefined
164
+ ? String(components.month).padStart(2, "0")
165
+ : "01";
166
+ const d =
167
+ components.day !== undefined
168
+ ? String(components.day).padStart(2, "0")
169
+ : "01";
170
+ let out = `${y}-${mo}-${d}`;
171
+
172
+ if (
173
+ components.hour !== undefined ||
174
+ components.minute !== undefined ||
175
+ components.second !== undefined ||
176
+ components.millisecond !== undefined
177
+ ) {
178
+ const h =
179
+ components.hour !== undefined
180
+ ? String(components.hour).padStart(2, "0")
181
+ : "00";
182
+ const mi =
183
+ components.minute !== undefined
184
+ ? String(components.minute).padStart(2, "0")
185
+ : "00";
186
+ const s =
187
+ components.second !== undefined
188
+ ? String(Math.floor(components.second)).padStart(2, "0")
189
+ : "00";
190
+ const ms =
191
+ components.millisecond !== undefined
192
+ ? String(components.millisecond).padStart(3, "0")
193
+ : "000";
194
+ out += `T${h}:${mi}:${s}.${ms}`;
195
+ }
196
+ return out;
197
+ }
198
+
199
+ /**
200
+ * Validate TPS date string or components.
201
+ * TPS has months 1-12, each with 28 days.
202
+ */
203
+ validate(input: string | Partial<TPSComponents>): boolean {
204
+ if (typeof input === "string") {
205
+ const valid =
206
+ /^\d{4}-\d{2}-\d{2}(?:[ T]\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?)?$/.test(
207
+ input.trim(),
208
+ );
209
+ if (!valid) return false;
210
+
211
+ // Parse and validate constraints
212
+ try {
213
+ this.parseDate(input);
214
+ return true;
215
+ } catch {
216
+ return false;
217
+ }
218
+ }
219
+
220
+ if (typeof input === "object") {
221
+ return (
222
+ input.year !== undefined &&
223
+ input.month !== undefined &&
224
+ input.day !== undefined &&
225
+ input.year >= 0 &&
226
+ input.month >= 1 &&
227
+ input.month <= this.TPS_MONTHS_PER_YEAR &&
228
+ input.day >= 1 &&
229
+ input.day <= this.TPS_DAYS_PER_MONTH
230
+ );
231
+ }
232
+ return false;
233
+ }
234
+
235
+ /**
236
+ * Get TPS calendar metadata.
237
+ * TPS has 12 months, each with 28 days.
238
+ */
239
+ getMetadata(): CalendarMetadata {
240
+ return {
241
+ name: "TPS Canonical (28-day months)",
242
+ monthNames: [
243
+ "Month 1",
244
+ "Month 2",
245
+ "Month 3",
246
+ "Month 4",
247
+ "Month 5",
248
+ "Month 6",
249
+ "Month 7",
250
+ "Month 8",
251
+ "Month 9",
252
+ "Month 10",
253
+ "Month 11",
254
+ "Month 12",
255
+ ],
256
+ dayNames: [
257
+ "Sunday",
258
+ "Monday",
259
+ "Tuesday",
260
+ "Wednesday",
261
+ "Thursday",
262
+ "Friday",
263
+ "Saturday",
264
+ ],
265
+ monthsPerYear: this.TPS_MONTHS_PER_YEAR,
266
+ epochYear: 1999,
267
+ isLunar: false,
268
+ };
269
+ }
270
+ }
@@ -0,0 +1,79 @@
1
+ import { CalendarDriver, TPSComponents, TPS, CalendarMetadata } from "../index";
2
+
3
+ /**
4
+ * Unix calendar driver. Represents the epoch timestamp in seconds with
5
+ * fractional milliseconds. This mirrors the built-in `CalendarCode.UNIX`
6
+ * behaviour that was previously hard-coded in TPS.
7
+ */
8
+ export class UnixDriver implements CalendarDriver {
9
+ readonly code: string = "unix";
10
+
11
+ getComponentsFromDate(date: Date): Partial<TPSComponents> {
12
+ const s = (date.getTime() / 1000).toFixed(3);
13
+ return { calendar: this.code, unixSeconds: parseFloat(s) };
14
+ }
15
+
16
+ getDateFromComponents(components: Partial<TPSComponents>): Date {
17
+ // prefer an explicit unixSeconds value when available
18
+ if (components.unixSeconds !== undefined) {
19
+ return new Date(components.unixSeconds * 1000);
20
+ }
21
+
22
+ // otherwise attempt to derive a date from the other temporal fields by
23
+ // round-tripping through the core TPS logic. This is a little heavier but
24
+ // keeps the Unix driver useful when callers supply a full set of
25
+ // millennium/century/... values instead of a raw epoch.
26
+ try {
27
+ // The toURI helper will fill in the required time tokens and defaults.
28
+ const tpsString = TPS.buildTimePart(components as TPSComponents);
29
+ const date = TPS.toDate(tpsString);
30
+ if (!date) {
31
+ throw new Error("unable to convert components to Date");
32
+ }
33
+ return date;
34
+ } catch (err: any) {
35
+ throw new Error(
36
+ "UnixDriver.toGregorian: missing unixSeconds and unable to compute date",
37
+ );
38
+ }
39
+ }
40
+
41
+ getFromDate(date: Date): string {
42
+ const comp = this.getComponentsFromDate(date) as TPSComponents;
43
+ return TPS.buildTimePart(comp);
44
+ }
45
+
46
+ parseDate(input: string, format?: string): Partial<TPSComponents> {
47
+ const s = input.trim();
48
+ // Accept simple numeric timestamps with optional fractional part
49
+ if (!/^[0-9]+(?:\.[0-9]+)?$/.test(s)) {
50
+ throw new Error(`UnixDriver.parseDate: unsupported format "${input}"`);
51
+ }
52
+ return { calendar: this.code, unixSeconds: parseFloat(s) };
53
+ }
54
+
55
+ format(components: Partial<TPSComponents>, format?: string): string {
56
+ if (components.unixSeconds === undefined) {
57
+ throw new Error("UnixDriver.format: missing unixSeconds");
58
+ }
59
+ return new Date(components.unixSeconds * 1000).toISOString();
60
+ }
61
+
62
+ validate(input: string | Partial<TPSComponents>): boolean {
63
+ if (typeof input === "string") {
64
+ return /^[0-9]+(?:\.[0-9]+)?$/.test(input.trim());
65
+ }
66
+ if (typeof input === "object") {
67
+ return typeof input.unixSeconds === "number" && !isNaN(input.unixSeconds);
68
+ }
69
+ return false;
70
+ }
71
+
72
+ getMetadata(): CalendarMetadata {
73
+ return {
74
+ name: "Unix Epoch",
75
+ // there is no concept of months; include minimal info
76
+ monthsPerYear: 0,
77
+ };
78
+ }
79
+ }