@kalcodes/ethiopian-calendar 1.0.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/README.md ADDED
@@ -0,0 +1 @@
1
+ ## Simple Calander Converter
@@ -0,0 +1,14 @@
1
+ import { dateStr, type SimpleDate, isValidEthDate, isValidGregDate, isEthLeapYear, isGregLeapYear } from "./utils.js";
2
+ declare const ETH_JDN_OFFSET = 1724221;
3
+ declare function ethToJDN(date: SimpleDate): number;
4
+ declare function jdnToEth(jdn: number): SimpleDate;
5
+ declare function gregToJDN(date: SimpleDate): number;
6
+ declare function jdnToGreg(jdn: number): {
7
+ year: number;
8
+ month: number;
9
+ day: number;
10
+ };
11
+ declare function gregToEth(date: SimpleDate): SimpleDate;
12
+ declare function ethToGreg(date: SimpleDate): SimpleDate;
13
+ export { type SimpleDate, ETH_JDN_OFFSET, dateStr, ethToJDN, jdnToEth, ethToGreg, gregToJDN, jdnToGreg, gregToEth, isValidEthDate, isValidGregDate, isEthLeapYear, isGregLeapYear, };
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,KAAK,UAAU,EACf,cAAc,EACd,eAAe,EACf,aAAa,EACb,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,QAAA,MAAM,cAAc,UAAU,CAAC;AAE/B,iBAAS,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAU1C;AAED,iBAAS,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAmBzC;AAED,iBAAS,SAAS,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAmB3C;AAED,iBAAS,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb,CAeA;AAED,iBAAS,SAAS,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAK/C;AAED,iBAAS,SAAS,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAK/C;AAED,OAAO,EACL,KAAK,UAAU,EACf,cAAc,EACd,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,SAAS,EACT,SAAS,EACT,SAAS,EACT,cAAc,EACd,eAAe,EACf,aAAa,EACb,cAAc,GACf,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,63 @@
1
+ import { dateStr, isValidEthDate, isValidGregDate, isEthLeapYear, isGregLeapYear, } from "./utils.js";
2
+ const ETH_JDN_OFFSET = 1724221;
3
+ function ethToJDN(date) {
4
+ const { year, month, day } = date;
5
+ if (!isValidEthDate(date))
6
+ throw new Error(`The date ${dateStr(date)} is invalid!`);
7
+ const leapDays = Math.floor((year - 1) / 4);
8
+ const daysInYear = 30 * (month - 1) + (day - 1);
9
+ return ETH_JDN_OFFSET + 365 * (year - 1) + leapDays + daysInYear;
10
+ }
11
+ function jdnToEth(jdn) {
12
+ const ethDays = jdn - ETH_JDN_OFFSET;
13
+ const cycles = Math.floor(ethDays / 1461);
14
+ const daysRemaining = ethDays % 1461;
15
+ let yearsInCycle = Math.floor(daysRemaining / 365);
16
+ let daysInYear = daysRemaining % 365;
17
+ if (daysRemaining === 1460) {
18
+ yearsInCycle = 3;
19
+ daysInYear = 365;
20
+ }
21
+ const year = cycles * 4 + yearsInCycle + 1;
22
+ const month = Math.floor(daysInYear / 30) + 1;
23
+ const day = (daysInYear % 30) + 1;
24
+ return { year, month, day };
25
+ }
26
+ function gregToJDN(date) {
27
+ const { year, month, day } = date;
28
+ if (!isValidGregDate(date))
29
+ throw new Error(`The date ${dateStr(date)} is invalid!`);
30
+ const a = Math.floor((14 - month) / 12);
31
+ const y = year + 4800 - a;
32
+ const m = month + 12 * a - 3;
33
+ return (day +
34
+ Math.floor((153 * m + 2) / 5) +
35
+ 365 * y +
36
+ Math.floor(y / 4) -
37
+ Math.floor(y / 100) +
38
+ Math.floor(y / 400) -
39
+ 32045);
40
+ }
41
+ function jdnToGreg(jdn) {
42
+ const a = jdn + 32044;
43
+ const b = Math.floor((4 * a + 3) / 146097);
44
+ const c = a - Math.floor((146097 * b) / 4);
45
+ const d = Math.floor((4 * c + 3) / 1461);
46
+ const e = c - Math.floor((1461 * d) / 4);
47
+ const m = Math.floor((5 * e + 2) / 153);
48
+ const day = e - Math.floor((153 * m + 2) / 5) + 1;
49
+ const month = m + 3 - 12 * Math.floor(m / 10);
50
+ const year = 100 * b + d - 4800 + Math.floor(m / 10);
51
+ return { year, month, day };
52
+ }
53
+ function gregToEth(date) {
54
+ if (!isValidGregDate(date))
55
+ throw new Error(`The date ${dateStr(date)} is invalid!`);
56
+ return jdnToEth(gregToJDN(date));
57
+ }
58
+ function ethToGreg(date) {
59
+ if (!isValidEthDate(date))
60
+ throw new Error(`The date ${dateStr(date)} is invalid!`);
61
+ return jdnToGreg(ethToJDN(date));
62
+ }
63
+ export { ETH_JDN_OFFSET, dateStr, ethToJDN, jdnToEth, ethToGreg, gregToJDN, jdnToGreg, gregToEth, isValidEthDate, isValidGregDate, isEthLeapYear, isGregLeapYear, };
@@ -0,0 +1,11 @@
1
+ export type SimpleDate = {
2
+ year: number;
3
+ month: number;
4
+ day: number;
5
+ };
6
+ export declare function isEthLeapYear(year: number): boolean;
7
+ export declare function isGregLeapYear(year: number): boolean;
8
+ export declare function isValidEthDate(date: SimpleDate): boolean;
9
+ export declare function isValidGregDate(date: SimpleDate): boolean;
10
+ export declare function dateStr(date: SimpleDate, separator?: string): string;
11
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtE,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,WAEzC;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,WAE1C;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAYxD;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAQzD;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,SAAS,GAAE,MAAY,UAGhE"}
package/dist/utils.js ADDED
@@ -0,0 +1,31 @@
1
+ export function isEthLeapYear(year) {
2
+ return year % 4 === 0;
3
+ }
4
+ export function isGregLeapYear(year) {
5
+ return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
6
+ }
7
+ export function isValidEthDate(date) {
8
+ const { year, month, day } = date;
9
+ if (day < 1 || day > 30)
10
+ return false;
11
+ if (month < 1 || month > 13)
12
+ return false;
13
+ if (month === 13) {
14
+ if (isEthLeapYear(year))
15
+ return day <= 6;
16
+ return day <= 5;
17
+ }
18
+ return true;
19
+ }
20
+ export function isValidGregDate(date) {
21
+ const { year, month, day } = date;
22
+ if (month < 1 || month > 12)
23
+ return false;
24
+ const feb = isGregLeapYear(year) ? 29 : 28;
25
+ const daysInMonth = [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
26
+ return day <= daysInMonth[month - 1];
27
+ }
28
+ export function dateStr(date, separator = "-") {
29
+ const { year, month, day } = date;
30
+ return `${year}${separator}${month}${separator}${day}`;
31
+ }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@kalcodes/ethiopian-calendar",
3
+ "version": "1.0.0",
4
+ "description": "Simple ethiopian ⟵⟶ gregorian calendar converter.",
5
+ "main": "dist/index.js",
6
+ "type": "module",
7
+ "author": "",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/kalcodes/ethiopian-calendar.git"
11
+ },
12
+ "files": [
13
+ "dist",
14
+ "package.json",
15
+ "README.md"
16
+ ],
17
+ "keywords": [
18
+ "Ethiopian",
19
+ "Calendar",
20
+ "Simple",
21
+ "converter",
22
+ "kalcodes"
23
+ ],
24
+ "license": "ISC",
25
+ "devEngines": {
26
+ "packageManager": {
27
+ "name": "pnpm",
28
+ "version": "^11.9.0",
29
+ "onFail": "download"
30
+ }
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "^26.2.0",
34
+ "typescript": "^7.0.2"
35
+ },
36
+ "scripts": {
37
+ "build": "tsc --build",
38
+ "test": "pnpm run build && node --test",
39
+ "types:check": "tsc --noEmit"
40
+ }
41
+ }