Skip to content

Commit dc0f69e

Browse files
committed
Update to tz database 2018d. Start improving documentation.
1 parent 421680f commit dc0f69e

6 files changed

Lines changed: 147 additions & 66 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/dist-server
66
/tmp
77
/out-tsc
8+
/docs
89

910
# dependencies
1011
/node_modules

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"scripts": {
88
"build": "tsc",
99
"test": "karma start",
10-
"watch:build": "tsc --watch"
10+
"watch:build": "tsc --watch",
11+
"document": "typedoc --name \"ks-date-time-zone\" --module commonjs --exclude \"**/*+(e2e|spec|index).ts\" --excludePrivate --excludeProtected --readme README.md --target ES5 --out docs src"
1112
},
1213
"keywords": [
1314
"calendar",

src/ks-calendar.ts

Lines changed: 109 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,45 @@ export const FRIDAY = 5;
3434
export const SATURDAY = 6;
3535

3636
/**
37-
* @description Constant for indicating the last occurrence of a particular day of the week (e.g. the last Tuesday) of a given month.
37+
* Constant for indicating the last occurrence of a particular day of the week (e.g. the last Tuesday) of a given month.
3838
*/
3939
export const LAST = 6;
4040

41+
/** @hidden */
4142
const DISTANT_YEAR_PAST = -9999999;
43+
/** @hidden */
4244
const DISTANT_YEAR_FUTURE = 9999999;
4345
const FIRST_GREGORIAN_DAY_SGC = -141427; // 1582-10-15
4446

47+
/**
48+
* Specifies a calendar date by year, month, and day. Optionally provides day number and boolean flag indicating Julian
49+
* or Gregorian.
50+
*/
4551
export interface YMDDate {
52+
/** Year as signed integer (0 = 1 BCE, -1 = 2 BCE, etc.). */
4653
y: number;
54+
/** Month as 1-12. */
4755
m: number;
56+
/** Day of month. */
4857
d: number;
49-
n?: number; // Day number where 1970-01-01 = 0.
50-
j?: boolean; // true if Julian calendar date.
58+
/** Day number where 1970-01-01 = 0. */
59+
n?: number;
60+
/** true if this is a Julian calendar date, false for Gregorian. */
61+
j?: boolean;
5162
}
5263

64+
/**
65+
* Type allowing a year alone to be specified, a full date as a [[YMDDate]], or a full date as a numeric array in the
66+
* form [year, month, date].
67+
*/
5368
export type YearOrDate = number | YMDDate | number[];
69+
/**
70+
* Type for specifying the date when a calendar switches from Julian to Gregorian, or if the calendar is purely Julian
71+
* or purely Gregorian. As a string, the letters 'J' or 'G' can be used.
72+
*/
5473
export type GregorianChange = YMDDate | CalendarType | string;
5574

75+
/** @hidden */
5676
export function handleVariableDateArgs(yearOrDate: YearOrDate, month?: number, day?: number): number[] {
5777
let year: number;
5878

@@ -72,12 +92,28 @@ export function handleVariableDateArgs(yearOrDate: YearOrDate, month?: number, d
7292
return [year, month, day];
7393
}
7494

95+
/**
96+
* Determine if a given date falls during the Julian calendar or the Gregorian calendar, given the standard
97+
* Gregorian change date of 1582-10-15.
98+
*
99+
* @param {YearOrDate} yearOrDate
100+
* @param {number} month
101+
* @param {number} day
102+
* @returns True if the date is Julian.
103+
*/
75104
export function isJulianCalendarDate_SGC(yearOrDate: YearOrDate, month?: number, day?: number): boolean {
76105
let year: number; [year, month, day] = handleVariableDateArgs(yearOrDate, month, day);
77106

78107
return (year < 1582 || (year === 1582 && (month < 10 || month === 10 && day < 15)));
79108
}
80109

110+
/**
111+
* Gets the day number for the given date, relative to 1970-01-01, using the standard Gregorian change date 1582-10-15.
112+
* @param yearOrDate
113+
* @param month
114+
* @param day
115+
* @returns Day number.
116+
*/
81117
export function getDayNumber_SGC(yearOrDate: YearOrDate, month?: number, day?: number): number {
82118
let year: number; [year, month, day] = handleVariableDateArgs(yearOrDate, month, day);
83119

@@ -90,6 +126,13 @@ export function getDayNumber_SGC(yearOrDate: YearOrDate, month?: number, day?: n
90126
return getDayNumberGregorian(year, month, day);
91127
}
92128

129+
/**
130+
* Gets the day number for the given Gregorian calendar date, relative to 1970-01-01.
131+
* @param yearOrDate
132+
* @param month
133+
* @param day
134+
* @returns Day number.
135+
*/
93136
export function getDayNumberGregorian(yearOrDate: YearOrDate, month?: number, day?: number): number {
94137
let year: number; [year, month, day] = handleVariableDateArgs(yearOrDate, month, day);
95138

@@ -100,6 +143,13 @@ export function getDayNumberGregorian(yearOrDate: YearOrDate, month?: number, da
100143
div_tt0(275 * month, 9) + day - 719559;
101144
}
102145

146+
/**
147+
* Gets the day number for the given Julian calendar date, relative to 1970-01-01 Gregorian.
148+
* @param yearOrDate
149+
* @param month
150+
* @param day
151+
* @returns Day number.
152+
*/
103153
export function getDayNumberJulian(yearOrDate: YearOrDate, month?: number, day?: number): number {
104154
let year: number; [year, month, day] = handleVariableDateArgs(yearOrDate, month, day);
105155

@@ -109,13 +159,22 @@ export function getDayNumberJulian(yearOrDate: YearOrDate, month?: number, day?:
109159
return 367 * year - div_rd(7 * (year + div_tt0(month + 9, 12)), 4) + div_tt0(275 * month, 9) + day - 719561;
110160
}
111161

112-
// Always returns 1. This function exists only to parallel getFirstDateInMonth, which can be a different
113-
// value when the Gregorian change date is not fixed.
114-
//
162+
/**
163+
* Always returns 1. This function exists only to parallel getFirstDateInMonth, which isn't always 1 when the
164+
* Gregorian change date is not fixed.
165+
* @returns First date of calender month.
166+
*/
115167
export function getFirstDateInMonth_SGC(year: number, month: number): number {
116168
return 1;
117169
}
118170

171+
/**
172+
* The last date of the given calendar month, using the standard Gregorian change date 1582-10-15, e.g. 31 for
173+
* any January, 28 for non-leap-year February, 29 for leap-year February, etc.
174+
* @param year
175+
* @param month
176+
* @returns Last date of calendar month.
177+
*/
119178
export function getLastDateInMonth_SGC(year: number, month: number): number {
120179
if (month === 9 || month === 4 || month === 6 || month === 11)
121180
return 30;
@@ -127,6 +186,12 @@ export function getLastDateInMonth_SGC(year: number, month: number): number {
127186
return 28;
128187
}
129188

189+
/**
190+
* The last date of the given Gregorian calendar month.
191+
* @param year
192+
* @param month
193+
* @returns Last date of calendar month.
194+
*/
130195
export function getLastDateInMonthGregorian(year: number, month: number): number {
131196
if (month === 9 || month === 4 || month === 6 || month === 11)
132197
return 30;
@@ -138,6 +203,12 @@ export function getLastDateInMonthGregorian(year: number, month: number): number
138203
return 28;
139204
}
140205

206+
/**
207+
* The last date of the given Gregorian calendar month.
208+
* @param year
209+
* @param month
210+
* @returns Last date of calendar month.
211+
*/
141212
export function getLastDateInMonthJulian(year: number, month: number): number {
142213
if (month === 9 || month === 4 || month === 6 || month === 11)
143214
return 30;
@@ -149,6 +220,15 @@ export function getLastDateInMonthJulian(year: number, month: number): number {
149220
return 28;
150221
}
151222

223+
/**
224+
* Returns the number of days in the given calendar month. Since this
225+
* function is for the standard Gregorian change date of 1582-10-15,
226+
* it returns 21 for 1582/10, otherwise it returns the same value as
227+
* [[getLastDateInMonth_SGC]].
228+
* @param year
229+
* @param month
230+
* @returns Total number of days in the given month.
231+
*/
152232
export function getDaysInMonth_SGC(year: number, month: number): number {
153233
if (year === 1582 && month === 10)
154234
return 21;
@@ -160,31 +240,32 @@ export function getDaysInMonth_SGC(year: number, month: number): number {
160240
return getDayNumber_SGC(year, 3, 1) - getDayNumber_SGC(year, 2, 1);
161241
}
162242

243+
/**
244+
* This typically returns 365, or 366 for a leap year, but for the year
245+
* 1582 it returns 355.
246+
* @param year
247+
* @returns Total number of days in the given year.
248+
*/
163249
export function getDaysInYear_SGC(year: number): number {
164250
return getDayNumber_SGC(year + 1, 1, 1) - getDayNumber_SGC(year, 1, 1);
165251
}
166252

167253
/**
168-
* @description Get day of week for a given 1970-01-01-based day number.
169-
*
170-
* @param {number} dayNum - 1970-01-01-based day number.
171-
*
172-
* @return {number} Day of week as 0-6: 0 for Sunday, 1 for Monday... 6 for Saturday.
254+
* Get day of week for a given 1970-01-01-based day number.
255+
* @param dayNum 1970-01-01-based day number.
256+
* @return Day of week as 0-6: 0 for Sunday, 1 for Monday... 6 for Saturday.
173257
*/
174258
export function getDayOfWeek(dayNum: number): number {
175259
return mod(dayNum + 4, 7);
176260
}
177261

178262
/**
179-
* @description Get day of week for a given date, assuming standard Gregorian change.
180-
*
181-
* @param {YearOrDate} yearOrDateOrDayNum - 1970-01-01-based day number (month and date must be left undefined) - OR -
182-
* YMDDate form y/m/d - OR - [y, m, d].
183-
* @param {number} month
184-
*
185-
* @param {number} day
186-
*
187-
* @return {number} Day of week as 0-6: 0 for Sunday, 1 for Monday... 6 for Saturday.
263+
* Get day of week for a given date, assuming standard Gregorian change.
264+
* @param yearOrDateOrDayNum 1970-01-01-based day number (month and date must be left undefined) - OR -
265+
* YMDDate form y/m/d - OR - [y, m, d].
266+
* @param month
267+
* @param day
268+
* @return Day of week as 0-6: 0 for Sunday, 1 for Monday... 6 for Saturday.
188269
*/
189270
export function getDayOfWeek_SGC(yearOrDateOrDayNum: YearOrDate, month?: number, day?: number): number {
190271
if (_.isNumber(yearOrDateOrDayNum) && _.isUndefined(month))
@@ -194,17 +275,15 @@ export function getDayOfWeek_SGC(yearOrDateOrDayNum: YearOrDate, month?: number,
194275
}
195276

196277
/**
197-
* @description Get the date of the index-th day of the week of a given month, e.g. the date of the
278+
* Get the date of the index-th day of the week of a given month, e.g. the date of the
198279
* first Wednesday or the third Monday or the last Friday of the month.
199-
*
200-
* @param {number} year - Year.
201-
* @param {number} month - Month.
202-
* @param {number} dayOfTheWeek - The day of the week (e.g. 0 for Sunday, 2 for Tuesday, 6 for Saturday) for
203-
* which you wish to find the date.
204-
* @param {number} index - A value of 1-5, or LAST (6), for the occurrence of the specified day of the week.
205-
*
206-
* @return {number} 0 if the described day does not exist (e.g. there is no fifth Monday in the given month) or
207-
* the date of the specified day.
280+
* @param year Year.
281+
* @param month Month.
282+
* @param dayOfTheWeek The day of the week (e.g. 0 for Sunday, 2 for Tuesday, 6 for Saturday) for
283+
* which you wish to find the date.
284+
* @param index A value of 1-5, or LAST (6), for the occurrence of the specified day of the week.
285+
* @return 0 if the described day does not exist (e.g. there is no fifth Monday in the given month) or
286+
* the date of the specified day.
208287
*/
209288
export function getDateOfNthWeekdayOfMonth_SGC(year: number, month: number, dayOfTheWeek: number, index: number): number {
210289
const last: boolean = (index >= LAST);

0 commit comments

Comments
 (0)