@@ -44,99 +44,95 @@ const ABS_2SEP1752 = 639785;
44
44
* Formerly in namespace, now top-level
45
45
*/
46
46
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
+ }
54
54
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
+ }
64
64
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
+ }
72
72
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
+ }
91
91
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
+ /*
106
102
if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
107
103
throw new RangeError(`Invalid Date: ${date}`);
108
104
}
109
105
*/
110
- return abs ;
111
- }
106
+ return abs ;
107
+ }
112
108
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
+ /*
127
123
if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
128
124
throw new RangeError(`Invalid Date: ${abs}`);
129
125
}
130
126
*/
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 ) ;
142
136
}
137
+ return dt ;
138
+ }
0 commit comments