Skip to content

Commit 8d3631b

Browse files
committed
Add more documentation and examples for HDate
1 parent 88a8477 commit 8d3631b

15 files changed

+375
-185
lines changed

.editorconfig

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
insert_final_newline = true

.eslintignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build/
2+
test/
3+
docs/
4+
dist/
5+
src/*.po.ts

.eslintrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./node_modules/gts/"
3+
}

.prettierrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
...require('gts/.prettierrc.json')
3+
}

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ module.exports = {
33
preset: 'ts-jest',
44
verbose: true,
55
testEnvironment: 'node',
6-
};
6+
};

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hebcal/hdate",
3-
"version": "0.11.0",
3+
"version": "0.11.1",
44
"description": "converts between Hebrew and Gregorian dates using Rata Die (R.D.) algorithm by Dershowitz and Reingold",
55
"author": "Michael J. Radwin (https://github.com/mjradwin)",
66
"contributors": [

src/anniversary.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
longCheshvan,
99
SimpleHebrewDate,
1010
} from './hdate-base';
11-
import { abs2greg, greg2abs, isDate } from './greg';
11+
import {abs2greg, greg2abs, isDate} from './greg';
1212

1313
const NISAN = months.NISAN;
1414
const CHESHVAN = months.CHESHVAN;

src/dateFormat.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const dateFormatRegex = /^(\d+).(\d+).(\d+),?\s+(\d+).(\d+).(\d+)/;
2525
/**
2626
* Returns a string similar to `Date.toISOString()` but in the
2727
* timezone `tzid`. Contrary to the typical meaning of `Z` at the end
28-
* of the string, this is not actually a UTC date.
28+
* of the string, this is not actually a UTC date.
2929
*/
3030
export function getPseudoISO(tzid: string, date: Date): string {
3131
const str = getFormatter(tzid).format(date);

src/greg.ts

+78-82
Original file line numberDiff line numberDiff line change
@@ -44,99 +44,95 @@ const ABS_2SEP1752 = 639785;
4444
* Formerly in namespace, now top-level
4545
*/
4646

47-
/**
48-
* Returns true if the Gregorian year is a leap year
49-
* @param year Gregorian year
50-
*/
51-
export function isGregLeapYear(year: number): boolean {
52-
return !(year % 4) && (!!(year % 100) || !(year % 400));
53-
}
47+
/**
48+
* Returns true if the Gregorian year is a leap year
49+
* @param year Gregorian year
50+
*/
51+
export function isGregLeapYear(year: number): boolean {
52+
return !(year % 4) && (!!(year % 100) || !(year % 400));
53+
}
5454

55-
/**
56-
* Number of days in the Gregorian month for given year
57-
* @param month Gregorian month (1=January, 12=December)
58-
* @param year Gregorian year
59-
*/
60-
export function daysInGregMonth(month: number, year: number): number {
61-
// 1 based months
62-
return monthLengths[+isGregLeapYear(year)][month];
63-
}
55+
/**
56+
* Number of days in the Gregorian month for given year
57+
* @param month Gregorian month (1=January, 12=December)
58+
* @param year Gregorian year
59+
*/
60+
export function daysInGregMonth(month: number, year: number): number {
61+
// 1 based months
62+
return monthLengths[+isGregLeapYear(year)][month];
63+
}
6464

65-
/**
66-
* Returns true if the object is a Javascript Date
67-
*/
68-
export function isDate(obj: any): boolean {
69-
// eslint-disable-next-line no-prototype-builtins
70-
return typeof obj === 'object' && Date.prototype.isPrototypeOf(obj);
71-
}
65+
/**
66+
* Returns true if the object is a Javascript Date
67+
*/
68+
export function isDate(obj: any): boolean {
69+
// eslint-disable-next-line no-prototype-builtins
70+
return typeof obj === 'object' && Date.prototype.isPrototypeOf(obj);
71+
}
7272

73-
/**
74-
* @private
75-
* @param year
76-
* @param month (1-12)
77-
* @param day (1-31)
78-
*/
79-
function toFixed(year: number, month: number, day: number): number {
80-
const py: number = year - 1;
81-
return (
82-
365 * py +
83-
quotient(py, 4) -
84-
quotient(py, 100) +
85-
quotient(py, 400) +
86-
quotient(367 * month - 362, 12) +
87-
(month <= 2 ? 0 : isGregLeapYear(year) ? -1 : -2) +
88-
day
89-
);
90-
}
73+
/**
74+
* @private
75+
* @param year
76+
* @param month (1-12)
77+
* @param day (1-31)
78+
*/
79+
function toFixed(year: number, month: number, day: number): number {
80+
const py: number = year - 1;
81+
return (
82+
365 * py +
83+
quotient(py, 4) -
84+
quotient(py, 100) +
85+
quotient(py, 400) +
86+
quotient(367 * month - 362, 12) +
87+
(month <= 2 ? 0 : isGregLeapYear(year) ? -1 : -2) +
88+
day
89+
);
90+
}
9191

92-
/**
93-
* Converts Gregorian date to absolute R.D. (Rata Die) days
94-
* @param date Gregorian date
95-
*/
96-
export function greg2abs(date: Date): number {
97-
if (!isDate(date)) {
98-
throw new TypeError(`Argument not a Date: ${date}`);
99-
}
100-
const abs = toFixed(
101-
date.getFullYear(),
102-
date.getMonth() + 1,
103-
date.getDate()
104-
);
105-
/*
92+
/**
93+
* Converts Gregorian date to absolute R.D. (Rata Die) days
94+
* @param date Gregorian date
95+
*/
96+
export function greg2abs(date: Date): number {
97+
if (!isDate(date)) {
98+
throw new TypeError(`Argument not a Date: ${date}`);
99+
}
100+
const abs = toFixed(date.getFullYear(), date.getMonth() + 1, date.getDate());
101+
/*
106102
if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
107103
throw new RangeError(`Invalid Date: ${date}`);
108104
}
109105
*/
110-
return abs;
111-
}
106+
return abs;
107+
}
112108

113-
/**
114-
* Converts from Rata Die (R.D. number) to Gregorian date.
115-
* See the footnote on page 384 of ``Calendrical Calculations, Part II:
116-
* Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
117-
* Clamen, Software--Practice and Experience, Volume 23, Number 4
118-
* (April, 1993), pages 383-404 for an explanation.
119-
* @param abs - R.D. number of days
120-
*/
121-
export function abs2greg(abs: number): Date {
122-
if (typeof abs !== 'number') {
123-
throw new TypeError(`Argument not a Number: ${abs}`);
124-
}
125-
abs = Math.trunc(abs);
126-
/*
109+
/**
110+
* Converts from Rata Die (R.D. number) to Gregorian date.
111+
* See the footnote on page 384 of ``Calendrical Calculations, Part II:
112+
* Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
113+
* Clamen, Software--Practice and Experience, Volume 23, Number 4
114+
* (April, 1993), pages 383-404 for an explanation.
115+
* @param abs - R.D. number of days
116+
*/
117+
export function abs2greg(abs: number): Date {
118+
if (typeof abs !== 'number') {
119+
throw new TypeError(`Argument not a Number: ${abs}`);
120+
}
121+
abs = Math.trunc(abs);
122+
/*
127123
if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
128124
throw new RangeError(`Invalid Date: ${abs}`);
129125
}
130126
*/
131-
const year: number = yearFromFixed(abs);
132-
const priorDays: number = abs - toFixed(year, 1, 1);
133-
const correction: number =
134-
abs < toFixed(year, 3, 1) ? 0 : isGregLeapYear(year) ? 1 : 2;
135-
const month: number = quotient(12 * (priorDays + correction) + 373, 367);
136-
const day: number = abs - toFixed(year, month, 1) + 1;
137-
const dt: Date = new Date(year, month - 1, day);
138-
if (year < 100 && year >= 0) {
139-
dt.setFullYear(year);
140-
}
141-
return dt;
127+
const year: number = yearFromFixed(abs);
128+
const priorDays: number = abs - toFixed(year, 1, 1);
129+
const correction: number =
130+
abs < toFixed(year, 3, 1) ? 0 : isGregLeapYear(year) ? 1 : 2;
131+
const month: number = quotient(12 * (priorDays + correction) + 373, 367);
132+
const day: number = abs - toFixed(year, month, 1) + 1;
133+
const dt: Date = new Date(year, month - 1, day);
134+
if (year < 100 && year >= 0) {
135+
dt.setFullYear(year);
142136
}
137+
return dt;
138+
}

src/gregNamespace.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { abs2greg, daysInGregMonth, greg2abs, isDate, isGregLeapYear } from './greg';
1+
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-namespace */
2+
import {
3+
abs2greg,
4+
daysInGregMonth,
5+
greg2abs,
6+
isDate,
7+
isGregLeapYear,
8+
} from './greg';
29

310
/**
411
* Gregorian date helper functions
@@ -9,7 +16,7 @@ export namespace greg {
916
export declare function greg2abs(date: Date): number;
1017
export declare function isDate(obj: any): boolean;
1118
export declare function isLeapYear(year: number): boolean;
12-
};
19+
}
1320

1421
greg.abs2greg = abs2greg;
1522
greg.daysInMonth = daysInGregMonth;

0 commit comments

Comments
 (0)