panchang-ts 0.1.0
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/LICENSE +21 -0
- package/README.md +324 -0
- package/dist/index.cjs +3465 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +295 -0
- package/dist/index.d.ts +295 -0
- package/dist/index.js +3452 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ishank
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
# panchang-ts
|
|
2
|
+
|
|
3
|
+
Pure TypeScript Hindu Panchang (almanac) calculations. Zero native dependencies.
|
|
4
|
+
Works offline in React Native (Hermes), Node.js, and browsers.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- **10 Panchang elements:** Tithi, Nakshatra, Yoga, Karana, Vara, Sunrise/Sunset,
|
|
9
|
+
Rahu Kalam, Gulika Kalam, Yamaganda, Abhijit Muhurta
|
|
10
|
+
- **Daily mode:** Full sunrise-to-sunrise day with all element transitions
|
|
11
|
+
(e.g. two Tithis if a transition happens mid-day)
|
|
12
|
+
- **Instant mode:** Element active at an exact moment (birth charts, muhurta selection)
|
|
13
|
+
- **3 ayanamsa systems:** Lahiri (default), B.V. Raman, KP (Krishnamurti)
|
|
14
|
+
- **3 languages:** English, Sanskrit (Devanagari), Hindi
|
|
15
|
+
- **React Native compatible:** Pure JS math, no native modules, tested on Hermes
|
|
16
|
+
- **Fast:** ~0.1 ms names-only on Node.js; <100 ms on budget Android (Hermes)
|
|
17
|
+
- **Typed:** Full TypeScript types for every result and option
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install panchang-ts
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { getDailyPanchang } from 'panchang-ts';
|
|
29
|
+
|
|
30
|
+
const result = getDailyPanchang(
|
|
31
|
+
new Date(2025, 0, 14), // January 14, 2025
|
|
32
|
+
{ latitude: 18.5204, longitude: 73.8567 }, // Pune, India
|
|
33
|
+
{ timezone: 330 }, // IST = UTC+5:30 = 330 minutes
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
console.log(result.tithis[0].name); // "Krishna Chaturdashi"
|
|
37
|
+
console.log(result.nakshatras[0].name); // "Mrigashira"
|
|
38
|
+
console.log(result.vara.name); // "Mangalavara"
|
|
39
|
+
console.log(result.rahuKalam); // { start: Date, end: Date }
|
|
40
|
+
console.log(result.sunrise); // Date (read via getUTC*)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Reading Output Times
|
|
44
|
+
|
|
45
|
+
All `Date` objects in the result are **offset-adjusted** to the requested timezone.
|
|
46
|
+
**Always read time components via `getUTC*` methods:**
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
const sunrise = result.sunrise;
|
|
50
|
+
const h = sunrise.getUTCHours(); // 7
|
|
51
|
+
const m = sunrise.getUTCMinutes(); // 4
|
|
52
|
+
// → Sunrise at 07:04 local time
|
|
53
|
+
|
|
54
|
+
// Format helper:
|
|
55
|
+
function fmt(d: Date) {
|
|
56
|
+
const h = d.getUTCHours(), m = d.getUTCMinutes();
|
|
57
|
+
return `${h}:${String(m).padStart(2, '0')}`;
|
|
58
|
+
}
|
|
59
|
+
fmt(result.rahuKalam.start); // "09:04"
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Do **not** use `.getHours()` — it uses your system timezone, which may differ.
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## API Reference
|
|
67
|
+
|
|
68
|
+
### `getDailyPanchang(date, location, options)`
|
|
69
|
+
|
|
70
|
+
Returns the full Hindu day from sunrise to next sunrise, with all element transitions.
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { getDailyPanchang } from 'panchang-ts';
|
|
74
|
+
|
|
75
|
+
const result = getDailyPanchang(
|
|
76
|
+
date, // Date — any moment within the local calendar day
|
|
77
|
+
location, // GeoLocation — { latitude, longitude, elevation? }
|
|
78
|
+
options, // PanchangOptions — { timezone, ayanamsa?, language?, ... }
|
|
79
|
+
);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
**Returns: `DailyPanchangResult`**
|
|
83
|
+
|
|
84
|
+
| Field | Type | Description |
|
|
85
|
+
|-------|------|-------------|
|
|
86
|
+
| `date` | `Date` | Input date |
|
|
87
|
+
| `location` | `GeoLocation` | Input location |
|
|
88
|
+
| `timezone` | `number` | Resolved UTC offset in minutes |
|
|
89
|
+
| `sunrise` | `Date` | Sunrise (offset-adjusted) |
|
|
90
|
+
| `sunset` | `Date` | Sunset (offset-adjusted) |
|
|
91
|
+
| `nextSunrise` | `Date` | Following day's sunrise (offset-adjusted) |
|
|
92
|
+
| `dayDurationMinutes` | `number` | Length of daytime in minutes |
|
|
93
|
+
| `nightDurationMinutes` | `number` | Length of night in minutes |
|
|
94
|
+
| `tithis` | `DailyTithiInfo[]` | Tithis active during the day (usually 1–2) |
|
|
95
|
+
| `nakshatras` | `DailyNakshatraInfo[]` | Nakshatras active during the day |
|
|
96
|
+
| `yogas` | `DailyYogaInfo[]` | Yogas active during the day |
|
|
97
|
+
| `karanas` | `DailyKaranaInfo[]` | Karanas active during the day (usually 2–4) |
|
|
98
|
+
| `vara` | `VaraInfo` | Weekday (Vara) |
|
|
99
|
+
| `rahuKalam` | `TimePeriod` | Rahu Kalam start/end |
|
|
100
|
+
| `gulikaKalam` | `TimePeriod` | Gulika Kalam start/end |
|
|
101
|
+
| `yamaganda` | `TimePeriod` | Yamaganda start/end |
|
|
102
|
+
| `abhijitMuhurta` | `TimePeriod` | Abhijit Muhurta start/end |
|
|
103
|
+
| `ayanamsa` | `number` | Ayanamsa in degrees at sunrise |
|
|
104
|
+
| `siderealSunAtSunrise` | `number` | Sun sidereal longitude at sunrise (°) |
|
|
105
|
+
| `siderealMoonAtSunrise` | `number` | Moon sidereal longitude at sunrise (°) |
|
|
106
|
+
| `masa` | `MasaInfo` | Solar month (Saura Masa) |
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
### `getInstantPanchang(date, location, options?)`
|
|
111
|
+
|
|
112
|
+
Returns the single Panchang element active at an exact UTC moment.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { getInstantPanchang } from 'panchang-ts';
|
|
116
|
+
|
|
117
|
+
const result = getInstantPanchang(
|
|
118
|
+
new Date('2025-01-14T03:00:00Z'), // UTC moment
|
|
119
|
+
{ latitude: 18.5204, longitude: 73.8567 },
|
|
120
|
+
{ language: 'sa' }, // Sanskrit names
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
console.log(result.tithi.name); // "कृष्ण चतुर्दशी"
|
|
124
|
+
console.log(result.nakshatra.name); // "मृगशिरा"
|
|
125
|
+
console.log(result.tithi.endTime); // Date when this Tithi ends
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**Returns: `InstantPanchangResult`**
|
|
129
|
+
|
|
130
|
+
| Field | Type | Description |
|
|
131
|
+
|-------|------|-------------|
|
|
132
|
+
| `timestamp` | `Date` | Input UTC moment |
|
|
133
|
+
| `location` | `GeoLocation` | Input location |
|
|
134
|
+
| `tithi` | `TithiInfo` | Active Tithi with paksha, number, completion % |
|
|
135
|
+
| `nakshatra` | `NakshatraInfo` | Active Nakshatra with pada, degrees |
|
|
136
|
+
| `yoga` | `YogaInfo` | Active Yoga |
|
|
137
|
+
| `karana` | `KaranaInfo` | Active Karana (movable or fixed) |
|
|
138
|
+
| `vara` | `VaraInfo` | Active Vara (weekday) |
|
|
139
|
+
| `ayanamsa` | `number` | Ayanamsa in degrees |
|
|
140
|
+
| `siderealSun` | `number` | Sun sidereal longitude (°) |
|
|
141
|
+
| `siderealMoon` | `number` | Moon sidereal longitude (°) |
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
### Options
|
|
146
|
+
|
|
147
|
+
**`PanchangOptions`** (required for `getDailyPanchang`):
|
|
148
|
+
|
|
149
|
+
| Option | Type | Default | Description |
|
|
150
|
+
|--------|------|---------|-------------|
|
|
151
|
+
| `timezone` | `number \| string` | **required** | UTC offset in minutes (330 for IST). Use a number on Hermes — IANA strings require `Intl`. |
|
|
152
|
+
| `ayanamsa` | `'lahiri' \| 'raman' \| 'krishnamurti'` | `'lahiri'` | Ayanamsa system |
|
|
153
|
+
| `language` | `'en' \| 'sa' \| 'hi'` | `'en'` | Language for element names |
|
|
154
|
+
| `computeEndTimes` | `boolean` | `true` | Set `false` for ~5× faster, names-only output |
|
|
155
|
+
| `precision` | `'standard' \| 'high'` | `'standard'` | Binary-search iterations (15 vs 25). High precision is rarely needed. |
|
|
156
|
+
|
|
157
|
+
**`InstantPanchangOptions`** (optional for `getInstantPanchang`): same as above but without `timezone`.
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
### Low-level Utilities
|
|
162
|
+
|
|
163
|
+
These are exported for advanced use cases (building your own ayanamsa tools,
|
|
164
|
+
visualisations, or debugging).
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
import {
|
|
168
|
+
getSunrise, getSunset,
|
|
169
|
+
getSiderealSunLongitude, getSiderealMoonLongitude,
|
|
170
|
+
getAyanamsa,
|
|
171
|
+
computeRahuKalam, computeGulikaKalam, computeYamaganda,
|
|
172
|
+
computeAbhijitMuhurta,
|
|
173
|
+
} from 'panchang-ts';
|
|
174
|
+
|
|
175
|
+
// Sunrise/sunset
|
|
176
|
+
const sunrise = getSunrise(localMidnightUtc, { latitude: 28.6, longitude: 77.2 });
|
|
177
|
+
const sunset = getSunset(sunrise, { latitude: 28.6, longitude: 77.2 });
|
|
178
|
+
|
|
179
|
+
// Sidereal longitudes
|
|
180
|
+
const moonLon = getSiderealMoonLongitude(new Date(), 'lahiri'); // degrees [0, 360)
|
|
181
|
+
const sunLon = getSiderealSunLongitude(new Date(), 'lahiri');
|
|
182
|
+
|
|
183
|
+
// Ayanamsa
|
|
184
|
+
const ayan = getAyanamsa(new Date(), 'lahiri'); // e.g. 24.10
|
|
185
|
+
|
|
186
|
+
// Inauspicious periods (varaIndex: 0=Sun … 6=Sat)
|
|
187
|
+
const rahu = computeRahuKalam(sunrise, sunset, varaIndex);
|
|
188
|
+
// { start: Date, end: Date }
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
### Types
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
interface GeoLocation {
|
|
197
|
+
latitude: number; // -90 to 90
|
|
198
|
+
longitude: number; // -180 to 180
|
|
199
|
+
elevation?: number; // metres, default 0
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
interface TimePeriod {
|
|
203
|
+
start: Date;
|
|
204
|
+
end: Date;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
interface TithiInfo {
|
|
208
|
+
index: number; // 0–29
|
|
209
|
+
name: string; // e.g. "Shukla Pratipada"
|
|
210
|
+
paksha: 'Shukla' | 'Krishna';
|
|
211
|
+
number: number; // 1–15 within the paksha
|
|
212
|
+
completionPercentage: number;
|
|
213
|
+
endTime: Date | null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
interface NakshatraInfo {
|
|
217
|
+
index: number; // 0–26
|
|
218
|
+
name: string;
|
|
219
|
+
pada: number; // 1–4
|
|
220
|
+
degreesInNakshatra: number;
|
|
221
|
+
completionPercentage: number;
|
|
222
|
+
endTime: Date | null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
interface DailyTithiInfo extends TithiInfo {
|
|
226
|
+
startTime: Date | null; // null if active at sunrise
|
|
227
|
+
isActiveAtSunrise: boolean;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// DailyNakshatraInfo, DailyYogaInfo, DailyKaranaInfo follow the same pattern
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
## React Native / Hermes Usage
|
|
236
|
+
|
|
237
|
+
Works with Expo and bare React Native (Hermes engine). Pass `timezone` as a **number**
|
|
238
|
+
— IANA timezone strings (`'Asia/Kolkata'`) require `Intl`, which older Hermes versions
|
|
239
|
+
don't fully support.
|
|
240
|
+
|
|
241
|
+
**Two-pass rendering** for smooth UI:
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
import { getDailyPanchang } from 'panchang-ts';
|
|
245
|
+
import { InteractionManager } from 'react-native';
|
|
246
|
+
|
|
247
|
+
// Pass 1 — instant, names only (~0.1 ms on Node, <100 ms on Hermes)
|
|
248
|
+
const fast = getDailyPanchang(date, location, {
|
|
249
|
+
timezone: 330,
|
|
250
|
+
computeEndTimes: false,
|
|
251
|
+
});
|
|
252
|
+
setState(fast); // show names immediately
|
|
253
|
+
|
|
254
|
+
// Pass 2 — background, full with end-times (~0.5 ms on Node, <500 ms on Hermes)
|
|
255
|
+
InteractionManager.runAfterInteractions(() => {
|
|
256
|
+
const full = getDailyPanchang(date, location, { timezone: 330 });
|
|
257
|
+
setState(full); // update with transition times
|
|
258
|
+
});
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
## Performance
|
|
264
|
+
|
|
265
|
+
| Mode | Node.js | Hermes (budget Android) |
|
|
266
|
+
|------|---------|------------------------|
|
|
267
|
+
| Names-only (`computeEndTimes: false`) | ~0.1 ms | <100 ms |
|
|
268
|
+
| Full with end-times | ~0.5 ms | <500 ms |
|
|
269
|
+
|
|
270
|
+
Measured with Vitest benchmarks on Node 22 and on a physical budget Android device
|
|
271
|
+
via the dharmSetu React Native app.
|
|
272
|
+
|
|
273
|
+
---
|
|
274
|
+
|
|
275
|
+
## Accuracy
|
|
276
|
+
|
|
277
|
+
Validated against [DrikPanchang.com](https://www.drikpanchang.com) for 15+ date/city combinations.
|
|
278
|
+
|
|
279
|
+
| Element | Accuracy |
|
|
280
|
+
|---------|----------|
|
|
281
|
+
| Sunrise / Sunset | ±2 minutes |
|
|
282
|
+
| Tithi, Nakshatra, Yoga, Karana names | Exact match |
|
|
283
|
+
| Element end-times | ±5 minutes |
|
|
284
|
+
| Ayanamsa | ±0.005° vs Swiss Ephemeris |
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
## Compatibility
|
|
289
|
+
|
|
290
|
+
| Environment | Support |
|
|
291
|
+
|-------------|---------|
|
|
292
|
+
| Node.js 18+ | ✅ |
|
|
293
|
+
| Node.js 20+ | ✅ |
|
|
294
|
+
| Node.js 22+ | ✅ |
|
|
295
|
+
| React Native (Hermes) | ✅ (pass `timezone` as number) |
|
|
296
|
+
| Expo (managed + bare) | ✅ |
|
|
297
|
+
| Browser (modern) | ✅ (ESM build) |
|
|
298
|
+
| Browser (legacy / IE) | ✗ |
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## Error Handling
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
import { PanchangError } from 'panchang-ts';
|
|
306
|
+
|
|
307
|
+
try {
|
|
308
|
+
getDailyPanchang(date, location, options);
|
|
309
|
+
} catch (e) {
|
|
310
|
+
if (e instanceof PanchangError) {
|
|
311
|
+
console.error(e.code); // e.g. 'INVALID_LATITUDE', 'NO_SUNRISE'
|
|
312
|
+
console.error(e.message);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
`PanchangErrorCode` values: `INVALID_DATE`, `INVALID_LATITUDE`, `INVALID_LONGITUDE`,
|
|
318
|
+
`INVALID_TIMEZONE`, `INVALID_AYANAMSA`, `NO_SUNRISE`, `NO_SUNSET`.
|
|
319
|
+
|
|
320
|
+
---
|
|
321
|
+
|
|
322
|
+
## License
|
|
323
|
+
|
|
324
|
+
MIT
|