@timestamp-js/calendar-saka 0.1.0-rc.1 → 0.1.0-rc.2
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 +17 -0
- package/dist/index.global.js +293 -0
- package/dist/index.global.min.js +1 -0
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -42,5 +42,22 @@ monthEnd.date // '1946-01-31'
|
|
|
42
42
|
monthDays.length // 31
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
## Browser Globals
|
|
46
|
+
|
|
47
|
+
For CDN or CodePen usage, load `@timestamp-js/core` before the adapter package:
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
<script src="https://cdn.jsdelivr.net/npm/@timestamp-js/core@0.1.0-rc.2/dist/index.global.min.js"></script>
|
|
51
|
+
<script src="https://cdn.jsdelivr.net/npm/@timestamp-js/calendar-saka@0.1.0-rc.2/dist/index.global.min.js"></script>
|
|
52
|
+
<script>
|
|
53
|
+
const visible = TimestampJsCore.parseCalendarTimestamp(
|
|
54
|
+
'1946-01-15',
|
|
55
|
+
TimestampJsCalendarSaka.indianNationalCalendar,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
console.log(visible?.calendarId)
|
|
59
|
+
</script>
|
|
60
|
+
```
|
|
61
|
+
|
|
45
62
|
This package is early calendar-adapter work. Treat the adapter contract as release-candidate API
|
|
46
63
|
until `@timestamp-js/core` reaches a stable `1.0.0`.
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
var TimestampJsCalendarSaka = (function(exports) {
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const MILLISECONDS_IN_DAY$1 = 864e5;
|
|
6
|
+
const GREGORIAN_DAYS_IN_MONTH = [
|
|
7
|
+
0,
|
|
8
|
+
31,
|
|
9
|
+
28,
|
|
10
|
+
31,
|
|
11
|
+
30,
|
|
12
|
+
31,
|
|
13
|
+
30,
|
|
14
|
+
31,
|
|
15
|
+
31,
|
|
16
|
+
30,
|
|
17
|
+
31,
|
|
18
|
+
30,
|
|
19
|
+
31
|
|
20
|
+
];
|
|
21
|
+
const GREGORIAN_DAYS_IN_MONTH_LEAP = [
|
|
22
|
+
0,
|
|
23
|
+
31,
|
|
24
|
+
29,
|
|
25
|
+
31,
|
|
26
|
+
30,
|
|
27
|
+
31,
|
|
28
|
+
30,
|
|
29
|
+
31,
|
|
30
|
+
31,
|
|
31
|
+
30,
|
|
32
|
+
31,
|
|
33
|
+
30,
|
|
34
|
+
31
|
|
35
|
+
];
|
|
36
|
+
function fromUtcDate(date) {
|
|
37
|
+
return {
|
|
38
|
+
year: date.getUTCFullYear(),
|
|
39
|
+
month: date.getUTCMonth() + 1,
|
|
40
|
+
day: date.getUTCDate()
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function toGregorianUtcDate(date) {
|
|
44
|
+
return new Date(Date.UTC(date.year, date.month - 1, date.day));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const gregorianCalendar = Object.freeze({
|
|
48
|
+
id: "gregorian",
|
|
49
|
+
intlCalendar: "gregory",
|
|
50
|
+
label: "Gregorian",
|
|
51
|
+
monthsInYear() {
|
|
52
|
+
return 12;
|
|
53
|
+
},
|
|
54
|
+
isLeapYear(year) {
|
|
55
|
+
return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
56
|
+
},
|
|
57
|
+
daysInMonth(year, month) {
|
|
58
|
+
return this.isLeapYear(year) ? GREGORIAN_DAYS_IN_MONTH_LEAP[month] : GREGORIAN_DAYS_IN_MONTH[month];
|
|
59
|
+
},
|
|
60
|
+
toEpochDay(date) {
|
|
61
|
+
return Math.floor(Date.UTC(date.year, date.month - 1, date.day) / MILLISECONDS_IN_DAY$1);
|
|
62
|
+
},
|
|
63
|
+
fromEpochDay(epochDay) {
|
|
64
|
+
return fromUtcDate(/* @__PURE__ */ new Date(epochDay * MILLISECONDS_IN_DAY$1));
|
|
65
|
+
},
|
|
66
|
+
addDays(date, amount) {
|
|
67
|
+
return this.fromEpochDay(this.toEpochDay(date) + amount);
|
|
68
|
+
},
|
|
69
|
+
nextDay(date) {
|
|
70
|
+
return this.addDays(date, 1);
|
|
71
|
+
},
|
|
72
|
+
prevDay(date) {
|
|
73
|
+
return this.addDays(date, -1);
|
|
74
|
+
},
|
|
75
|
+
getDayOfYear(date) {
|
|
76
|
+
const yearStart = this.toEpochDay({
|
|
77
|
+
year: date.year,
|
|
78
|
+
month: 1,
|
|
79
|
+
day: 1
|
|
80
|
+
});
|
|
81
|
+
return this.toEpochDay(date) - yearStart + 1;
|
|
82
|
+
},
|
|
83
|
+
getWeekday(date) {
|
|
84
|
+
return toGregorianUtcDate(date).getUTCDay();
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
const TIME_CONSTANTS = {
|
|
90
|
+
MILLISECONDS_IN: {
|
|
91
|
+
SECOND: 1e3,
|
|
92
|
+
MINUTE: 6e4,
|
|
93
|
+
HOUR: 36e5,
|
|
94
|
+
DAY: 864e5,
|
|
95
|
+
WEEK: 6048e5
|
|
96
|
+
},
|
|
97
|
+
SECONDS_IN: {
|
|
98
|
+
MINUTE: 60,
|
|
99
|
+
HOUR: 3600,
|
|
100
|
+
DAY: 86400,
|
|
101
|
+
WEEK: 604800
|
|
102
|
+
},
|
|
103
|
+
MINUTES_IN: {
|
|
104
|
+
MINUTE: 1,
|
|
105
|
+
HOUR: 60,
|
|
106
|
+
DAY: 1440,
|
|
107
|
+
WEEK: 10080
|
|
108
|
+
},
|
|
109
|
+
HOURS_IN: {
|
|
110
|
+
DAY: 24,
|
|
111
|
+
WEEK: 168
|
|
112
|
+
},
|
|
113
|
+
DAYS_IN: { WEEK: 7 }
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const DAYS_IN_WEEK = TIME_CONSTANTS.DAYS_IN.WEEK;
|
|
117
|
+
|
|
118
|
+
const MINUTES_IN_HOUR = TIME_CONSTANTS.MINUTES_IN.HOUR;
|
|
119
|
+
|
|
120
|
+
const HOURS_IN_DAY = TIME_CONSTANTS.HOURS_IN.DAY;
|
|
121
|
+
|
|
122
|
+
const MILLISECONDS_IN_MINUTE = TIME_CONSTANTS.MILLISECONDS_IN.MINUTE;
|
|
123
|
+
|
|
124
|
+
const MILLISECONDS_IN_SECOND = TIME_CONSTANTS.MILLISECONDS_IN.SECOND;
|
|
125
|
+
|
|
126
|
+
const MILLISECONDS_IN_HOUR = TIME_CONSTANTS.MILLISECONDS_IN.HOUR;
|
|
127
|
+
|
|
128
|
+
const MILLISECONDS_IN_DAY = TIME_CONSTANTS.MILLISECONDS_IN.DAY;
|
|
129
|
+
|
|
130
|
+
const MILLISECONDS_IN_WEEK = TIME_CONSTANTS.MILLISECONDS_IN.WEEK;
|
|
131
|
+
|
|
132
|
+
const SECONDS_IN_MINUTE = TIME_CONSTANTS.SECONDS_IN.MINUTE;
|
|
133
|
+
|
|
134
|
+
const SECONDS_IN_HOUR = TIME_CONSTANTS.SECONDS_IN.HOUR;
|
|
135
|
+
|
|
136
|
+
const SECONDS_IN_DAY = TIME_CONSTANTS.SECONDS_IN.DAY;
|
|
137
|
+
|
|
138
|
+
const Timestamp = freezeTimestamp({
|
|
139
|
+
date: "",
|
|
140
|
+
hasDay: false,
|
|
141
|
+
year: 0,
|
|
142
|
+
month: 0,
|
|
143
|
+
day: 0,
|
|
144
|
+
hasTime: false,
|
|
145
|
+
hour: 0,
|
|
146
|
+
minute: 0,
|
|
147
|
+
weekday: 0,
|
|
148
|
+
doy: 0,
|
|
149
|
+
workweek: 0,
|
|
150
|
+
past: false,
|
|
151
|
+
current: false,
|
|
152
|
+
future: false,
|
|
153
|
+
disabled: false
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const TimeObject = Object.freeze({
|
|
157
|
+
hour: 0,
|
|
158
|
+
minute: 0
|
|
159
|
+
});
|
|
160
|
+
function freezeTimestamp(timestamp) {
|
|
161
|
+
return Object.freeze({ ...timestamp });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const GREGORIAN_YEAR_OFFSET = 78;
|
|
165
|
+
const MONTHS_IN_YEAR = 12;
|
|
166
|
+
const SAKA_DAYS_IN_MONTH_COMMON = [
|
|
167
|
+
0,
|
|
168
|
+
30,
|
|
169
|
+
31,
|
|
170
|
+
31,
|
|
171
|
+
31,
|
|
172
|
+
31,
|
|
173
|
+
31,
|
|
174
|
+
30,
|
|
175
|
+
30,
|
|
176
|
+
30,
|
|
177
|
+
30,
|
|
178
|
+
30,
|
|
179
|
+
30
|
|
180
|
+
];
|
|
181
|
+
const SAKA_DAYS_IN_MONTH_LEAP = [
|
|
182
|
+
0,
|
|
183
|
+
31,
|
|
184
|
+
31,
|
|
185
|
+
31,
|
|
186
|
+
31,
|
|
187
|
+
31,
|
|
188
|
+
31,
|
|
189
|
+
30,
|
|
190
|
+
30,
|
|
191
|
+
30,
|
|
192
|
+
30,
|
|
193
|
+
30,
|
|
194
|
+
30
|
|
195
|
+
];
|
|
196
|
+
function assertPositiveYear(year) {
|
|
197
|
+
if (year < 1) throw new RangeError("Saka calendar years start at 1.");
|
|
198
|
+
}
|
|
199
|
+
function getGregorianYearForSakaYear(year) {
|
|
200
|
+
assertPositiveYear(year);
|
|
201
|
+
return year + GREGORIAN_YEAR_OFFSET;
|
|
202
|
+
}
|
|
203
|
+
function getSakaYearStartEpochDay(year) {
|
|
204
|
+
const gregorianYear = getGregorianYearForSakaYear(year);
|
|
205
|
+
return gregorianCalendar.toEpochDay({
|
|
206
|
+
year: gregorianYear,
|
|
207
|
+
month: 3,
|
|
208
|
+
day: gregorianCalendar.isLeapYear(gregorianYear) === true ? 21 : 22
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
function daysBeforeMonth(year, month) {
|
|
212
|
+
let days = 0;
|
|
213
|
+
for (let currentMonth = 1; currentMonth < month; currentMonth += 1) days += sakaDaysInMonth(year, currentMonth);
|
|
214
|
+
return days;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function isSakaLeapYear(year) {
|
|
218
|
+
return gregorianCalendar.isLeapYear(getGregorianYearForSakaYear(year));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function sakaDaysInMonth(year, month) {
|
|
222
|
+
assertPositiveYear(year);
|
|
223
|
+
if (month < 1 || month > MONTHS_IN_YEAR) return 0;
|
|
224
|
+
return isSakaLeapYear(year) === true ? SAKA_DAYS_IN_MONTH_LEAP[month] : SAKA_DAYS_IN_MONTH_COMMON[month];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const indianNationalCalendar = Object.freeze({
|
|
228
|
+
id: "saka",
|
|
229
|
+
intlCalendar: "indian",
|
|
230
|
+
label: "Indian National (Saka)",
|
|
231
|
+
monthsInYear() {
|
|
232
|
+
return MONTHS_IN_YEAR;
|
|
233
|
+
},
|
|
234
|
+
isLeapYear(year) {
|
|
235
|
+
return isSakaLeapYear(year);
|
|
236
|
+
},
|
|
237
|
+
daysInMonth(year, month) {
|
|
238
|
+
return sakaDaysInMonth(year, month);
|
|
239
|
+
},
|
|
240
|
+
toEpochDay(date) {
|
|
241
|
+
assertPositiveYear(date.year);
|
|
242
|
+
return getSakaYearStartEpochDay(date.year) + daysBeforeMonth(date.year, date.month) + date.day - 1;
|
|
243
|
+
},
|
|
244
|
+
fromEpochDay(epochDay) {
|
|
245
|
+
let year = gregorianCalendar.fromEpochDay(epochDay).year - GREGORIAN_YEAR_OFFSET;
|
|
246
|
+
let yearStart = getSakaYearStartEpochDay(year);
|
|
247
|
+
if (epochDay < yearStart) {
|
|
248
|
+
year -= 1;
|
|
249
|
+
yearStart = getSakaYearStartEpochDay(year);
|
|
250
|
+
}
|
|
251
|
+
let dayOfYear = epochDay - yearStart;
|
|
252
|
+
let month = 1;
|
|
253
|
+
while (month < MONTHS_IN_YEAR) {
|
|
254
|
+
const daysInCurrentMonth = this.daysInMonth(year, month);
|
|
255
|
+
if (dayOfYear < daysInCurrentMonth) break;
|
|
256
|
+
dayOfYear -= daysInCurrentMonth;
|
|
257
|
+
month += 1;
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
year,
|
|
261
|
+
month,
|
|
262
|
+
day: dayOfYear + 1
|
|
263
|
+
};
|
|
264
|
+
},
|
|
265
|
+
addDays(date, amount) {
|
|
266
|
+
return this.fromEpochDay(this.toEpochDay(date) + amount);
|
|
267
|
+
},
|
|
268
|
+
nextDay(date) {
|
|
269
|
+
return this.addDays(date, 1);
|
|
270
|
+
},
|
|
271
|
+
prevDay(date) {
|
|
272
|
+
return this.addDays(date, -1);
|
|
273
|
+
},
|
|
274
|
+
getDayOfYear(date) {
|
|
275
|
+
return this.toEpochDay(date) - this.toEpochDay({
|
|
276
|
+
year: date.year,
|
|
277
|
+
month: 1,
|
|
278
|
+
day: 1
|
|
279
|
+
}) + 1;
|
|
280
|
+
},
|
|
281
|
+
getWeekday(date) {
|
|
282
|
+
return gregorianCalendar.getWeekday(gregorianCalendar.fromEpochDay(this.toEpochDay(date)));
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
const sakaCalendar = indianNationalCalendar;
|
|
287
|
+
|
|
288
|
+
exports.indianNationalCalendar = indianNationalCalendar;
|
|
289
|
+
exports.isSakaLeapYear = isSakaLeapYear;
|
|
290
|
+
exports.sakaCalendar = sakaCalendar;
|
|
291
|
+
exports.sakaDaysInMonth = sakaDaysInMonth;
|
|
292
|
+
return exports;
|
|
293
|
+
})({});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var TimestampJsCalendarSaka=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let t=864e5,n=[0,31,28,31,30,31,30,31,31,30,31,30,31],r=[0,31,29,31,30,31,30,31,31,30,31,30,31];function i(e){return{year:e.getUTCFullYear(),month:e.getUTCMonth()+1,day:e.getUTCDate()}}function a(e){return new Date(Date.UTC(e.year,e.month-1,e.day))}let o=Object.freeze({id:`gregorian`,intlCalendar:`gregory`,label:`Gregorian`,monthsInYear(){return 12},isLeapYear(e){return e%4==0&&e%100!=0||e%400==0},daysInMonth(e,t){return this.isLeapYear(e)?r[t]:n[t]},toEpochDay(e){return Math.floor(Date.UTC(e.year,e.month-1,e.day)/t)},fromEpochDay(e){return i(new Date(e*t))},addDays(e,t){return this.fromEpochDay(this.toEpochDay(e)+t)},nextDay(e){return this.addDays(e,1)},prevDay(e){return this.addDays(e,-1)},getDayOfYear(e){let t=this.toEpochDay({year:e.year,month:1,day:1});return this.toEpochDay(e)-t+1},getWeekday(e){return a(e).getUTCDay()}}),s={MILLISECONDS_IN:{SECOND:1e3,MINUTE:6e4,HOUR:36e5,DAY:864e5,WEEK:6048e5},SECONDS_IN:{MINUTE:60,HOUR:3600,DAY:86400,WEEK:604800},MINUTES_IN:{MINUTE:1,HOUR:60,DAY:1440,WEEK:10080},HOURS_IN:{DAY:24,WEEK:168},DAYS_IN:{WEEK:7}};s.DAYS_IN.WEEK,s.MINUTES_IN.HOUR,s.HOURS_IN.DAY,s.MILLISECONDS_IN.MINUTE,s.MILLISECONDS_IN.SECOND,s.MILLISECONDS_IN.HOUR,s.MILLISECONDS_IN.DAY,s.MILLISECONDS_IN.WEEK,s.SECONDS_IN.MINUTE,s.SECONDS_IN.HOUR,s.SECONDS_IN.DAY,c({date:``,hasDay:!1,year:0,month:0,day:0,hasTime:!1,hour:0,minute:0,weekday:0,doy:0,workweek:0,past:!1,current:!1,future:!1,disabled:!1}),Object.freeze({hour:0,minute:0});function c(e){return Object.freeze({...e})}let l=[0,30,31,31,31,31,31,30,30,30,30,30,30],u=[0,31,31,31,31,31,31,30,30,30,30,30,30];function d(e){if(e<1)throw RangeError(`Saka calendar years start at 1.`)}function f(e){return d(e),e+78}function p(e){let t=f(e);return o.toEpochDay({year:t,month:3,day:o.isLeapYear(t)===!0?21:22})}function m(e,t){let n=0;for(let r=1;r<t;r+=1)n+=g(e,r);return n}function h(e){return o.isLeapYear(f(e))}function g(e,t){return d(e),t<1||t>12?0:h(e)===!0?u[t]:l[t]}let _=Object.freeze({id:`saka`,intlCalendar:`indian`,label:`Indian National (Saka)`,monthsInYear(){return 12},isLeapYear(e){return h(e)},daysInMonth(e,t){return g(e,t)},toEpochDay(e){return d(e.year),p(e.year)+m(e.year,e.month)+e.day-1},fromEpochDay(e){let t=o.fromEpochDay(e).year-78,n=p(t);e<n&&(--t,n=p(t));let r=e-n,i=1;for(;i<12;){let e=this.daysInMonth(t,i);if(r<e)break;r-=e,i+=1}return{year:t,month:i,day:r+1}},addDays(e,t){return this.fromEpochDay(this.toEpochDay(e)+t)},nextDay(e){return this.addDays(e,1)},prevDay(e){return this.addDays(e,-1)},getDayOfYear(e){return this.toEpochDay(e)-this.toEpochDay({year:e.year,month:1,day:1})+1},getWeekday(e){return o.getWeekday(o.fromEpochDay(this.toEpochDay(e)))}}),v=_;return e.indianNationalCalendar=_,e.isSakaLeapYear=h,e.sakaCalendar=v,e.sakaDaysInMonth=g,e})({});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@timestamp-js/calendar-saka",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.2",
|
|
4
4
|
"description": "Indian National Calendar (Saka) adapter for Timestamp.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"calendar",
|
|
@@ -29,6 +29,8 @@
|
|
|
29
29
|
"sideEffects": false,
|
|
30
30
|
"main": "./dist/index.js",
|
|
31
31
|
"types": "./dist/index.d.ts",
|
|
32
|
+
"unpkg": "./dist/index.global.min.js",
|
|
33
|
+
"jsdelivr": "./dist/index.global.min.js",
|
|
32
34
|
"exports": {
|
|
33
35
|
".": {
|
|
34
36
|
"types": "./dist/index.d.ts",
|
|
@@ -39,7 +41,7 @@
|
|
|
39
41
|
"access": "public"
|
|
40
42
|
},
|
|
41
43
|
"dependencies": {
|
|
42
|
-
"@timestamp-js/core": "0.1.0-rc.
|
|
44
|
+
"@timestamp-js/core": "0.1.0-rc.2"
|
|
43
45
|
},
|
|
44
46
|
"devDependencies": {
|
|
45
47
|
"@types/node": "26.0.1",
|
|
@@ -55,7 +57,7 @@
|
|
|
55
57
|
"scripts": {
|
|
56
58
|
"clean": "rm -rf dist node_modules",
|
|
57
59
|
"prebuild": "pnpm --filter @timestamp-js/core build",
|
|
58
|
-
"build": "tsc -p tsconfig.build.json",
|
|
60
|
+
"build": "node ../../scripts/build-browser-globals.mjs @timestamp-js/calendar-saka && tsc -p tsconfig.build.json",
|
|
59
61
|
"format": "oxfmt",
|
|
60
62
|
"format:check": "oxfmt --check",
|
|
61
63
|
"lint": "oxlint",
|