forked from RideReport/localdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
397 lines (347 loc) · 10.2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
* Classes for manipulating days, weeks, and months without needing to worry
* about time zones.
*/
/**
* Get a list of incremental numbers with a given start and end.
*
* @param from - the starting number, inclusive.
* @param to - the ending number, exclusive.
*/
function range(from: number, to: number): number[];
/**
* Get a list of incremental numbers, starting at zero.
*
* @param to – the number of items in the list.
*/
function range(to: number): number[];
function range(...args: [number] | [number, number]) {
let from = 0,
to = 0;
if (args.length === 1) {
to = args[0];
} else {
from = args[0];
to = args[1];
}
const result: number[] = [];
for (let i = from; i < to; i++) {
result.push(i);
}
return result;
}
export class DateFormatError extends Error {
constructor(date: any) {
super(`Date is not in a valid format: ${date}`);
}
}
/** A range of calendary days, without any knowledge of time of day or timezones.*/
export interface LocalDatePeriod {
start: LocalDate;
end: LocalDate;
}
/**
* Represents a day in time, without any knowledge of attached timezones. Allows us
* to safely manipulate the idea of a "report date" without worrying about confusing
* these date-only objects with specific datetimes.
*
* A lot of this involves taking advantage of the `toLocaleDateString` function,
* which is the only browser built-in that understands timezones.
*/
export class LocalDate implements LocalDatePeriod {
private readonly date: Date;
constructor(year: number, month: number, day: number) {
this.date = new Date(2000, 1, 1);
this.date.setFullYear(year, month - 1, day);
}
get year() {
return this.date.getFullYear();
}
/**
* Gets the month, 1-indexed so 1 is January
*/
get month() {
return this.date.getMonth() + 1;
}
get day() {
return this.date.getDate();
}
/**
* Get the day of the week. 0-indexed so 0 is Monday
*/
get weekday() {
return (this.date.getDay() + 6) % 7;
}
toString() {
return `${this.year}-${formatToTwoDigits(
this.month
)}-${formatToTwoDigits(this.day)}`;
}
toDate() {
// copy it so the LocalDate internals can't be changed
return new Date(this.date);
}
toLocalWeek() {
return new LocalWeek(this);
}
toLocalMonth() {
return new LocalMonth(this.year, this.month);
}
toQuarter() {
return new Quarter(
this.year,
(Math.trunc((this.month - 1) / 3) + 1) as QuarterNumber
);
}
/** Get an array of dates from this date to the target, inclusive */
range(to: LocalDate) {
const greater = to.toString() > this.toString();
let current: LocalDate = this;
const result: LocalDate[] = [];
while (!current.equals(to)) {
result.push(current);
current = greater ? current.plusDays(1) : current.minusDays(1);
}
result.push(to);
return result;
}
plusDays(days: number) {
// js dates roll the days over, so the 35th of January is converted
// to the 4th of February
return new LocalDate(this.year, this.month, this.day + days);
}
minusDays(days: number) {
return this.plusDays(-days);
}
equals(date: LocalDate) {
return (
date.year === this.year &&
date.month === this.month &&
date.day === this.day
);
}
isBefore(date: LocalDate) {
return this.toString() < date.toString();
}
isAfter(date: LocalDate) {
return this.toString() > date.toString();
}
static fromDateString(date: string) {
const parts = date.split("-");
if (parts.length !== 3) throw new DateFormatError(date);
const [year, month, day] = parts.map((n) => parseInt(n, 10));
if (isNaN(year) || isNaN(month) || isNaN(day)) {
throw new DateFormatError(date);
}
return new LocalDate(year, month, day);
}
static fromDateInTz(date: Date, timeZone: string) {
// yes, this is a weird hack
// it works tho and is actually how most libraries that don't involve
// 100mb imports do it too :(
const year = parseInt(
date.toLocaleDateString("en", { year: "numeric", timeZone }),
10
);
const month = parseInt(
date.toLocaleDateString("en", { month: "numeric", timeZone }),
10
);
const day = parseInt(
date.toLocaleDateString("en", { day: "numeric", timeZone }),
10
);
return new LocalDate(year, month, day);
}
static todayInTz(timeZone: string) {
return LocalDate.fromDateInTz(new Date(), timeZone);
}
static fromDate(date: Date) {
return new LocalDate(
date.getFullYear(),
date.getMonth() + 1,
date.getDate()
);
}
get start() {
/** Returns the date itself.
*
* Part of the {@link LocalDatePeriod} interface. **/
return this;
}
get end() {
/** Returns the date itself.
*
* Part of the {@link LocalDatePeriod} interface. **/
return this;
}
}
const formatToTwoDigits = (number: number) => {
return ("" + number).padStart(2, "0");
};
/**
* Represents a week without a timezone attached.
*/
export class LocalWeek implements LocalDatePeriod {
readonly monday: LocalDate;
/**
* There's no standard representation of a week, so we accept any day
* from that week as a valid reference to it
*/
constructor(date: LocalDate) {
this.monday = date.plusDays(-date.weekday);
}
get sunday() {
return this.monday.plusDays(6);
}
plusWeeks(weeks: number) {
return new LocalWeek(this.monday.plusDays(7 * weeks));
}
minusWeeks(weeks: number) {
return this.plusWeeks(-weeks);
}
isBefore(date: LocalWeek) {
return this.monday.isBefore(date.monday);
}
isAfter(date: LocalWeek) {
return this.monday.isAfter(date.monday);
}
toDays() {
return range(7).map((i) => this.monday.plusDays(i));
}
toString() {
return `${this.monday.toString()}--${this.sunday.toString()}`;
}
get start(): LocalDate {
/** The first day of the week.
*
* Part of the {@link LocalDatePeriod} interface.
*/
return this.monday;
}
get end() {
/** The last day of the week.
*
* Part of the {@link LocalDatePeriod} interface.
*/
return this.sunday;
}
}
/**
* Represents a month from a specific year, without a timezone attached.
*/
export class LocalMonth implements LocalDatePeriod {
private readonly date: LocalDate;
/**
* This handles weird entries for month, so "0" will be the December
* of the previous year, and "15" will the March of the next year
*/
constructor(year: number, month: number) {
this.date = new LocalDate(year, month, 1);
}
get year() {
return this.date.year;
}
get month() {
return this.date.month;
}
get first() {
return this.date;
}
get last() {
return this.plusMonths(1).first.minusDays(1);
}
plusMonths(months: number) {
return new LocalMonth(this.year, this.month + months);
}
minusMonths(months: number) {
return this.plusMonths(-months);
}
numberOfDays() {
return this.last.day;
}
weekdayStart() {
return this.first.weekday;
}
isAfter(month: LocalMonth) {
return this.toString() > month.toString();
}
isBefore(month: LocalMonth) {
return this.toString() < month.toString();
}
equals(month: LocalMonth) {
return this.year === month.year && this.month === month.month;
}
/**
* Generates an iterable of weeks in the month. A week is included if any
* days of that week are in the month.
*/
toWeeks() {
const result: LocalWeek[] = [];
let week = this.first.toLocalWeek();
while (
week.monday.toLocalMonth().equals(this) ||
week.sunday.toLocalMonth().equals(this)
) {
result.push(week);
week = week.plusWeeks(1);
}
return result;
}
toString() {
return `${this.year}-${formatToTwoDigits(this.month)}`;
}
static fromLocalDate(date: LocalDate) {
return new LocalMonth(date.year, date.month);
}
static fromString(date: string) {
const parts = date.split("-");
if (parts.length !== 2) throw new DateFormatError(date);
const [year, month] = parts.map((s) => parseInt(s, 10));
if (isNaN(year) || isNaN(month)) throw new DateFormatError(date);
return new LocalMonth(year, month);
}
static listForYear(year: number) {
return range(1, 13).map((month) => new LocalMonth(year, month));
}
get start() {
/** The first day of the month.
*
* Part of the {@link LocalDatePeriod} interface.
*/
return this.first;
}
get end() {
/** The last day of the month.
*
* Part of the {@link LocalDatePeriod} interface.
*/
return this.last;
}
}
type QuarterNumber = 1 | 2 | 3 | 4;
export class Quarter implements LocalDatePeriod {
readonly year: number;
readonly quarter: QuarterNumber;
constructor(year: number, quarter: QuarterNumber) {
this.year = year;
this.quarter = quarter;
}
get start(): LocalDate {
return new LocalDate(this.year, (this.quarter - 1) * 3 + 1, 1);
}
get end(): LocalDate {
return this.plusQuarters(1).start.minusDays(1);
}
plusQuarters(quarters: number) {
const count = this.year * 4 + this.quarter + quarters;
// This will not handle negative years correctly
return new Quarter(Math.trunc(count / 4), (count % 4) as QuarterNumber);
}
minusQuarters(quarters: number) {
return this.plusQuarters(-quarters);
}
toString() {
return `Q${this.quarter} ${this.year}`;
}
}